User Principal will not be distributed in EJB SessionContext on Jboss AS

In an EJB project, I need to replace the name of the main call in "javax.ejb.SessionContext". I am using Jboss AS 6.0 Final as an application server.

I have defined a custom UserLoginModule that extends UsernamePasswordLoginModule and adds a user principle, but my user principle will not propagate in the EJB SessionContext.

Here is the code from my user login module:

@Override protected Group[] getRoleSets() throws LoginException { Group[] groups = new Group[2]; groups[0] = new SimpleGroup("Roles"); groups[0].addMember(createRoleIdentity()); Group callerPrincipal = new SimpleGroup("CallerPrincipal"); callerPrincipal.addMember(createIdentity(this.getUsername())); groups[1] = callerPrincipal; subject.getPrincipals().add(callerPrincipal); return groups; } @Override protected Principal createIdentity(String username) throws LoginException { return new MyCustomPrincipal(username); } } 

My user login module works fine, but the main caller I get from "javax.ejb.SessionContext" is still SimplePrincipal.

It turned out that there is a Jobss error: EJBContext.getCallerPrincipal () does not return the user main https://issues.jboss.org/browse/JBAS-8427

And related topic: http://community.jboss.org/thread/44388 .

I wonder if you have some experience with this issue and is it safe to replace the main Jboss project? Are there any side effects?

+4
source share
1 answer

With the help of my team, I got a solution, I hope this can be useful for those who have the same problem.

Instead of "sessionContext.getCallerPrincipal ()" To get a custom principal, use the following:

  try { Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container"); Set<Group> subjectGroups = subject.getPrincipals(Group.class); Iterator<Group> iter = subjectGroups.iterator(); while (iter.hasNext()) { Group group = iter.next(); String name = group.getName(); if (name.equals("CallerPrincipal")) { Enumeration<? extends Principal> members = group.members(); if (members.hasMoreElements()) { Principal principal = (Principal) members.nextElement(); return principal; } } } } } catch (PolicyContextException e1) { ... } 
+3
source

All Articles