|
|
@@ -8,6 +8,8 @@ import com.ningdatech.pmapi.user.security.auth.agent.AgentAuthSecurityConfig; |
|
|
|
import com.ningdatech.pmapi.user.security.auth.common.CommonAuthSecurityConfig; |
|
|
|
import com.ningdatech.pmapi.user.security.auth.credential.CredentialAuthSecurityConfig; |
|
|
|
import com.ningdatech.pmapi.user.security.auth.handler.DefaultExpiredSessionStrategy; |
|
|
|
import com.ningdatech.pmapi.user.security.auth.handler.DefaultLogoutSuccessHandler; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
@@ -27,29 +29,16 @@ import java.util.Set; |
|
|
|
* @Version 1.0 |
|
|
|
*/ |
|
|
|
@Configuration |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
|
private final AuthProperties authProperties; |
|
|
|
private final CredentialAuthSecurityConfig credentialAuthSecurityConfig; |
|
|
|
private final LogoutSuccessHandler logoutSuccessHandler; |
|
|
|
private final DefaultLogoutSuccessHandler logoutSuccessHandler; |
|
|
|
private final DefaultExpiredSessionStrategy defaultExpiredSessionStrategy; |
|
|
|
private final AgentAuthSecurityConfig agentAuthSecurityConfig; |
|
|
|
private final CommonAuthSecurityConfig commonAuthSecurityConfig; |
|
|
|
|
|
|
|
public WebSecurityConfig(AuthProperties authProperties, |
|
|
|
CredentialAuthSecurityConfig credentialAuthSecurityConfig, |
|
|
|
AgentAuthSecurityConfig agentAuthSecurityConfig, |
|
|
|
CommonAuthSecurityConfig commonAuthSecurityConfig, |
|
|
|
@Qualifier(value = "defaultLogoutSuccessHandler") LogoutSuccessHandler logoutSuccessHandler, |
|
|
|
DefaultExpiredSessionStrategy defaultExpiredSessionStrategy) { |
|
|
|
this.authProperties = authProperties; |
|
|
|
this.credentialAuthSecurityConfig = credentialAuthSecurityConfig; |
|
|
|
this.agentAuthSecurityConfig = agentAuthSecurityConfig; |
|
|
|
this.commonAuthSecurityConfig = commonAuthSecurityConfig; |
|
|
|
this.logoutSuccessHandler = logoutSuccessHandler; |
|
|
|
this.defaultExpiredSessionStrategy = defaultExpiredSessionStrategy; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
|
|
assemblerPreAuthUrls(http); |
|
|
@@ -59,27 +48,29 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
|
.and().apply(agentAuthSecurityConfig) |
|
|
|
.and().apply(commonAuthSecurityConfig) |
|
|
|
.and() |
|
|
|
.authorizeRequests().antMatchers(authProperties.getIgnoreAuthUrlsArray()).permitAll().anyRequest() |
|
|
|
.authorizeRequests() |
|
|
|
.antMatchers(authProperties.getIgnoreAuthUrlsArray()) |
|
|
|
.permitAll() |
|
|
|
.anyRequest() |
|
|
|
.authenticated().and() |
|
|
|
// 防止固定会话攻击,Spring security的默认配置就是如此: |
|
|
|
// 登陆成功之后会创建一个新的会话,然后将旧的session信息复制到新的session中(客户端的sessionId变了) |
|
|
|
.sessionManagement().invalidSessionUrl(authProperties.getInvalidSessionUrl()).sessionFixation() |
|
|
|
.sessionManagement() |
|
|
|
.invalidSessionUrl(authProperties.getInvalidSessionUrl()) |
|
|
|
.sessionFixation() |
|
|
|
.migrateSession() |
|
|
|
// .invalidSessionStrategy(defaultInvalidSessionStrategy) |
|
|
|
.maximumSessions(10) |
|
|
|
.maxSessionsPreventsLogin(true) |
|
|
|
.expiredSessionStrategy(defaultExpiredSessionStrategy) |
|
|
|
.and().and() |
|
|
|
.logout().logoutUrl(authProperties.getLogoutUrl()).logoutSuccessHandler(logoutSuccessHandler) |
|
|
|
.logout() |
|
|
|
.logoutUrl(authProperties.getLogoutUrl()) |
|
|
|
.logoutSuccessHandler(logoutSuccessHandler) |
|
|
|
.deleteCookies(CommonConst.COOKIE_KEY) |
|
|
|
// .and() |
|
|
|
// .cors().configurationSource(corsConfigurationSource()) |
|
|
|
.and() |
|
|
|
// .csrf().disable(); |
|
|
|
// 开启csrf验证,需要前端同步传入token |
|
|
|
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) |
|
|
|
.ignoringAntMatchers(authProperties.getIgnoreCsrfUrlsArray()); |
|
|
|
// http.anonymous().authenticationFilter(availableUserAuthenticationFilter); |
|
|
|
} |
|
|
|
|
|
|
|
private AuthenticationEntryPoint authenticationEntryPoint() { |
|
|
|