@Authenticationprincipal

!이 어노테이션은 Package org.springframework.security.core.annotation 에 있는 어노테이션 입니다.

docs.spring.io에서 확인해 보면 이 어노테이션은 Authentication.getPrincipal()메서드 인수를 확인하는 데 사용된다고 하는데 Authentication 인터페이스에 대해서는 다음에 다루고 우선 이 메서드만 확인해 보게 되면

getPrincipal

인증 중인 주체의 ID입니다.

사용자 이름과 비밀번호가 있는 인증 요청의 경우, 이 사용자 이름이 됩니다.

호출자는 인증 요청의 주체를 채워야 합니다.

AuthenticationManager 구현체는 응용 프로그램에서 사용할 수 있는 주체로서 더 풍부한 정보를 포함하는 인증을 반환하는 경우가 많습니다.

많은 인증 공급자가 UserDetails 개체를 주체로 만듭니다.

라고한다. 그럼 이제 @Authenticationprincipal에 대해 한번 확인해보자.

Spring Security를 사용할 때 @AuthenticationPrincipal은 사용자가 인증된 경우 principal를 주입하고

사용자가 인증되지 않은 경우 null을 주입하는데 대부분의 로그인 기능에 UserDetails을 상속받아 커스텀으로 유저 디테일을 만들고 시작하기에 아래 코드에서는 TestUserDetails로 받을 수 있다.

@RestController
public class UserController {

    @GetMapping("/username")
    public String getUserName(@AuthenticationPrincipal TestUserDetails testUserDetails) {
        return testUserDetails.getUsername();
    }
    
}

만약에 @Authenticationprincipal를 사용하지 않는다면 SecurityContextHolder를 사용해 Principal를 가져올 수 있다.

@RestController
public class UserController {

    @GetMapping("/username")
    public String getUserName(@AuthenticationPrincipal TestUserDetails testUserDetails) {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        TestUserDetails testUserDetails = (TestUserDetails) authentication.getPrincipal();
        return testUserDetails.getUsername();
    }
    
}

참조

- 끗 -

image