JBoss AS 7 Security: How to get a registered username?

I work in a Jboss AS 7 environment. My application / admIn / * is protected by a security constraint that requires forms-based authentication. A security domain is a database.

That's fine, but now I want to show "good morning" in every page title. I am looking for some kind of getLoggedUsername () or getPrincipal () function, but I cannot find it.

Please post a link to official documents, if any. Thanks.

+6
source share
1 answer

You should be able to use JAAS. This is what JBoss 7 should use.

The caller will be stored in a SessionContext , which you can get by telling JBoss about this resource.

 @Resource private SessionContext context; public void myAwesomeMethod() { String currentUser = context.getCallerPrincipal().getName(); } 

If for some reason Injection does not work on a Stateless bean, you can look for a direct EJBContext.

 @Stateless public class HelloBean implements com.foo.ejb.HelloRemote { public void hello() { try { InitialContext ic = new InitialContext(); SessionContext sctxLookup = (SessionContext) ic.lookup("java:comp/EJBContext"); System.out.println("look up EJBContext by standard name: " + sctxLookup); } catch (NamingException ex) { throw new IllegalStateException(ex); } } } 

This snippet was obtained from 4 ways to get an EJBContext .

+9
source

Source: https://habr.com/ru/post/915600/


All Articles