LoginSignup
4
3

More than 5 years have passed since last update.

spring Security ログインフォームの項目とSecurytyConfig、値の受け渡し

Last updated at Posted at 2016-04-09

目的

画面のログインフォームから取得される
ユーザー名
パスワード
を、securityConfigから受けとる設定

確認ポイント

Java側 SecurityConfig.java
// フィルタの設定
    @Override
    protected void configure(HttpSecurity http)throws Exception{
        http.authorizeRequests().antMatchers("/loginForm").permitAll().anyRequest().authenticated();
        http.formLogin().loginProcessingUrl("/login").loginPage("/loginForm").failureUrl("/loginForm?error").defaultSuccessUrl("/testdb", true)
.usernameParameter("user_id") <=この箇所
.passwordParameter("password") <=で値を受け取っている
.and();
        http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout**")).logoutSuccessUrl("/loginForm");
    }

値の受け取りは
configureメソッドないの

  • usernameParameter
  • passwordParameter メソッドで行う

ここで指定する文字列は、画面上のhtmlのname属性の名前と一致させる

画面側
<form class="form-sinin" method="post" th:action="@{/login}">
        <h2 class="form-signin-heading">ログインフォーム</h2>
        <div th:if="${param.error}" class="alert alert-danger">
            ユーザーidまたはパスワードが誤っています
        </div>
        <input type="text" class="form-control" name="user_id" placeholder="user id" required="required" autofocus="autofocus" />
        <input type="password" class="form-control" name="password" placeholder="password" required="required" />
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>

input項目のname属性

  • name="user_id"
  • name="password"

以上のようにして、フォームからSecurityConfigへログイン画面の入力を反映する

4
3
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
4
3