LoginSignup
0
0

More than 3 years have passed since last update.

【SpringSecurity】SpringbootでのSpringSecurityの認証失敗時の制御設定について

Posted at

概要

以下、SpringBootでSpringSecurityの認証を実装する際のメモです。
SecurityConfigの設定において、認証失敗時のリダイレクト設定方法について記載しています。
SpringBootでのSpringSecurity認証までのService,Entityなど設定については他の方の詳細な記事を参考にたほうがいいかと思いますので省略します。

前提およびやりたいこと

ユーザIDとパスワードだけでなく3つ目の入力項目を持つ独自認証を作る必要があったため認証処理の拡張をしました。
その上で

  • 認証失敗時にリダイレクトするURLを設定する
  • 該当ユーザがアカウントロック中である場合にリダイレクトするURLを設定する・

の2つを設定する場合について記載します。
独自認証をしていない場合filter周りの記載が不要ないし変更する必要があると思います。

方法

OriginalSecurityConfig.java
//import省略

@EnableWebSecurity
public class OriginalSecurityConfig extends WebSecurityConfigurerAdapter {

    //独自認証処理をしているProvider
    @Autowired
    OriginalUsernamePasswordAuthenticationProvider originalProvider;

    //その他の@Autowired等省略

    @Override
    protected void configure(WebSecurity web) throws Exception {
        web.ignoring().andMatchers(
        //各種静的ファイル除外設定を記載
        );
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //独自認証用filterの設定
        OriginalUsernamePasswordAuthenticationFilter myFilter = new OriginalUsernamePasswordAuthenticationFilter();
        myFilter.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/admin/login/","POST");
        myFilter.setAuthenticationSuccessHandler(new SimpleUrlAuthentificaonSuccessHandler("/top"))        

        //独自認証用filterの設定
        ExceptionMappingAuthenticationFailureHandler failurehandler = new ExceptionMappingAuthenticationFailureHandler();
        Map<String, String> urlMap = new HashMap<>();
        urlMap.put(BadCredentialExceprion.class.getname(), "/admin/login/?error=1"); 
        urlMap.put(LockedExceprion.class.getname(), "/userLocked/"); 
        failurehandler.setExceptionMappings(urlMap);
        myFilter.setAuthenticationFailureHandler(failurehandler);

        http.addFilter(myFilter);

        http
            .antMatcher("/admin/**")
            .authorizeRequests()
            .antMatchers("/admin/login/").permitAll()
        //...以下任意の設定を記載

    }

    @Override
    protected void configure(AuthenticationmanagerBuilder auth) throws Exception {
        auth.authenticaitonProvider(originalProvider);
    }
}

configureの中でもExceptionMappingAuthenticationFailureHandlerに関する部分だけをメインに記載しています。流れとしては

  • Map<String, String>に発生させるExceptionとその場合のリダイレクトをセットで登録する
  • 各リダイレクト設定を持ったマップをExceptionMappingAuthenticationFailureHandlerにセットする
  • 独自認証filterのOriginalUsernamePasswordAuthenticationFilterに上記で作ったHandlerをセットする
  • HttpSecurityに独自認証のfilterをセットする
  • その他各URLの許可,不許可設定等を適宜HttpSecurityに対して登録していく

今回の例ではBadCredentialExceptionLockedExceptionだけを登録していますが、これ以外のExceptionでも登録可能です。
あとは独自認証をしているOriginalUsernamePasswordAuthenticationProviderの中でそれぞれのExeptionを発生させることで設定したリダイレクト先へと遷移することになります。

当然ですが、それぞれのリダイレクト先をController側で制御する必要があります。

OriginalController.java
@RequestMapping(value="/userLocked/")
public String locked() {
  //アカウントロック画面を返却などする
}
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