teratail
質問するログイン新規登録

Q&A

解決済

1回答

3755閲覧

Spring Securityでのログインにおける、WebSecurityConfigurerAdapterを継承したクラスについて

退会済みユーザー

退会済みユーザー

総合スコア0

Spring Security

Spring Securityは、Springのサブプロジェクトの一つでWebアプリケーションに必要な機能を追加します。正規ユーザーであるかを確認するための「認証機能」と、ユーザーのアクセスを制御する「認可機能」を簡単に追加することが可能です。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2019/06/05 14:11

0

0

質問

Spring Bootを使って開発をしており、Spring Securityを使ったログインについて、ネットに書かれているコードなどを見ながら調べています。

Spring Securityでは認証に際してWebSecurityConfigAdapterを継承したクラスを実装し、以下のようなコードを書くと思います。ここではコードの下部においてGlobalAuthenticationConfigurerAdapterを継承するインナークラスを使っていますが(別のファイルに書いているコードもありました)、この部分について質問があります。

Java

1@Configuration 2@EnableWebMvcSecurity 3public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 4 5 @Override 6 protected void configure(HttpSecurity httpSecurity) throws Exception 7 { 8 // 認可の設定 9 httpSecurity 10 .authorizeRequests() 11 .antMatchers("/login").permitAll() 12 .anyRequest().authenticated(); 13 14 // ログイン設定 15 httpSecurity 16 .formLogin() 17 .loginProcessingUrl("/login") 18 .loginPage("/login") 19 .failureHandler(new SampleAuthenticationFailureHandler()) 20 .usernameParameter("username") 21 .passwordParameter("password") 22 .defaultSuccessUrl("/") 23 .and(); 24 25 // ログアウト設定 26 httpSecurity 27 .logout() 28 .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 29 .logoutSuccessUrl("/login"); 30 } 31 32 @Configuration 33 protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { 34 @Autowired 35 UserDetailsService userDetailsService; 36 37 @Override 38 public void init(AuthenticationManagerBuilder auth) throws Exception { 39 // 認証するユーザーを設定する 40 auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 41 } 42 } 43}

上記のコードでは、GlobalAuthenticationConfigurerAdapterを継承するクラスを実装し、init()という関数をオーバーライドすれば動くのか、となんとなくわかるのですが、下記のようなコードも見られることがわかりました。(最後のconfigureGlobal()以外の部分は同じです)

Java

1@Configuration 2@EnableWebMvcSecurity 3public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 4 5 @Override 6 protected void configure(HttpSecurity httpSecurity) throws Exception 7 { 8 // 認可の設定 9 httpSecurity 10 .authorizeRequests() 11 .antMatchers("/login").permitAll() 12 .anyRequest().authenticated(); 13 14 // ログイン設定 15 httpSecurity 16 .formLogin() 17 .loginProcessingUrl("/login") 18 .loginPage("/login") 19 .failureHandler(new SampleAuthenticationFailureHandler()) 20 .usernameParameter("username") 21 .passwordParameter("password") 22 .defaultSuccessUrl("/") 23 .and(); 24 25 // ログアウト設定 26 httpSecurity 27 .logout() 28 .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 29 .logoutSuccessUrl("/login"); 30 } 31 32 @Autowired 33 public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception { 34 auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 35 } 36}

この書き方だと、configureGlobal()は他のメソッドをオーバーライドしたりしているわけでもないように見え、どのタイミングでどう呼ばれるのかすら不明なのですが、なぜこのコードで問題なく動くのか、わかる方がいらっしゃれば教えていただけると助かります。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

guest

回答の取得に失敗しました

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問