I have the following code working to run a Java (SE) application (not on server), where the login should be done using an existing JBoss server (I am tied to 4.2.3) running with JAAS authentication. I started with a simple console application to perform the login and later integrate this functionality to the application.
I use a snippet - found here - to perform the login:
JaasJbossConfiguration.activateConfiguration();
UsernamePasswordHandler handler =
new UsernamePasswordHandler("userName", "passWord");
LoginContext lc = new LoginContext("myrealm", handler);
try {
lc.login();
} catch (LoginException e) {
// Authentication failed.
}
This works like a charm. Now I want to extend my application and permit access only for users in special role. Is there any way to get the roles of the user from the Java application side or permit authentication only for those users?
@RolesAllowed
)EJBContext.getCallerPrincipal()
and EJBContext.isUserInRole()
in an EJBHttpServletRequest.getRemoteUser()
and HttpServletRequest.isUserInRole()
Anyway, look into the source of a login module (for example: DatabaseServerLoginModule
). Then write an EJB which does the same (regarding roles lookup), and which returns the list of roles to your stand-alone application.