2012-08-01 65 views
2

我想在struts2中實現基於表單的認證。如何使用struts2實現基於表單的認證

我的目錄結構是:

enter image description here

我的web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <security-constraint> 
     <display-name>Example Security Constraint</display-name> 
     <web-resource-collection> 
      <web-resource-name>Protected Area</web-resource-name> 
      <url-pattern>/protected/*</url-pattern> 
      <http-method>DELETE</http-method> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
      <http-method>PUT</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>manager</role-name> 
     </auth-constraint> 
     <user-data-constraint> 
      <transport-guarantee>NONE</transport-guarantee> 
     </user-data-constraint> 
    </security-constraint> 


    <!-- Default login configuration uses form-based authentication --> 
    <login-config> 
     <auth-method>FORM</auth-method> 
     <realm-name>Example Form-Based Authentication Area</realm-name> 
     <form-login-config> 
      <form-login-page>/login.jsp</form-login-page> 
      <form-error-page>/error.jsp</form-error-page> 
     </form-login-config> 
    </login-config> 
    <security-role> 
     <description> An administrator </description> 
     <role-name> 
      manager 
     </role-name> 
    </security-role> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

我的login.jsp:

<form method="POST" action="j_security_check" > 
    <table border="0" cellspacing="5"> 
    <tr> 
     <th align="right">Username:</th> 
     <td align="left"><input type="text" name="j_username"></td> 
    </tr> 
    <tr> 
     <th align="right">Password:</th> 
     <td align="left"><input type="password" name="j_password"></td> 
    </tr> 
    <tr> 
     <td align="right"><input type="submit" value="Log In"></td> 
     <td align="left"><input type="reset"></td> 
    </tr> 
    </table> 
</form> 

行動支柱:

<action name= "j_security_check" class="action.LoginAction" > 
      <result name="success" >protected/index.jsp</result> 
      <result name="error" > error.jsp </result> 
      <result name="input" > login.jsp</result> 
     </action> 

我LoginAction.java

public class LoginAction extends ActionSupport { 

     String j_username, j_password; 

     public String getJ_password() { 
      return j_password; 
     } 

     public void setJ_password(String j_password) { 
      this.j_password = j_password; 
     } 

     public String getJ_username() { 
      return j_username; 
     } 

     public void setJ_username(String j_username) { 
      this.j_username = j_username; 
     } 

     @Override 
     public String execute() throws Exception { 
      if (getJ_username().equals(getJ_password())) { 
       return SUCCESS; 
      } else { 
       this.addActionError("Error..!"); 
       return ERROR; 
      } 
     } 
@Override 
    public void validate() { 
     if ((getJ_username() == null) || (getJ_username().length() == 0)) { 
      this.addActionError("Username Empty"); 
     } 
     if ((getJ_password() == null) || (getJ_password().length() == 0)) { 
      this.addActionError("Password Empty"); 
     } 
    } 

有了這個,我是當我插入相同的登錄ID和密碼,但我會重定向到錯誤頁面..

有人可以提供一個良好的鏈接是相同的。 。?

的例子應該包含一個受保護的文件夾,登錄一個動作類..

感謝..

+0

我不認爲SO是一個共享代碼的地方嗎?更好地提出你迄今爲止所做的工作以及你面臨的問題? – 2012-08-01 06:25:30

+0

其實m非常新的支持,並希望學習如何在其中實施表單身份驗證..所以我尋求從一個完美的例子開始..在互聯網上有幾個,但沒有一個是完美的。一些缺乏行動類和一些缺乏一個受保護的文件夾..可以幫助我出來plz .. – codeofnode 2012-08-01 06:31:13

+0

我不知道你的意思是受保護的文件夾?你需要什麼?你能解釋一下嗎? – 2012-08-01 06:33:47

回答

3

你將不得不提供對應於誰已經在創建了一個用戶名和密碼組合GlassFish服務器的文件領域並已分配給經理組。

Here是如何創建文件領域的鏈接。

Here是如何創建jdbc安全領域的鏈接。

Here是一個工作示例。你可以檢查它是否與你的代碼匹配。

Here是更好地理解基於表單的身份驗證的鏈接。

希望這會有所幫助。 :)

編輯

背後基於表單的身份驗證的想法是,你寫呈現以下字段和行動形式的JSP或Servlet的:

<form action="j_security_check" method="post"> 
    <input type="text" name="j_username"/> 
    <input type="password" name="j_password"/> 
    <input type="submit"/> 
</form> 

當窗體提交後,servlet容器會使用您定義的機制(例如JAAS)爲您檢查憑據。在你的web.xml,你設置以下:

<login-config> 
    <form-login-check> 
     <form-login-page>/login.jsp</form-login-page> 
     <form-error-page>/error.jsp</form-error-page> 
    </form-login-check> 
</login-config> 

這使容器定位JSP或Servlet的包含您的形式,或您的錯誤處理代碼。

+0

我想他只是想檢查表單提交和驗證。沒有必要創建一個數據庫記錄等只是爲了檢查您的工作流程。我相信他會在後面添加這些東西 – anu 2012-08-01 07:06:30

+0

在基於表單的身份驗證中,表單重定向到的行爲不是struts動作。因此,他可以通過指向他自己的支柱行動來檢查工作流程 – HashimR 2012-08-01 07:10:25

+0

downvote的原因? – HashimR 2012-08-01 07:10:40

相關問題