|
|
@@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.ProviderManager;
|
|
|
@@ -62,14 +63,53 @@ public class SecurityConfig {
|
|
|
private final SecurityProperties securityProperties;
|
|
|
private final ThirdPartyJwtAuthFilter thirdPartyAuthFilter;
|
|
|
|
|
|
+ // 仅针对第三方URL的安全过滤链,只挂第三方认证过滤器
|
|
|
+ @Bean
|
|
|
+ @Order(1)
|
|
|
+ public SecurityFilterChain thirdPartySecurityFilterChain(HttpSecurity http) throws Exception {
|
|
|
+
|
|
|
+ log.info("第三方认证过滤器: {}", thirdPartyAuthFilter);
|
|
|
+
|
|
|
+ String[] thirdPartyUrls = securityProperties.getThirdPartyUrls();
|
|
|
+ if (thirdPartyUrls == null || thirdPartyUrls.length == 0) {
|
|
|
+ log.warn("第三方URL未配置或为空,使用占位符避免匹配所有请求");
|
|
|
+ thirdPartyUrls = new String[]{"/__thirdparty_noop__"};
|
|
|
+ }
|
|
|
+ log.info("========== 配置第三方 SecurityFilterChain, urls: {} ==========", (Object) thirdPartyUrls);
|
|
|
+
|
|
|
+ http
|
|
|
+ .securityMatcher(thirdPartyUrls) // 只匹配第三方URL
|
|
|
+ .authorizeHttpRequests(registry -> registry
|
|
|
+ .anyRequest().authenticated()
|
|
|
+ )
|
|
|
+ .exceptionHandling(configurer ->
|
|
|
+ configurer
|
|
|
+ .authenticationEntryPoint(new MyAuthenticationEntryPoint())
|
|
|
+ .accessDeniedHandler(new MyAccessDeniedHandler())
|
|
|
+ )
|
|
|
+ .sessionManagement(configurer ->
|
|
|
+ configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
|
+ )
|
|
|
+ .csrf(AbstractHttpConfigurer::disable)
|
|
|
+ .formLogin(AbstractHttpConfigurer::disable)
|
|
|
+ .httpBasic(AbstractHttpConfigurer::disable)
|
|
|
+ .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
|
|
|
+ // 只加第三方认证过滤器
|
|
|
+ .addFilterBefore(thirdPartyAuthFilter, UsernamePasswordAuthenticationFilter.class);
|
|
|
+
|
|
|
+ return http.build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 配置安全过滤链 SecurityFilterChain
|
|
|
*/
|
|
|
@Bean
|
|
|
+ @Order(2)
|
|
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
|
|
|
log.info("========== 配置 SecurityFilterChain ==========");
|
|
|
- log.info("第三方认证过滤器: {}", thirdPartyAuthFilter);
|
|
|
+
|
|
|
|
|
|
return http
|
|
|
.authorizeHttpRequests(requestMatcherRegistry -> {
|
|
|
@@ -103,8 +143,6 @@ public class SecurityConfig {
|
|
|
.addFilterBefore(new CaptchaValidationFilter(redisTemplate, codeGenerator), UsernamePasswordAuthenticationFilter.class)
|
|
|
// 验证和解析过滤器
|
|
|
.addFilterBefore(new TokenAuthenticationFilter(tokenManager), UsernamePasswordAuthenticationFilter.class)
|
|
|
- // 第三方认证过滤器
|
|
|
- .addFilterBefore(thirdPartyAuthFilter, UsernamePasswordAuthenticationFilter.class)
|
|
|
.build();
|
|
|
}
|
|
|
|