- 履歴一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- Spring Boot/パスワードの有効期限切れでリダイレクトする へ行く。
- 1 (2017-05-23 (火) 17:28:08)
- 2 (2017-05-23 (火) 17:29:10)
キーワード†
- Spring Boot
- HandlerInterceptor
- アスペクト
- 共通処理
したいこと†
ログインユーザーのパスワードが有効期限切れのとき、パスワードリセットページにリダイレクトする。
ただし、UserDetails#isCredentialsNotExpired()は使わない。これを使うとログインした状態でパスワードリセットにリダイレクトさせることができない。ログインさせないためのフラグなので。
どうやって†
HandlerInterceptorでアスペクト処理する。
@Component
public class PasswordResetHandlerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Authentication authentication = (Authentication) request.getUserPrincipal();
boolean isPasswordExpired =
Optional.ofNullable(authentication)
.map(a -> a.getPrincipal())
.filter(p -> p instanceof UserDetails)
.map(p -> (LoggedInUser) p)
.map(u -> u.isPasswordExpired()) // isCredentialsNotExpired()は無条件にtrue
.orElse(false);
if (isPasswordExpired) // リダイレクト処理
return ! isPasswordExpired;
}
}
これを登録する。
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
PasswordResetHandlerInterceptor passwordResetInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(passwordResetInterceptor);
}
}
ノート†
次に手こずった。
- HttpServletRequestからログインユーザーを引っこ抜く
- HandlerInterceptorを登録する