gpt4 book ai didi

jboss - 自定义主体不会传播到 Jboss AS 上的 EJB SessionContext

转载 作者:行者123 更新时间:2023-12-01 02:42:20 25 4
gpt4 key购买 nike

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

我定义了一个扩展 UsernamePasswordLoginModule 的自定义 UserLoginModule 并添加了一个自定义主体,但我的自定义主体不会传播到 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() is not return custom principal https://issues.jboss.org/browse/JBAS-8427

还有一个相关的话题: http://community.jboss.org/thread/44388 .

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

最佳答案

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

而不是“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) {
...
}

关于jboss - 自定义主体不会传播到 Jboss AS 上的 EJB SessionContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7887182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com