Spring Security(十):Logoutに登録する

9235 ワード

一:Spring Securityデフォルト終了処理ロジック
  • により、現在のセッションを無効にする
  • .
  • は、現在のユーザに関するRemember-meレコード
  • を明確にしている.
  • 現在のSecurityConteetをクリアします.
  • は、ログインページ
  • にリダイレクトする.
    二:Spring Security登出配置
    spring security設定spingに登録した場合、デフォルト値を使わないと自分の値を設定できます.
  • logoutUrl:対応するアドレスを登録する
  • logout Success Handler:成功したらここで自分の登出ロジックを処理できます.
  • deleteCookie:成功したら指定されたCookie
  • を削除します.
    protected void configure(HttpSecurity http) throws Exception {
         
    	http.csrf().disable()
    	.antMatchers("/login", "/session/invalid", "/logout", "/signOut").permitAll()
    	.logout()
    	    .logoutUrl("/logout")
    	    .logoutSuccessHandler(myLogoutSuccessHandler)
    	    .deleteCookies("JSESSIONID")
    	    .permitAll();
    }    
    
    @Slf4j
    @Component
    public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
         
        @Override
        public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
         
            log.info("    ");
    
            response.sendRedirect("/signOut");
        }
    }
    
    三:ページを出す
    パスとビューの簡単なマッピング
    @Configuration
    public class WebMvcConfiguration implements WebMvcConfigurer {
         
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
         
            registry.addViewController("/login").setViewName("login");
            registry.addViewController("/signOut").setViewName("signOut");
            registry.addViewController("/index").setViewName("index");
        }
    }
    
    
    signOut.はページを書き出します.
    
    <html lang="en"
          xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="utf-8">
        <title>  title>
    head>
    <body>
        
    body>
    html>