- 履歴一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- Spring Boot/特定のログインエラーで画面遷移する へ行く。
- 1 (2017-05-22 (月) 17:37:57)
- 2 (2017-05-23 (火) 09:18:19)
- 3 (2017-05-23 (火) 17:28:35)
キーワード†
- Spring Boot
- Spring Security
したいこと†
特定のログインエラー時に画面遷移したい(ログイン画面には戻らずに)。
どうやって†
認証失敗したときのハンドラを用意する†
public class SecurityConfigAuthenticationFailureHandler
extends ExceptionMappingAuthenticationFailureHandler{
public SecurityConfigAuthenticationFailureHandler() {
this.setDefaultFailureUrl("/");
this.setExceptionMappings(getFailureUrlMap());
}
private Map<String, String> getFailureUrlMap() {
Map<String, String> map = new HashMap<>();
map.put(CredentialsExpiredException.class.getName(), "/reset_password");
return map;
}
}
- Mapは<String,String>
- 例外のクラス名
- リダイレクト先(コンテキストパスは補ってくれる)
ハンドラをセットする†
http.formLogin()
.loginPage("/")
.failureHandler(new SecurityConfigAuthenticationFailureHandler())
// あといろいろ
リダイレクト先を認証不要にする†
http.authorizeRequests()
// 認証不要
.antMatchers(
// ログイン
"/",
// パスワードリセット
"/reset_password"
)
.permitAll()
// あといろいろ
ノート†
こうだよとバシッと書いてくれてる記事が見つからなくて少してこずった。