wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 1 | package com.example.g8backend.config; |
| 2 | |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame^] | 3 | import com.example.g8backend.filter.JwtAuthenticationFilter; |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 4 | import org.springframework.context.annotation.Bean; |
| 5 | import org.springframework.context.annotation.Configuration; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 6 | import org.springframework.security.authentication.AuthenticationManager; |
| 7 | import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; |
| 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame^] | 9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 10 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 12 | import org.springframework.security.web.SecurityFilterChain; |
| 13 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 14 | |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 15 | @Configuration |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 16 | @EnableWebSecurity |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 17 | public class SecurityConfig { |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 18 | private final JwtAuthenticationFilter jwtAuthenticationFilter; |
| 19 | |
| 20 | public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter) { |
| 21 | this.jwtAuthenticationFilter = jwtAuthenticationFilter; |
| 22 | } |
| 23 | |
| 24 | @Bean |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame^] | 25 | public BCryptPasswordEncoder passwordEncoder() { |
| 26 | return new BCryptPasswordEncoder(); |
| 27 | } |
| 28 | |
| 29 | @Bean |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 30 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 31 | return http |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame^] | 32 | .csrf(AbstractHttpConfigurer::disable) |
| 33 | .authorizeHttpRequests(auth -> auth |
| 34 | // 管理员接口需ADMIN角色 |
| 35 | .requestMatchers("/admin/**").hasRole("ADMIN") |
| 36 | // 用户签到接口需认证 |
| 37 | .requestMatchers("/user/signin").authenticated() |
| 38 | // 其他请求允许匿名访问(感觉这里应该还需要做修改,暂时先放着) |
| 39 | .anyRequest().permitAll() |
| 40 | ) |
| 41 | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) |
| 42 | .build(); |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Bean |
| 46 | public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { |
| 47 | return config.getAuthenticationManager(); |
| 48 | } |
夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame^] | 49 | } |