0
0

More than 5 years have passed since last update.

Spring Security ログインフォームの処理

Posted at

ログインのjsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ログイン</title>
</head>
<body>
    <div id="wrapper">
        <h3>ログインフォーム</h3>
        <c:if test="${param.containsKey('error')}">
            <span style="color: red;">
                <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
            </span>
        </c:if>
<%--        <c:url var="loginUrl" value="/login"/>  --%>
        <form:form action="/authentication">
            <table>
                <tr>
                    <td><label for="username">ユーザー名</label></td>
                    <td><input type="text" id="username" name="username"></td>
                </tr>
                <tr>
                    <td><label for="password">パスワード</label></td>
                    <td><input type="password" id="password" name="password"></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><button>ログイン</button></td>
                </tr>
            </table>
        </form:form>
    </div>
</body>
</html>

<%@ taglib prefix="c" uri=""%>でcという表記にuriの値を紐づけている

パラメータにコンテンツキー[エラー]の値が入っていれば、SPRING_SECURITY_LAST_EXCEPTION.messageの値を出力
formの行先は、/authentication
ユーザー名のnameは"username"
パスワードのnameは"password"

Handlerメソッド

@Controller
public class AuthenticationController {

    @RequestMapping(path = "/login", method = RequestMethod.GET)
        public String viewLogin() {
        return "loginForm";
    }
}

コントローラーです
"/login"に送られてきたGETを受け取り、loginFormを返す単純なメソッドです

xmlでのBean定義

security-config.xml
<sec:http>
    <sec:form-login 
                    login-page="/login"
        login-processing-url="/login"
        username-parameter="username"
        password-parameter="password"/>

    <sec:intercept-url pattern="/login" access="permitAll"/>
    <sec:intercept-url pattern="/**" access="isAuthenticated()"/>
</sec:http>

login-page属性に"/login"を設定すると、ログインフォームが設定される
login-processing-url属性に、"/login"を設定する
認証処理を行うurlになる
設定をしないとj_spring_security_checkが認証用のパスになる
username-parameter,password-parameter属性にそれぞれ設定する
そうするとユーザー名、パスワードをどういう名前で受け取るかが設定される
※sec:intercept-urlはより指定が細かいほうを上に書きましょう

認証が成功したときの動作

デフォルトでは、認証前にクライアントが行こうとしていたページにログイン完了後に遷移する
もともとログインフォームに対してアクセスしてきたユーザーはルートに飛ぶ

設定
security-config.xml
<sec:http>
    内容
    <sec:form-login default-target-url="/menu" />
</sec:http>

この設定で、ログイン完了後に"/menu"に飛ぶようになる

認証が失敗したときの動作

デフォルトではログインフォームに設定されているパスに"error"クエリパラメータを付けてリダイレクトする

設定
security-config.xml
<sec:http>
    中身
    <sec:form-login
        authentication-failure-url="/loginFailure" />
</sec:http>

まとめ

ここまでで、取りあえず動かしてみると自分で作成したログインフォームが表示されます
ですが、なぜかspring-securityのバージョンを5.0.0.RELEASEに上げないと上手くいきませんでした(4.2.3.RELEASEからの変更)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0