Spring Security is a Security framework that provides declarative secure access control solutions for Spring-based enterprise applications. It provides a set of beans that can be configured in a Spring application context, taking full advantage of Spring IoC,DI (Inversion of Control), and AOP (aspect oriented programming) capabilities. Provide declarative secure access control function for application system, reduce the work of writing a lot of repetitive code for enterprise system security control.
Exit the principle
- remove
Cookie
- Clears the current user’s
remember-me
record - Make the current
session
failure - Clear the current
SecurityContext
- Redirect to the login screen
Spring Security’s exit request (which defaults to /logout) is intercepted by the LogoutFilter filter.
Implementation of exit
- Add an exit link to the home page
<a href="/signOut">exit</a>
Copy the code
- Configuration MerryyouSecurityConfig
. .and() .logout() .logoutUrl("/signOut")// Define the exit address
.logoutSuccessUrl("/register")// Go to the registration page after exit
.deleteCookies("JSESSIONID")// Delete the current JSESSIONID
.and()
......
Copy the code
Results the following
Source code analysis
LogoutFilter#doFilter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//#1. Matches the /logout request
if (requiresLogout(request, response)) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (logger.isDebugEnabled()) {
logger.debug("Logging out user '" + auth
+ "' and transferring to logout destination");
}
//#2. Handle steps 1-4
this.handler.logout(request, response, auth);
//#3. Redirect to the registration screen
logoutSuccessHandler.onLogoutSuccess(request, response, auth);
return;
}
chain.doFilter(request, response);
}
Copy the code
- Matches the currently intercepted request
- Processing to empty
Cookie
,remember-me
,session
andSecurityContext
- Redirect to the login screen
handler
CookieClearingLogoutHandler
emptyCookie
PersistentTokenBasedRememberMeServices
emptyremember-me
SecurityContextLogoutHandler
Make the currentsession
Void, empty currentSecurityContext
CookieClearingLogoutHandler#logout
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
for (String cookieName : cookiesToClear) {
//# 1. Set Cookie to null
Cookie cookie = new Cookie(cookieName, null);
String cookiePath = request.getContextPath();
if(! StringUtils.hasLength(cookiePath)) { cookiePath ="/";
}
cookie.setPath(cookiePath);
cookie.setMaxAge(0); response.addCookie(cookie); }}Copy the code
Cookie
Set to null
PersistentTokenBasedRememberMeServices#logout
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
super.logout(request, response, authentication);
if(authentication ! =null) {
// clear the persistent_logins tabletokenRepository.removeUserTokens(authentication.getName()); }}Copy the code
- Clear records in the persistent_logins table
SecurityContextLogoutHandler#logout
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
Assert.notNull(request, "HttpServletRequest required");
if (invalidateHttpSession) {
HttpSession session = request.getSession(false);
if(session ! =null) {
logger.debug("Invalidating session: " + session.getId());
//#1. Invalidate the current sessionsession.invalidate(); }}if (clearAuthentication) {
SecurityContext context = SecurityContextHolder.getContext();
Empty the current 'SecurityContext'
context.setAuthentication(null);
}
SecurityContextHolder.clearContext();
}
Copy the code
- Invalidates the current session
- Clear the current
SecurityContext
AbstractAuthenticationTargetUrlRequestHandler#handle
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//#1
String targetUrl = determineTargetUrl(request, response);
if (response.isCommitted()) {
logger.debug("Response has already been committed. Unable to redirect to "
+ targetUrl);
return;
}
//#2
redirectStrategy.sendRedirect(request, response, targetUrl);
}
Copy the code
- Gets the configured forward address
- A jump request
The code download
Download it from my Github, github.com/longfeizhen…