diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index 644b38b60..4272f3395 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -21,13 +21,13 @@ package com.pig4cloud.pigx.auth.config; import com.pig4cloud.pigx.common.security.handler.FormAuthenticationFailureHandler; import com.pig4cloud.pigx.common.security.handler.MobileLoginSuccessHandler; +import com.pig4cloud.pigx.common.security.handler.SsoLogoutSuccessHandler; import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.annotation.Order; -import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; @@ -36,6 +36,7 @@ import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; /** * @author lengleng @@ -51,12 +52,9 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) { http.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form") .failureHandler(authenticationFailureHandler()).and().logout() - .logoutSuccessHandler((request, response, authentication) -> { - String referer = request.getHeader(HttpHeaders.REFERER); - response.sendRedirect(referer); - }).deleteCookies("JSESSIONID").invalidateHttpSession(true).and().authorizeRequests() - .antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll().anyRequest().authenticated().and() - .csrf().disable().apply(mobileSecurityConfigurer()); + .logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true) + .and().authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll() + .anyRequest().authenticated().and().csrf().disable().apply(mobileSecurityConfigurer()); } /** @@ -80,6 +78,11 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { return new FormAuthenticationFailureHandler(); } + @Bean + public LogoutSuccessHandler logoutSuccessHandler() { + return new SsoLogoutSuccessHandler(); + } + @Bean public AuthenticationSuccessHandler mobileLoginSuccessHandler() { return new MobileLoginSuccessHandler(); diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java new file mode 100644 index 000000000..53f5602d7 --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java @@ -0,0 +1,38 @@ +package com.pig4cloud.pigx.common.security.handler; + +import cn.hutool.core.util.StrUtil; +import org.springframework.http.HttpHeaders; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author lengleng + * @date 2020/10/6 + *

+ * sso 退出功能 ,根据客户端传入跳转 + */ +public class SsoLogoutSuccessHandler implements LogoutSuccessHandler { + + private static final String REDIRECT_URL = "redirect_url"; + + @Override + public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) + throws IOException { + + // 获取请求参数中是否包含 回调地址 + String redirectUrl = request.getParameter(REDIRECT_URL); + if (StrUtil.isNotBlank(redirectUrl)) { + response.sendRedirect(redirectUrl); + } + else { + // 默认跳转referer 地址 + String referer = request.getHeader(HttpHeaders.REFERER); + response.sendRedirect(referer); + } + } + +}