Custom Principal不会传播到Jboss AS上的EJB SessionContext

时间:2011-10-25 09:22:45

标签: jboss ejb

在EJB项目中,我需要替换“javax.ejb.SessionContext”中的call princial name。我使用Jboss AS 6.0 Final作为应用程序服务器。

我定义了一个自定义UserLoginModule,它扩展了UsernamePasswordLoginModule并添加了一个自定义主体,但我的自定义主体不会传播到EJB SessionContext。

以下是我的自定义登录模块中的一些代码:

@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);
}

}

我的自定义登录模块运行良好,但我从“javax.ejb.SessionContext”获得的调用者主体仍然是SimplePrincipal。

原来有一个Jobss错误:EJBContext.getCallerPrincipal()没有返回自定义主体https://issues.jboss.org/browse/JBAS-8427

相关主题:http://community.jboss.org/thread/44388

我想知道您是否对此有一些经验并且替换Jboss创建的默认主体是否安全?有任何副作用吗?

1 个答案:

答案 0 :(得分:3)

在我的团队的帮助下,我得到了一个解决方案,希望这对那些遇到同样问题的人有所帮助。

而不是“sessionContext.getCallerPrincipal()” 使用以下命令获取自定义主体:

        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) {
            ...
        }