네! @RequestMapping
이 Spring Security와 충돌할 가능성이 있어요! 😱
Spring Security는 URL 패턴을 순서대로 매칭하는데, @RequestMapping이 URL 구조를 바꾸면서 Security 규칙과 안 맞을 수 있어요
// Security가 기대하는 URL
/api/v1/my-shops/{shopId}/customers
// @RequestMapping으로 실제 생성된 URL이 다를 수 있음
/customers (절대경로로 인식되는 경우)
Spring Security의 request-matcher 전략(ant, mvc, regex)에 따라 @RequestMapping과 다르게 해석될 수 있어요
@RestController
// @RequestMapping("/customers") // 제거
public class VisitorsController {
@GetMapping("/customers") // 직접 명시
@PostMapping("/customers")
// ...
}
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, "/api/v1/my-shops/*/customers/**").permitAll()
.requestMatchers(HttpMethod.POST, "/api/v1/my-shops/*/customers/**").permitAll()
// 더 구체적으로 지정
)
.requestMatchers(mvc()).mvcMatcher("/api/v1/my-shops/**") // MVC 매처 사용
);
}
}
클래스 레벨 @RequestMapping
이 Security 필터 체인과 경로 해석을 다르게 해서 403이 발생한 것 같아요!
이런 경우 직접 매핑하는 게 더 안전하고 예측 가능해요. 👍\
Spring Boot 개발을 하다 보면 코드 중복을 줄이기 위해 클래스 레벨에서 @RequestMapping
을 사용하고 싶은 유혹을 받는다. 하지만 이는 예상치 못한 문제를 일으킬 수 있다. 특히 Spring Security와 함께 사용할 때 더욱 그렇다.
다음과 같은 상황을 가정해보자: