2

struts2 でフォームベースの認証を実装したいと考えています。

私のディレクトリ構造は次のとおりです。

ここに画像の説明を入力

私の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とパスワードを挿入すると、エラーページにリダイレクトされます..

誰かが同じものに良いリンクを与えることができます..?

この例には、ログイン用のアクション クラスである保護されたフォルダーが含まれている必要があります。

ありがとう..

4

1 に答える 1

3

GlassFish サーバーのファイル領域で既に作成され、マネージャーのグループに割り当てられているユーザーに対応するユーザー名とパスワードの組み合わせを提供する必要があります。

ファイル レルムの作成方法に関するリンクを次に示します。

jdbc セキュリティ レルムの作成方法に関するリンクを次に示します。

これが実際の例です。コードと一致するかどうかを確認できます。

フォームベースの認証をよりよく理解するためのリンクを次に示します。

お役に立てれば。:)

編集

フォーム ベースの認証の背後にある考え方は、次のフィールドとアクションを含むフォームを提示する JSP またはサーブレットを作成することです。

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

フォームが送信されると、サーブレット コンテナーは、定義したメカニズム (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 またはサーブレットを見つけることができます。

于 2012-08-01T07:04:21.737 に答える