2013-09-24 32 views
2

我正在嘗試基於表單的身份驗證,但我無法弄清楚爲什麼,在登錄頁面輸入正確的用戶名/密碼後,它會將我重定向到錯誤頁面而不是index.jsp基於表單的身份驗證 - 成功驗證後重定向到錯誤頁面(Tomcat 7.0.4)

當我鍵入:

http://localhost:8080/<context>/secure/index.jsp 

我得到的登錄頁面。但是,當我輸入用戶/密碼(經理/經理)時,它將我帶到error.html而不是index.jsp

WEB.XML:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>FormBasedAuthentication</display-name> 
<login-config> 
    <auth-method>FORM</auth-method> 
    <form-login-config> 
     <form-login-page>/login.html</form-login-page> 
     <form-error-page>/error.html</form-error-page> 
    </form-login-config> 
</login-config> 
<security-role> 
    <role-name>role1</role-name> 
</security-role>  
<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>SecurePages</web-resource-name> 
     <description>Security constraint for JSP resources</description> 
     <url-pattern>/secure/*</url-pattern> 
     <http-method>POST</http-method> 
     <http-method>GET</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>role1</role-name> 
    </auth-constraint> 
    <user-data-constraint> 
     <transport-guarantee>NONE</transport-guarantee> 
    </user-data-constraint> 
</security-constraint> 

TOMCAT-USER.XML:

<tomcat-users> 
    <role rolename="manager-gui"/> 
    <role rolename="manager-script"/> 
    <role rolename="manager-status"/> 
    <role rolename="manager-jmx"/> 
    <role rolename="role1"/> 
    <user username="manager" password="manager" roles="role1"/> 
</tomcat-users> 
+0

什麼是您的應用程序服務器? – zaerymoghaddam

+0

@moghaddam Tomcat7 – yapkm01

回答

2

在Tomcat配置的服務器位置本來應該是「使用Tomcat的安裝」,而不是「使用工作區的元數據」。

2

你必須有一個auth-constraintsecurity-constraint元素(而不是user-data-constraint )。使用auth-constraint你可以指定你的應用程序的用戶或角色可以訪問指定web-resource-collection(在JAAS詞彙和角色在web.xml中的詞彙命名主要)。此用戶或角色必須在security-role tag內的web.xml中定義。

最終的web.xml文件內容應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" 
     version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>FormBasedAuthentication</display-name> 
    <login-config> 
     <auth-method>FORM</auth-method> 
     <form-login-config> 
      <form-login-page>/login.jsp</form-login-page> 
      <form-error-page>/error.html</form-error-page> 
     </form-login-config> 
    </login-config> 
    <security-constraint> 
     <web-resource-collection> 
      <web-resource-name>SecurePages</web-resource-name> 
      <description>Security constraint for resources in the secure directory</description> 
      <url-pattern>/secure/*</url-pattern> 
      <http-method>POST</http-method> 
      <http-method>GET</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>admin</role-name> 
     </auth-constraint> 
    </security-constraint> 
    <security-role> 
     <role-name>admin</role-name> 
    </security-role> 
</web-app> 
+0

請參閱我的修訂問題。我已經做到了,現在正在工作。然而,在認證成功後,tomcat將請求重定向到error.html而不是index.jsp。任何想法? – yapkm01

4

成功的身份驗證和對/ secure/*的請求之間發生了一些錯誤。例如,可能沒有爲特定的HTTP方法配置過濾器,或者在這種情況下,servlet會拋出一個可能與auth失敗混淆的異常。如果我是你,我會用一個臨時servlet(或一個jsp:error.jsp)替換error.html來詳細檢查請求,以獲取有關失敗原因的詳細信息。 這些請求屬性應該檢查:

Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 
String exceptionType = (String) request.getAttribute("javax.servlet.error.exception_type"); 
String errorMsg = (String) request.getAttribute("javax.servlet.error.message"); 
相關問題