728x90

로그인 후 그 객체의 사용법을 정리해보자.

1. Bean을 통해 가져오기

SecurityContextHolder를 통해 가져온다.

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
UserDetails userDetails = (UserDetails)principal; 
String username = principal.getUsername(); 
String password = principal.getPassword();

 

2. Controller에서 사용자 정보 얻기

Principal 객체에 접근해서 정보를 가져온다.

    @GetMapping("/list")
    public String test(Model model, Principal principal){
        String loginUsername = principal.getName();        
        model.addAttribute("loginUsername", loginUsername);
        
        return "board/list";
    }

 

-

 

출처 : https://djunnni.gitbook.io/springboot/2019-11-30

 

추가 : 로그인 초기화 (회원탈퇴 등에 사용)

SecurityContextHolder.clearContext();

+ Recent posts