【问题标题】:isUserInRole sometimes returns falseisUserInRole 有时会返回 false
【发布时间】:2013-02-16 16:48:57
【问题描述】:

我正在开发我的第一个 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

我该怎么做?

【问题讨论】:

    标签: java jaas


    【解决方案1】:

    添加一个名为 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 的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-12
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      相关资源
      最近更新 更多