ysk(0soo)
Lifealong
ysk(0soo)
전체 방문자
오늘
어제
  • 분류 전체보기 (238)
    • Java (50)
      • whiteship-java-study (11)
      • Java (28)
      • time (6)
    • Spring (68)
      • JPA (15)
      • Spring (1)
      • SpringBoot (1)
      • SpringMVC (6)
      • Spring Security (22)
      • Jdbc (1)
      • RestDocs (14)
      • log (6)
    • Kotlin (3)
    • Web (2)
      • nginx (1)
    • Database (14)
      • MySQL (5)
      • PostgreSQL (1)
      • SQL (1)
      • Redis (4)
    • C, C++ (0)
    • Git (1)
    • Docker (2)
    • Cloud (3)
      • AWS (3)
    • 도서, 강의 (0)
      • t5 (0)
    • 기타 (7)
      • 프로그래밍 (1)
    • 끄적끄적 (0)
    • CS (14)
      • 운영체제(OS) (2)
      • 자료구조(Data Structure) (9)
    • 하루한개 (12)
      • 우아한 테크코스-10분테코톡 (12)
    • 스터디 (12)
      • 클린 아키텍처- 로버트마틴 (2)
      • JPA 프로그래밍 스터디 (10)
    • 테스트 (34)
      • JUnit (19)
      • nGrinder (2)
      • JMeter (0)
    • Infra (3)
    • 프로그래머스 백엔드 데브코스 3기 (0)
    • 디자인 패턴 (3)
    • Issue (4)
    • system (1)
      • grafana (0)
      • Prometheus (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • github

공지사항

인기 글

태그

  • 가상 스레드 예외 핸들링
  • 동시성 제어
  • 정규표현식
  • node exporter basic auth
  • jpa
  • AccessDecisionVoter 커스텀
  • nGrinder
  • LocalDateTime
  • scope value
  • VirtualThread Springboot
  • 인가(Authorization) 처리
  • restdocs custom
  • 가상 스레드
  • AccessDecisionManager
  • 트랜잭션
  • 구조화된 동시성
  • java
  • 동일성
  • UserDetailsService
  • nginx basic auth
  • restdocs enum
  • tree
  • StructuredConcorrency
  • querydsl
  • AuthenticationException
  • 동등성
  • FilterSecurityInterceptor
  • mysql
  • DataJpaTest
  • junit5

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ysk(0soo)

Lifealong

Issue

AuthenticationPrincipal Null - 주입받지 못하는 이슈

2022. 11. 24. 20:44

AuthenticationPrincipal Null 이슈 - 주입받지 못함

결론부터

  • AuthenticationPrincipalArgumentResolver 가 빈으로 등록되지 않음. 빈으로 등록해줘야함
  • @EnableWebSecurity는 설정했고, @EnableWebMvc는 등록안했다가 등록하니 해결됌

spring boot version : 2.7.4

implementation("org.springframework.boot:spring-boot-starter-security:2.7.4")

스프링 시큐리티를 사용하였을 때, 사용자가 인증된다면 Controller 에서 파라미터로 @AuthenticationPrincipal 을 주입 받을 수 있다.

@AuthenticationPrincipal

로그인한 사용자의 정보를 파라메터로 받고 싶을때 기존에는 다음과 같이 Principal 객체로 받아서 사용한다.

  • 하지만 이 객체는 SecurityContextHolder의 Principal과는 다른 객체이다.

스프링 시큐리티로 인증관련 부분을 개발하다가 @AuthenticationPrincipal을 사용했는데, 값이 주입 안되어서 고생을 좀 했었다.

  • UserDetails 정상 구현.
  • @EnableWebSecurity 정상 설정
  • 의존성 문제 없음
  • org.springframework.security.core.userdetails. UserDetails를 adapter 패턴으로 구현도 해봄,
    • 물론 엔티티에 직접 구현도 해봄
  • org.springframework.security.core.userdetailsUser를 상속 받기도 해봄.
  • SecurityContextHolder.getContext().getAuthentication() null 아님. UserDetailsService에서 반환한 값 제대로 주입됌

문제 발생 환경은 다음과 같았다.

  1. WebConfigruation 추가 설정을 해줘야 하는 부분이 있어서 WebConfig 클래스에 WebMvcConfigurationSupport를 상속받음
  2. @EnableMvc 어노테이션을 사용하지 않음
  3. @AuthenticationPrincipal 어노테이션 변수에 주입해주는 행동은 AuthenticationPrincipalArgumentResolver 가 해주는데 아무리 디버깅 해도 걸리지도 않음. 즉 활성화 되지 않았단 소리.
  4. @EnableWebMvc 어노테이션을 붙이니까 해결됌. 자동으로 빈 등록해주어서 받아올 수 있었다.

그래서 왜 그러는가 원인을 분석해 보았는데, 답은찾지 못했고, @EnableWebMvc 어노테이션을 붙이니까 해결되었다.

This issue is because DelegatingFlashMessagesConfiguration from CustomFlashMessagesConfigurer eagerly initializes the handlerExceptionResolver which prevents the AuthenticationPrincipalArgumentResolver from being registered until after Spring MVC has constructed the DispatcherServlets custom argument resolvers.

@EnableWebMvc

EnableWebMvc어노테이션을 사용하면 스프링이 제공하는 웹과 관련된 최신 전략 빈들이 등록된다.

이 어노테이션을 사용 안하면 스프링과 관련된 웹과 관련된 빈들이 등록 안되는 부분이 있다.

아무래도 이부분에 대해서 더 공부 해봐야 겠다.

공부할만한 링크

  • https://ncucu.me/21
  • https://incheol-jung.gitbook.io/docs/q-and-a/spring/enablewebmvc
  • https://goodgid.github.io/Spring-Enable-MVC-Annotation/
  • https://mangkyu.tistory.com/176
  • https://docs.spring.io/spring-security/reference/servlet/integrations/mvc.html#mvc-authentication-principal
  • https://stackoverflow.com/questions/29657039/authenticationprincipal-is-empty-when-using-enablewebsecurity
저작자표시 비영리 (새창열림)

'Issue' 카테고리의 다른 글

동시 삭제 요청 ObjectOptimisticLockingFailureException  (1) 2023.05.11
TransientPropertyValueException: object references an unsaved transient instance 에러  (0) 2023.01.19
Controller + JPA, JUnit Slice Test Error [JPA metamodel must not be empty! 해결]  (0) 2022.12.12
    'Issue' 카테고리의 다른 글
    • 동시 삭제 요청 ObjectOptimisticLockingFailureException
    • TransientPropertyValueException: object references an unsaved transient instance 에러
    • Controller + JPA, JUnit Slice Test Error [JPA metamodel must not be empty! 해결]
    ysk(0soo)
    ysk(0soo)
    백엔드 개발을 좋아합니다. java kotlin spring, infra 에 관심이 많습니다. email : kim206gh@naver.com github : https://github.com/devysk

    티스토리툴바