2012-08-01 4 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 내가 같은은 loginid과 암호를 삽입, 아직 내가 오류 페이지로 리디렉션 할 때 나는이와

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"); 
     } 
    } 

..

사람은 같은에 대한 좋은 링크를 제공 할 수 있습니다. .?

보호 된 폴더, 로그인에 대한 작업 클래스 ..

덕분에 .. 포함해야한다 예

+0

저는 SO가 코드를 공유 할 장소라고 생각하지 않습니까? 지금까지해온 일과 앞으로 직면하게 될 일에 대해 더 잘 생각해보십시오. –

+0

실제로 struts에 매우 새로운 m 및 양식 인증을 구현하는 방법을 배우고 싶습니다. 그래서 완벽한 예제부터 시작하고 싶습니다.인터넷에는 몇 가지가 있지만 나에게는 완벽하지 않습니다. 일부는 액션 클래스가 부족하고 일부는 보호 된 폴더가 없습니다. plz을 도와 줄 수 있습니다 .. – codeofnode

+0

보호 된 폴더가 무엇을 의미하는지 모르시겠습니까? 너 모두 필요한거야? u 더 설명 할 수 있을까요? –

답변

3

당신은 이미 생성 된 사용자에 해당하는 사용자 이름과 암호 조합을 제공해야합니다 GlassFish Server의 파일 영역이며 관리자 그룹에 할당되었습니다.

Here은 File 영역을 만드는 방법에 대한 링크입니다.

Here은 jdbc 보안 영역을 만드는 방법에 대한 링크입니다.

Here은 작업 예입니다. 코드와 일치하는지 확인할 수 있습니다.

Here은 폼 기반 인증을 더 잘 이해할 수있는 링크입니다.

희망이 도움이됩니다. :)

편집는 : 양식 인 경우

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

:

폼 기반 인증 뒤에 아이디어는 다음과 같은 필드와 행동과 양식을 제시하는 JSP 나 서블릿을 쓰기 때문이다 제출 된 서블릿 컨테이너는 사용자가 정의한 메커니즘 (예 : 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

나는 그가 폼 제출과 validation을 확인하려고하고 있다고 생각한다. 워크 플로우를 점검하기 위해 db 레코드 등을 만들 필요가 없다. 나중에 그가 추가 할 것이라고 확신합니다. – anu

+0

폼 기반 인증에서 폼이 리다이렉션하는 작업은 struts 작업이 아닙니다. 그래서 그는 자신의 스트럿츠 동작을 가리켜 워크 플로를 확인할 수 있습니다. – HashimR

+0

downvote 이유? – HashimR

관련 문제