spring security를 사용한 oauth 구현 과정에서 발생한 이슈에 관한 정리

Untitled

보통 spring security와 swagger를 동시에 쓸 때 추가하라고 나오는 위와 같은 설정은

swagger v2 버전으로 만약 v3버전을 사용한다면 아래와 같이 설정

Untitled

위 코드를 보면 .userInfoEndpoint().userService(new CustomOAuth2UserService(UserRepository)); 부분이 있다. 이 부분에서 CustomOAuth2UserService 를 생성해서 loadUser 등의 메서드를 처리하는데 이 때

Untitled

이 처럼 @autowired 를 통해 userRepository를 의존성 주입 하면 생성시기의 문제로

아래의 에러가 발생하게 된다.

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception repository null

Untitled

그래서 위와 같이 생성자 주입 방식으로 바꿔주니 nullpointexception이 해결