| 33 | import java.util.List; |
| 34 | |
| 35 | @Configuration |
| 36 | @EnableWebSecurity |
| 37 | @EnableMethodSecurity |
| 38 | @RequiredArgsConstructor |
| 39 | public class SecurityConfig { |
| 40 | |
| 41 | private final JwtAuthenticationFilter jwtAuthenticationFilter; |
| 42 | private final UserDetailsService userDetailsService; |
| 43 | |
| 44 | /** |
| 45 | * 白名单路径 - 无需认证 |
| 46 | */ |
| 47 | private static final String[] WHITE_LIST = { |
| 48 | "/api/auth/login", |
| 49 | "/api/auth/register", |
| 50 | "/api/auth/refresh", |
| 51 | "/api/public/**", |
| 52 | "/swagger-ui/**", |
| 53 | "/swagger-ui.html", |
| 54 | "/v3/api-docs/**", |
| 55 | "/doc.html", |
| 56 | "/webjars/**", |
| 57 | "/actuator/**" |
| 58 | }; |
| 59 | |
| 60 | @Bean |
| 61 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 62 | http |
| 63 | // 禁用 CSRF(使用 JWT 无状态认证) |
| 64 | .csrf(AbstractHttpConfigurer::disable) |
| 65 | // 启用 CORS |
| 66 | .cors(cors -> cors.configurationSource(corsConfigurationSource())) |
| 67 | // 配置授权规则 |
| 68 | .authorizeHttpRequests(auth -> auth |
| 69 | // 白名单路径 |
| 70 | .requestMatchers(WHITE_LIST).permitAll() |
| 71 | // 面试官和 HR 均可读取岗位列表(供面试官选择岗位出题) |
| 72 | .requestMatchers(HttpMethod.GET, "/api/hr/positions/**").hasAnyRole("HR", "INTERVIEWER") |
| 73 | // HR 相关接口 |
| 74 | .requestMatchers("/api/hr/**").hasRole("HR") |
| 75 | // 面试官相关接口 |
| 76 | .requestMatchers("/api/interviewer/**").hasRole("INTERVIEWER") |
| 77 | // 其他请求需要认证 |
| 78 | .anyRequest().authenticated() |
| 79 | ) |
| 80 | // 无状态会话管理 |
| 81 | .sessionManagement(session -> session |
| 82 | .sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
| 83 | ) |
| 84 | // 认证提供者 |
| 85 | .authenticationProvider(authenticationProvider()) |
| 86 | // JWT 过滤器 |
| 87 | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); |
| 88 | |
| 89 | return http.build(); |
| 90 | } |
| 91 | |
| 92 | @Bean |
nothing calls this directly
no outgoing calls
no test coverage detected