wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 1 | package com.example.g8backend.config; |
| 2 | |
| 3 | import org.springframework.context.annotation.Bean; |
| 4 | import org.springframework.context.annotation.Configuration; |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame^] | 5 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 6 | import com.example.g8backend.filter.JwtAuthenticationFilter; |
| 7 | import org.springframework.security.authentication.AuthenticationManager; |
| 8 | import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; |
| 9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 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 | |
| 15 | |
| 16 | @Configuration |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame^] | 17 | @EnableWebSecurity |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 18 | public class SecurityConfig { |
| 19 | @Bean |
| 20 | public BCryptPasswordEncoder passwordEncoder() { |
| 21 | return new BCryptPasswordEncoder(); |
| 22 | } |
wuchimedes | 223bfab | 2025-04-04 17:16:05 +0800 | [diff] [blame^] | 23 | |
| 24 | private final JwtAuthenticationFilter jwtAuthenticationFilter; |
| 25 | |
| 26 | public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter) { |
| 27 | this.jwtAuthenticationFilter = jwtAuthenticationFilter; |
| 28 | } |
| 29 | |
| 30 | @Bean |
| 31 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 32 | return http |
| 33 | .csrf(AbstractHttpConfigurer::disable) |
| 34 | .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) |
| 35 | .build(); |
| 36 | } |
| 37 | |
| 38 | @Bean |
| 39 | public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { |
| 40 | return config.getAuthenticationManager(); |
| 41 | } |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 42 | } |