簡體   English   中英

如何通過用戶角色聲明對用戶進行身份驗證?

[英]How to authenticate users by their role declarative?

如何將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編寫代碼,實際上只需要檢查用戶是否具有所需角色(管理員)即可。

在Seam中,此功能無法正常工作。 嘗試訪問/secure/*的頁面時收到HTTP錯誤代碼403

我在components.xml配置了,這在不更改web.xml的情況下有效。

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

還有jboss-web.xml

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

問題是我在哪里配置角色。

您必須在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文件中)。

您在這里有更多信息: JBoss上的安全性

更新:

關於您的問題的“對不同的Web資源集合使用不同的安全性約束”部分,您可以設置為每個要控制的資源組添加一個不同的“安全性約束”:

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

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

使用帶有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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM