|
@@ -7,6 +7,7 @@ import com.xuekairui.user.security.JwtAuthenticationFilter;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
@@ -17,13 +18,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
-import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
-import org.springframework.web.cors.CorsConfigurationSource;
|
|
|
|
|
-import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
-
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Spring Security 配置
|
|
* Spring Security 配置
|
|
@@ -64,10 +59,13 @@ public class SecurityConfig {
|
|
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
http
|
|
http
|
|
|
.csrf(AbstractHttpConfigurer::disable)
|
|
.csrf(AbstractHttpConfigurer::disable)
|
|
|
- .cors(cors -> cors.configurationSource(corsConfigurationSource()))
|
|
|
|
|
|
|
+ // CORS 由 Nginx 统一处理,后端关闭,避免重复/冲突
|
|
|
|
|
+ .cors(AbstractHttpConfigurer::disable)
|
|
|
.sessionManagement(session ->
|
|
.sessionManagement(session ->
|
|
|
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
.authorizeHttpRequests(auth -> auth
|
|
.authorizeHttpRequests(auth -> auth
|
|
|
|
|
+ // OPTIONS 预检请求直接放行(CORS 由 Nginx 处理,此处仅作兜底避免 401)
|
|
|
|
|
+ .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
|
|
// 超级管理员专属:角色赋权
|
|
// 超级管理员专属:角色赋权
|
|
|
.requestMatchers("/api/admin/users/**").hasRole("SUPER_ADMIN")
|
|
.requestMatchers("/api/admin/users/**").hasRole("SUPER_ADMIN")
|
|
|
// 运营后台接口
|
|
// 运营后台接口
|
|
@@ -106,26 +104,6 @@ public class SecurityConfig {
|
|
|
return http.build();
|
|
return http.build();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Bean
|
|
|
|
|
- public CorsConfigurationSource corsConfigurationSource() {
|
|
|
|
|
- CorsConfiguration configuration = new CorsConfiguration();
|
|
|
|
|
- // allowedOriginPatterns 会回显请求中的 Origin(不会发 *),
|
|
|
|
|
- // 比 allowedOrigins 更安全且与代理服务器兼容
|
|
|
|
|
- configuration.setAllowedOriginPatterns(List.of("*"));
|
|
|
|
|
- configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"));
|
|
|
|
|
- configuration.setAllowedHeaders(List.of("*"));
|
|
|
|
|
- // 允许前端读取的响应头
|
|
|
|
|
- configuration.setExposedHeaders(List.of("Authorization", "X-Request-Id"));
|
|
|
|
|
- // 注意:不设置 allowCredentials,因为前端通过 Authorization 头传递 JWT,
|
|
|
|
|
- // 不依赖 Cookie。如果 Nginx 反向代理添加了 Access-Control-Allow-Origin: *,
|
|
|
|
|
- // allowCredentials=true 会导致浏览器拒绝该组合(CORS 规范禁止)。
|
|
|
|
|
- configuration.setMaxAge(3600L);
|
|
|
|
|
-
|
|
|
|
|
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
|
|
- source.registerCorsConfiguration("/**", configuration);
|
|
|
|
|
- return source;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
public PasswordEncoder passwordEncoder() {
|
|
public PasswordEncoder passwordEncoder() {
|
|
|
return new BCryptPasswordEncoder();
|
|
return new BCryptPasswordEncoder();
|