Skip to content

Commit 079ef09

Browse files
committed
Feat: Spring Security 설정
1 parent a9981a9 commit 079ef09

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/main/java/com/back/global/security/JwtAuthenticationFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import lombok.RequiredArgsConstructor;
88
import org.springframework.security.core.Authentication;
99
import org.springframework.security.core.context.SecurityContextHolder;
10+
import org.springframework.stereotype.Component;
1011
import org.springframework.web.filter.OncePerRequestFilter;
1112

1213
import java.io.IOException;
@@ -16,6 +17,7 @@
1617
* - 모든 요청에 대해 JWT 토큰을 검사
1718
* - 토큰이 유효하면 Authentication 객체를 생성하여 SecurityContext에 저장
1819
*/
20+
@Component
1921
@RequiredArgsConstructor
2022
public class JwtAuthenticationFilter extends OncePerRequestFilter {
2123
private final JwtTokenProvider jwtTokenProvider;

src/main/java/com/back/global/security/SecurityConfig.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,45 @@
33
import lombok.RequiredArgsConstructor;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
67
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
78
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
89
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
910
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1011
import org.springframework.security.crypto.password.PasswordEncoder;
1112
import org.springframework.security.web.SecurityFilterChain;
13+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1214
import org.springframework.web.servlet.config.annotation.CorsRegistry;
1315
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1416

1517
@Configuration
1618
@RequiredArgsConstructor
19+
@EnableMethodSecurity
1720
public class SecurityConfig {
21+
private final JwtAuthenticationFilter jwtAuthenticationFilter;
22+
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
23+
private final JwtAccessDeniedHandler jwtAccessDeniedHandler;
24+
1825
@Bean
1926
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
2439
)
40+
41+
// JWT 필터 추가
42+
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
43+
44+
// 기타 설정
2545
.headers(
2646
headers -> headers
2747
.frameOptions(

0 commit comments

Comments
 (0)