Accessing the clients principal inside an ejb meth

2019-08-06 06:07发布

问题:

I need to access the clients principal (username ) inside a ejb method. I don't want to add it as a parameter. Tryed adding them to Context object like ;

prop.add(Context.SECURITY_AUTHENTICATION,"user")
prop.add(Context.SECURITY_CREDENTIALS,"pass")

but trying to access them inside the method like; @Resource private SessionContext ctx;

public void someMethod() {
    Principal principal = ctx.getCallerPrincipal();
    //returns anonymous

still give me the annoymous user.

Working on weblogic, any pointers ?

Thanks

回答1:

Similar issue...I am testing with a client cert along with a custom UserNameMapper. My UserNameMapper returns "steve", but the principal within the EJB was returning "<anonymous>" until I added a "steve" user via the console.

Environment env = new Environment();
env.setInitialContextFactory(Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
//  env.setSecurityPrincipal("user");
//  env.setSecurityCredentials("pass");
env.setProviderUrl("t3s://localhost:7002");

InputStream key = new PEMInputStream(new FileInputStream(CERT_KEYFILE));
InputStream cert = new PEMInputStream(new FileInputStream(CERT_CERTFILE));
env.setSSLClientCertificate(new InputStream[] {key, cert});
env.setSSLClientKeyPassword(CERT_KEYPASSWORD);

Same issue when using a JAAS client with the UsernamePasswordLoginModule. Fixed by setting the username/password within the InitialContext lookup within the PrivilegedAction. The EJB references the latter as the principal as it can be a different username/password.