自定义主体将不会传播到Jboss AS上的EJB SessionContext [英] Custom Principal won't be propagated to EJB SessionContext on Jboss AS

查看:104
本文介绍了自定义主体将不会传播到Jboss AS上的EJB SessionContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EJB项目中,我需要替换javax.ejb.SessionContext中的调用公式名称。我使用Jboss AS 6.0 Final作为应用程序服务器。

In a EJB project, I need to replace the call princial name in "javax.ejb.SessionContext". I use Jboss AS 6.0 Final as the application server.

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

I defined a custom UserLoginModule that extends UsernamePasswordLoginModule and added a custom principal, but my custom principal won't be propagated to EJB SessionContext.

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

Here is some code from my custom 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);
}

}

我的自定义登录模块工作得很好,但是我从javax.ejb.SessionContext获取的调用者主体仍然是SimplePrincipal。

My custom login module works well, but the caller principal I get from "javax.ejb.SessionContext" is still SimplePrincipal.

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

It turned out that there is a Jobss bug: EJBContext.getCallerPrincipal() is not returning custom principal https://issues.jboss.org/browse/JBAS-8427

相关主题: http://社区。 jboss.org/thread/44388

我想知道你是否有一些经验,是否可以安全地替换Jboss创建的默认主体?是否有任何副作用?

I wonder if you have some experiences on this and is it safe to replace the default principal Jboss creates? Are ther any side effects?

推荐答案

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

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

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

Instead of "sessionContext.getCallerPrincipal()" Use the following to get the custom principal:

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

这篇关于自定义主体将不会传播到Jboss AS上的EJB SessionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆