코딩딩/Error
RestController에서 @AuthenticationPrincipal UserDetails principal 정보가 담기지 않은 문제
전낙타
2024. 1. 12. 15:54
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/my")
public class MemberApiController {
private final MemberService memberService;
@PatchMapping("/hospital")
@ResponseStatus(HttpStatus.OK)
public String appendFavorites(
@AuthenticationPrincipal UserDetails principal,
@RequestParam("hospitalSeq") Long hospitalSeq
) {
log.info("=========================={}===========================", hospitalSeq);
log.info("=========================={}===========================", principal.getUsername());
memberService.changeFavoriteStatus(hospitalSeq, principal.getUsername());
return "Favorites have been successfully changed";
}
}
해당 컨트롤러에서만 AuthenticationPrincipal이 담기지 않아 한참을 뻘짓했는데
알고보니 api 개발 단계에서 편의성을 위해 스프링 시큐리티에 api를 제외하는 설정값을 줬었다.
// 시큐리티 비활성화
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring().
requestMatchers(new AntPathRequestMatcher("/h2-console/**"))
.requestMatchers(new AntPathRequestMatcher("/img/**"))
.requestMatchers(new AntPathRequestMatcher("/css/**"))
.requestMatchers(new AntPathRequestMatcher("/js/**"))
.requestMatchers(new AntPathRequestMatcher("/img/**"))
.requestMatchers(new AntPathRequestMatcher("/lib/**"))
// 개발 단계에서 편의성을 위해 api를 AntPathRequestMatcher에 추가했습니다.
.requestMatchers(new AntPathRequestMatcher("/api/**"));
}
맨 밑줄을 삭제하고 다시 조회하니 멀쩡히 잘 돌아간다
정말 바보같은 실수를 했네