|
3 | 3 | import lombok.RequiredArgsConstructor; |
4 | 4 | import org.springframework.context.annotation.Bean; |
5 | 5 | import org.springframework.context.annotation.Configuration; |
| 6 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
6 | 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
7 | 8 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
8 | 9 | import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer; |
9 | 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
10 | 11 | import org.springframework.security.crypto.password.PasswordEncoder; |
11 | 12 | import org.springframework.security.web.SecurityFilterChain; |
| 13 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
12 | 14 | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
13 | 15 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
14 | 16 |
|
15 | 17 | @Configuration |
16 | 18 | @RequiredArgsConstructor |
| 19 | +@EnableMethodSecurity |
17 | 20 | public class SecurityConfig { |
| 21 | + private final JwtAuthenticationFilter jwtAuthenticationFilter; |
| 22 | + private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; |
| 23 | + private final JwtAccessDeniedHandler jwtAccessDeniedHandler; |
| 24 | + |
18 | 25 | @Bean |
19 | 26 | public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
20 | | - http.authorizeHttpRequests( |
21 | | - auth -> auth |
22 | | - .requestMatchers("/**").permitAll() |
23 | | - .anyRequest().authenticated() |
| 27 | + http |
| 28 | + // 인가 규칙 설정 |
| 29 | + .authorizeHttpRequests( |
| 30 | + auth -> auth |
| 31 | + .requestMatchers("/api/auth/**").permitAll() |
| 32 | + .anyRequest().authenticated() |
| 33 | + ) |
| 34 | + |
| 35 | + // 인증/인가 실패 핸들러 |
| 36 | + .exceptionHandling(exception -> exception |
| 37 | + .authenticationEntryPoint(jwtAuthenticationEntryPoint) // 401 |
| 38 | + .accessDeniedHandler(jwtAccessDeniedHandler) // 403 |
24 | 39 | ) |
| 40 | + |
| 41 | + // JWT 필터 추가 |
| 42 | + .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) |
| 43 | + |
| 44 | + // 기타 설정 |
25 | 45 | .headers( |
26 | 46 | headers -> headers |
27 | 47 | .frameOptions( |
|
0 commit comments