package com.bruce.sams.config; import com.bruce.sams.filters.JwtAuthFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; /** * Spring Security 配置类 */ @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf(csrf -> csrf.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers("/api/auth/login").permitAll() .requestMatchers("/api/admin/**").hasAuthority("leader") .requestMatchers("/api/user/**").hasAuthority("participant") .requestMatchers("/api/activity/**").authenticated() .anyRequest().authenticated() ) .addFilterBefore(new JwtAuthFilter(), UsernamePasswordAuthenticationFilter.class); return http.build(); } }