2010-10-27 70 views
2

Seam如何配置爲不同的Web資源集合使用不同的安全約束?如何通過角色聲明來驗證用戶?

web.xml我包括像

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>AdminPages</web-resource-name> 
     <url-pattern>/secure/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>admin</role-name> 
    </auth-constraint> 
</security-constraint> 

<security-role> 
    <role-name>admin</role-name> 
</security-role> 

一個節如果我省略上述(web.xml)中的配置。用戶使用JAAS進行身份驗證(僅密碼)。我不想爲Authenticatin編寫代碼,我真的只需要檢查用戶是否具有所需的角色(admin)。

在Seam中,這並不像預期的那樣工作。我在嘗試訪問頁面時收到HTTP-Errorcode 403 /secure/*

我在components.xml中配置了這適用於web.xml未更改的情況。

<security:identity jaas-config-name="admins" /> 

而且jboss-web.xml

<jboss-web> 
    <security-domain>java:/jaas/admins</security-domain> 
</jboss-web> 

的問題是我在哪裏配置中的作用。

回答

4

你必須在JBoss上建立一個新的安全域。

例如:

<policy> 
    <application-policy name="testUsersRoles"> 
     <authentication> 
      <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" 
          flag="required"> 
       <module-option name="usersProperties">usersb64.properties</module-option> 
       <module-option name="hashAlgorithm">MD5</module-option> 
       <module-option name="hashEncoding">base64</module-option> 
       <module-option name="unauthenticatedIdentity">nobody</module-option> 
      </login-module> 
     </authentication> 
    </application-policy> 
</policy> 

(在JBoss的情況下的conf/login-config.xml文件)。

你這裏有更多的信息:Security on JBoss

UPDATE:

關於「使用不同的網絡資源的集合不同的安全約束」你的問題的一部分,你可以將它添加不同的「安全性約束」對於每個組的資源控制:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>AdminPages</web-resource-name> 
     <url-pattern>/secure/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>admin</role-name> 
    </auth-constraint> 
</security-constraint> 


<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>CommonUserPages</web-resource-name> 
     <url-pattern>/common/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>admin</role-name> 
     <role-name>commonUser</role-name> 
    </auth-constraint> 
</security-constraint> 

<security-role> 
    <role-name>admin</role-name> 
    <role-name>commonUser</role-name> 
</security-role> 

請注意,這兩個角色將通過在登錄時asociated的LoginModule提取 時間。因此,當您的LoginModule對用戶進行身份驗證時,它將檢索此用戶所屬的一組角色。

+0

感謝您的回覆。是的,我確實設置了應用程序策略,因爲我注意到它適用於驗證用戶身份。問題是我還需要檢查角色。 – stacker 2010-10-27 15:31:01

+0

除了身份驗證之外,登錄模塊還負責授權(以鏈接到用戶會話的主體對象的形式加載用戶身份的用戶配置文件)。根據選擇的實現方式,它以不同的方式進行配置(用於LdapLoginModule的組,用於簡單UsersRolesLoginModule的roles.properties等)。 – 2010-10-27 15:34:32

1

通過postAuthenticate方法使用自定義縮進。

<security:identity jaas-config-name="admins" class="my.Identity"/> 

示例代碼:

package my; 

public class Identity extends org.jboss.seam.security.Identity { 

    private static final long serialVersionUID = 1L; 

    @Override 
    protected void postAuthenticate() { 
     super.postAuthenticate(); 
     if(isLoggedIn() && !hasRole("admin")) { 
      unAuthenticate(); 
     } 
    } 
} 
+0

謝謝,但我不再有這種環境。 (1) – stacker 2011-12-20 13:09:00