1

私は最初の Jsf、Jaas、JPA、JBoss アプリケーションを開発していますが、この問題が発生しています。JBoss で 2 つのセキュリティ ドメインを作成しました。

<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>

「データベース」ドメインを使用するとすべてが機能しますが、「カスタム」ドメインを使用するとロールをプリンシパルに設定できません。

私のSampleLoginModule

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

MySimpleプリンシパル

public class SamplePrincipal implements Principal {
    private String name;

    public SamplePrincipal() {
        super();
    }

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

メソッド commit 内のプリンシパルにロールを追加しisUserInRoleますfalse

これどうやってするの?

4

1 に答える 1

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 の実装です。

于 2013-04-14T21:10:53.513 に答える