Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YS-131] refact: WebSecurityConfig 우선순위 수정 및 memberId 삽입 위치 변경 #25

Merged
merged 7 commits into from
Jan 11, 2025
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSecurityConfig에서 먼저 인증 생략해야 하는 URI를 적용하고 나머지 URI에 대해 인증을 수행하도록 우선순위를 수정했습니다. 테스트 했을 때, 정상적으로 동작했습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이메일 인증 로직까지 헤더에 토큰 필요하지 않은 부분 작업 깔끔하게 잘해주신 것 같아요 💪💪

Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,64 @@ import org.springframework.web.servlet.HandlerExceptionResolver

@Configuration
@EnableMethodSecurity
class WebSecurityConfig(
) {
class WebSecurityConfig {
@Bean
@Order(0)
fun securityFilterChain(
@Order(1)
fun authSecurityFilterChain(
httpSecurity: HttpSecurity,
jwtTokenProvider: JwtTokenProvider,
handlerExceptionResolver: HandlerExceptionResolver,
handlerExceptionResolver: HandlerExceptionResolver
): SecurityFilterChain = httpSecurity
.securityMatcher( "/v1/**")
.securityMatcher("/v1/auth/**", "/v1/members/signup", "/v1/emails/**")
.csrf { it.disable() }
.cors(Customizer.withDefaults())
.sessionManagement {
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
}
.authorizeHttpRequests {
it.anyRequest().permitAll() // 모든 요청 허용
}
.addFilterBefore(
JwtAuthenticationFilter(jwtTokenProvider, handlerExceptionResolver),
UsernamePasswordAuthenticationFilter::class.java
)
.exceptionHandling {
it.accessDeniedHandler { request, response, exception ->
handlerExceptionResolver.resolveException(request, response, null, PermissionDeniedException())
}.authenticationEntryPoint { request, response, authException ->
handlerExceptionResolver.resolveException(request, response, null, UnauthorizedException())
}
it.requestMatchers("/v1/auth/**").permitAll()
it.requestMatchers("/v1/members/signup", "/v1/emails/**").permitAll()
it.anyRequest().authenticated()
}
.build()

@Bean
@Order(1)
fun authSecurityFilterChain(
@Order(2)
fun securityFilterChain(
httpSecurity: HttpSecurity,
jwtTokenProvider: JwtTokenProvider,
handlerExceptionResolver: HandlerExceptionResolver
handlerExceptionResolver: HandlerExceptionResolver,
): SecurityFilterChain = httpSecurity
.securityMatcher("/v1/auth/**")
.securityMatcher("/v1/**")
.csrf { it.disable() }
.cors(Customizer.withDefaults())
.sessionManagement {
it.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
}
.authorizeHttpRequests {
println("[DEBUG] authSecurityFilterChain triggered")
it.requestMatchers("/v1/auth/**").permitAll()
it.anyRequest().authenticated()
}
.addFilterBefore(
JwtAuthenticationFilter(jwtTokenProvider, handlerExceptionResolver),
UsernamePasswordAuthenticationFilter::class.java
)
.exceptionHandling {
it.accessDeniedHandler { request, response, exception ->
handlerExceptionResolver.resolveException(request, response, null, PermissionDeniedException())
}.authenticationEntryPoint { request, response, authException ->
handlerExceptionResolver.resolveException(request, response, null, UnauthorizedException())
}
}
.build()

@Bean
@Order(2)
@Order(3)
fun swaggerSecurityFilterChain(httpSecurity: HttpSecurity): SecurityFilterChain = httpSecurity
.securityMatcher("/swagger-ui/**", "/v3/api-docs/**")
.csrf { it.disable() }
.cors(Customizer.withDefaults())
.authorizeHttpRequests {
it.requestMatchers(
"/swagger-ui/**",
"/v3/api-docs/**"
).permitAll()
it.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
}
.build()

}