• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

CustomAuthenticationProvider 에서 token의 principal 값 설정

21.07.11 16:26 작성 조회수 245

0

CustomAuthenticationProvider 에서 인증 token의 principal 값 설정하시는 코드가 있습니다.

new UsernamePasswordAuthenticationToken(accountContext.getAccount(), null, accountContext.getAuthorities());

여기서는 principal 값으로 Account 엔티티( = accountContext.getAccount) 를 넣는데요,

왜 spring security에서 제공하는 User 클래스를 상속한 

AccountContext가 아닌 Account 엔티티를 주었는지 궁금합니다.

답변 1

답변을 작성해보세요.

3

말씀하신 것 처럼 AccountContext 객체를 넣어도 상관없습니다.

principal 에 저장한 값을 상황에 맞게 핸들링 하시면 됩니다.

다만 강좌에서는 principal 에 wrapper 클래스인 AccountContext 를 넣지 않고 바로 Account 클래스를 참조하도록 했을 뿐입니다.

다만 참고하실 점은 principal 속성은 인증객체 자체를 의미하기 때문에 Wrapper 클래스보다는 Account 객체가 좀 더 의미에 가까울 수 있다고 생각합니다.

그러나 정해진 기준은 없으니 자유롭게 구현하시면 됩니다.