Spring Security(十一):ログアウト
このページでは、登録を終了するためのいくつかの処理について説明します.登録を終了する方法、Spring Securityデフォルトのログアウト処理ロジック、ログアウト登録に関する設定項目が含まれます.
終了処理
どうやってログインを終了しますかログインページ は、主な構成クラスに登録する必要があります.上記のurlを登出経路に設定し、登出後ジャンプページ を設定します.は、現在 を使用する.は、現在のユーザに関する をクリアする.現在の をクリアします.は、 にリダイレクトされる.
ログアウト登録に関する設定上にはページホッピングとSpring Securityのデフォルトのいくつかのログアウト処理ロジックが配置されているだけで、ログアウトする前にいくつかのカスタム処理をするには、成功したプロセッサを設定する必要があります. は、このカスタマイズされた登出プロセッサ 終了時には、ブラウザのクッキー情報をクリアする必要があります.対応する構成項目は です.前編:Spring Security(十):Session管理 次編:Spring Security(十二):Spring Security OAuth紹介
終了処理
どうやってログインを終了しますか
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index<br>
<a href="/signOut"> </a>
</body>
</html>
http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
.formLogin()
.loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form")
.successHandler(meicloudAuthenticationSuccessHandler)
.failureHandler(meicloudAuthenticationFailureHandler)
.and()
.rememberMe()
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(3600)
.userDetailsService(userDetailsService)
.and()
.authorizeRequests()
.antMatchers("/authentication/require", securityProperties.getBrowser().getSignInPage(), "/code/*").permitAll()
.anyRequest()
.authenticated()
.and()
.logout()
//
.logoutUrl("/signOut")
//
.logoutSuccessUrl("meicloud-logout.html")
.and()
.csrf().disable()
.apply(smsCodeAuthenticationSecurityConfig);
Spring Securityデフォルトのログアウト処理ロジックSession
remember-me
SecurityContext
ログアウト登録に関する設定
LogoutSuccessHandler
インターフェースを実現します.public class MeicloudLogoutSuccessHandler implements LogoutSuccessHandler {
private Logger logger = LoggerFactory.getLogger(getClass());
public MeicloudLogoutSuccessHandler(String signOutSuccessUrl) {
this.signOutSuccessUrl = signOutSuccessUrl;
}
private String signOutSuccessUrl;
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
logger.info(" ");
// , ,
if (StringUtils.isBlank(signOutSuccessUrl)) {
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(new SimpleResponse(" ")));
} else {
response.sendRedirect(signOutSuccessUrl);
}
}
}
MeicloudLogoutSuccessHandler
を主構成クラスに配置する必要があり、logoutSuccessHandler
の構成項目とlogoutSuccessUrl
の構成項目は互いに反発することに注意する. http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
.formLogin()
.loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form")
.successHandler(meicloudAuthenticationSuccessHandler)
.failureHandler(meicloudAuthenticationFailureHandler)
.and()
.rememberMe()
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(3600)
.userDetailsService(userDetailsService)
.and()
.authorizeRequests()
.antMatchers("/authentication/require", securityProperties.getBrowser().getSignInPage(), "/code/*").permitAll()
.anyRequest()
.authenticated()
.and()
.logout()
//
.logoutUrl("/signOut")
//
// .logoutSuccessUrl("meicloud-logout.html")
//
.logoutSuccessHandler(logoutSuccessHandler)
.and()
.csrf().disable()
.apply(smsCodeAuthenticationSecurityConfig);
deleteCookies
.logout()
//
.logoutUrl("/signOut")
//
// .logoutSuccessUrl("meicloud-logout.html")
//
.logoutSuccessHandler(logoutSuccessHandler)
.deleteCookies("JSESSIONID")
.and()