簡體   English   中英

Wildfly表單認證

[英]Wildfly Form Authentication

我已經嘗試了好幾天了。 我正在嘗試為在wildfly上運行的JAVA EE應用程序創建基於表單的身份驗證。 我正在使用JAX-RS和AngularJS。 我創建了wildfly安全域,如下所示

 <security-domain name="malison">
                <authentication>
                    <login-module code="Database" flag="required">
                        <module-option name="dsJndiName" value="java:jboss/datasources/malisonDS"/>
                        <module-option name="principalsQuery" value="select password from USER where user=?"/>
                        <module-option name="rolesQuery" value="select position from USER where user=?"/>
                    </login-module>
                </authentication>
            </security-domain>

並配置了我的web.xml

 <security-constraint>
    <display-name>UnSecuredPages</display-name>

    <web-resource-collection>
        <web-resource-name>Access</web-resource-name>
        <url-pattern>/api/user/*</url-pattern>
    </web-resource-collection>        
    <web-resource-collection>
        <web-resource-name>Access</web-resource-name>
        <url-pattern>/user.jsp</url-pattern>
    </web-resource-collection>

    <web-resource-collection>
        <web-resource-name>Access</web-resource-name>
        <url-pattern>/assets/*</url-pattern>
    </web-resource-collection>
</security-constraint>

<security-role>
    <role-name>ADMIN</role-name>
</security-role>

現在我在為我的身份驗證功能編寫代碼時遇到問題。 哪個應該向客戶端發送成功回復。 我以為這樣可以。

    @POST
@Path("/authenticate")
@Consumes(MediaType.APPLICATION_JSON)
public String authenticate(@Context HttpServletRequest request, JSONObject obj){

    String userName = String.valueOf(obj.get("username"));
    String password = String.valueOf(obj.get("password"));
    try{
        request.login(userName, password);
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return "{\"success\":true, \"msg\": \"Saved successfully\"}";
}

任何人都可以提供解決方案或解決方法,我將不勝感激...

您需要告訴Wildfly該應用程序使用哪個安全域進行身份驗證。 將jboss-web.xml添加到WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
    <security-domain>java:/jaas/malison</security-domain>
</jboss-web>

另外,您可能需要向web.xml添加登錄配置。 這是HTML基本身份驗證的示例:

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

此處有更多詳細信息: https : //dzone.com/articles/understanding-web-security

但是請注意,由於web.xml定義了表單身份驗證,因此您沒有進行表單身份驗證。 您直接在HTTP請求上調用login方法,而在web.xml中配置了表單身份驗證后,在身份驗證成功后,應用程序服務器將自動重定向到登錄頁面並返回安全的URL。

暫無
暫無

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

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