Spring Security(十一):ログアウト

20491 ワード

このページでは、登録を終了するためのいくつかの処理について説明します.登録を終了する方法、Spring Securityデフォルトのログアウト処理ロジック、ログアウト登録に関する設定項目が含まれます.
終了処理
どうやってログインを終了しますか
  • ログインページ
  • <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	index<br>
    	<a href="/signOut">  </a>
    </body>
    </html>
    
  • は、主な構成クラスに登録する必要があります.上記のurlを登出経路に設定し、登出後ジャンプページ
  • を設定します.
    	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
  • をクリアします.
  • は、
  • にリダイレクトされる.
    ログアウト登録に関する設定
  • 上にはページホッピングとSpring Securityのデフォルトのいくつかのログアウト処理ロジックが配置されているだけで、ログアウトする前にいくつかのカスタム処理をするには、成功したプロセッサを設定する必要があります.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()
    
  • 前編:Spring Security(十):Session管理
  • 次編:Spring Security(十二):Spring Security OAuth紹介