gpt4 book ai didi

java - isUserInRole 有时返回 false

转载 作者:太空宇宙 更新时间:2023-11-04 07:48:29 25 4
gpt4 key购买 nike

我正在开发我的第一个 Jsf、Jaas、JPA、JBoss 应用程序,现在我遇到了这个麻烦。我在 JBoss 中创建了两个安全域:

<security-domain name="Database" cache-type="default">
<authentication>
<login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
<module-option name="dsJndiName" value="java:jboss/JaasDS"/>
<module-option name="principalsQuery" value="select password from user where mail=?"/>
<module-option name="rolesQuery" value="select role, 'Roles' from user u where u.mail=?"/>
</login-module>
</authentication>
</security-domain>
<security-domain name="Custom" cache-type="default">
<authentication>
<login-module code="demo.SampleLoginModule" flag="required"/>
</authentication>
</security-domain>

如果我使用“数据库”域,一切正常,而如果我使用“自定义”域,我无法将角色设置为主体。

我的示例登录模块

public class SampleLoginModule implements LoginModule {
private String username;
private String password;

private SamplePrincipal userPrincipal;

public boolean login() throws LoginException {
//Here i check the credentials
}

public boolean commit() throws LoginException {
//Here i add principal to subject

userPrincipal.setName("username");

if (!(subject.getPrincipals().contains(userPrincipal)))
subject.getPrincipals().add(userPrincipal);
}
}
}

MySimplePrincipal

public class SamplePrincipal implements Principal {
private String name;

public SamplePrincipal() {
super();
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

我会在方法提交中向主体添加一个角色,因为 isUserInRole 否则返回 false

我该怎么做?

最佳答案

添加一个名为 Roles 的 java.security.acl.Group,其中包含用户的角色名称:

Set<Principal> principals = subject.getPrincipals();

Group roleGroup = new JAASGroup("Roles");
for (String role : userRoles)
roleGroup.addMember(new RolePrincipal(role));

// group principal
principals.add(roleGroup);

// username principal
principals.add(new UserPrincipal("user"));

其中 JAASGroup 是 java.security.acl.Group 的实现,RolePrincipal 和 UserPrincipal 是 java.security.Principal 的实现。

关于java - isUserInRole 有时返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14912665/

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