Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Principal vs. @AuthenticationPrincipal #97

Closed
yoon-youngjin opened this issue May 10, 2022 · 0 comments
Closed

Principal vs. @AuthenticationPrincipal #97

yoon-youngjin opened this issue May 10, 2022 · 0 comments

Comments

@yoon-youngjin
Copy link
Collaborator

Principal vs. @AuthenticationPrincipal

Principal

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

@GetMapping("/")
public String index(Model model,Principal principal){
        ...
        }

위의 Principal 객체는 Spring Security가 제공하는 객체가 아닌, 자바에 정의되어있는 Principal 객체를 바인딩 해주는 것이라 사용할 수 있는 메소드가 getName() 밖에 없다.

@AuthenticationPrincipal

@PostMapping("/new")
private String itemNew(
@Valid @ModelAttribute("insertItemDto") InsertItemDto dto,
        BindingResult bindingResult,
@AuthenticationPrincipal UserDetailsImpl userDetails,
        RedirectAttributes redirectAttributes
        ){
        ...
        }

실제 SecurityContextHolder 내 Principal(SecurityContextHolder.getContext().getAuthentication().getPrincipal())를 가져오기 위해서 @AuthenticationPrincipal를 사용한다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant