Exception or 에러

ClassCastException: class org.springframework.security.core.userdetails.User cannot be cast to class

jaewoo 2023. 2. 23. 15:50

JPA AuditingAware를 통해 CreatedBy를 사용하려 했다.

 

MemberDTO dto = (MemberDTO) authentication.getPrincipal();

return Optional.of(dto.getEmail());

SecurityContext에서 값을 뽑아서 인증확인하고 하려했는데 계속 형변환 에러가 나서 찾아봤는데 여기서 Principal이 String을 넘어와서 그런거 같다.

그래서 값을 그냥 authentication.getName()으로 변경함

 


        return Optional.of(authentication.getName());

 

https://stackoverflow.com/questions/32276482/java-lang-classcastexception-org-springframework-security-core-userdetails-user

 

java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to model.User

I am using Spring Security in my application. I need loggedIn user details in the controllers of my application. For that I am using this code User loggedInUser = (User)SecurityContextHolder.

stackoverflow.com