I am trying to convert an existing api from a web application that was previously deployed in bea weblogic. I need to deploy this application now in JBoss EAP 7.
In my user login code, I am doing servlet authentication like so:
HttpServletRequest request = ServletActionContext.getRequest();
request.login("user1", "password");
testEJB= (TestEJB) LookupUtil.lookup("TestEJB");
System.out.println("logged in user: " + testEJB.getName()); //returns user1
Now the sessionContext in every EJB looked up by the container returns the logged in user, which is perfect except that some app functions need to switch user and do some privileged actions e.g. uploading of document.
I am switching user and performing the privileged action like so:
LoginContext loginContext = new LoginContext("TestLoginContext", new UsernamePasswordHandler("user2", "password"));
loginContext.login();
String newUser = (String) Subject.doAs(loginContext.getSubject(), new TestPrivilegedAction());
System.out.println("privileged User: " + newUser); //still returns user1
In my TestPrivilegedAction which implements PrivilegedAction, I have the following codes:
@Override
public Object run() {
return this.getSwitchedUser();
}
private Object getSwitchedUser() {
testEJB= (TestEJB) LookupUtil.lookup("TestEJB");
System.out.println("logged in user: " + testEJB.getName());
}
Basically, the original api used when the app was deployed in weblogic are:
Authenticate.authenticate
Security.runAs
...then I replaced it with the following to make it work in JBoss:
LoginContext.login
Subject.doAs
So why now it still returns the logged in user (user1), even though the authentication succeeded and the subject is already set to user2? Ive been searching the web and trying to see if Im just missing some configurations but i'm really stuck with this now. I appreciate any feedback or suggestions.