Spring Boot 2.4.0 java 1.8 thymeleaf 3.0.11 Spring Security 2.4.0
I want to get the information of the class that inherits USERDETAILS of the logged-in user.
It took almost a whole day. I don't think I understand Spring Boot properly, but I've looked at various sites and tried it, and it worked, so I'll keep a record of it.
Tenants class
Staff class
The loadUserByUsername method of the MyUserDetailsService class separates the returned classes.
MyUserDetailsService class
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//Throw an exception if the argument is null
if (Objects.isNull(username)) {
throw new UsernameNotFoundException("username is empty");
}
Tenants tenants = tenantRepository.findByAdminId(username);
Staff staff = staffRepository.findByEmail(username);
//Throw an exception if null is returned
if(tenants != null) {
return tenants;
} else if(staff != null) {
return staff;
} else {
throw new UsernameNotFoundException("Not found username: " + username);
}
}
I want to display which class is currently logged in with the controller that displays the top screen after login.
As a result of various trials, it is now possible to get which class is authenticated with the implementation below.
@GetMapping("/top")
public ModelAndView userTop(eModelAndView mv, Principal principal) {
//Get a class that inherits USERDETAILS of the logged-in user
Authentication auth = (Authentication) principal;
String authClass = auth.getPrincipal().getClass().getSimpleName();
if(authClass.equals("Tenants")) {
System.out.println("It's a tenant class! !!");
String tenantId = ((Tenants)auth.getPrincipal()).getId().toString();
httpServletResponse.addCookie(new Cookie("tenantId", tenantId));
}
if(authClass.equals("Staff")) {
System.out.println("Staff class! !!");
String tenantId = ((Staff)auth.getPrincipal()).getTenantId().toString();
httpServletResponse.addCookie(new Cookie("tenantId", tenantId));
}
}
I don't know how to use Principal or Authentication, ・ How to pass arguments ・ How to use @AuthenticationPrincipal annotation ・ Do you need a cast? ・ I have a Tenants class in the Principal, but I can't retrieve the field? -GetClass () appears in the code assistant, but I get an error and can't execute it? etc. .. ..
Apparently, Principal contains all the records of the class that inherited UserDetails when the authentication was successful.
However, only username can be retrieved. .. ..
I just thought I should use Principal, but apparently it wasn't.
It was an image that the Principal class was included in the Authentication class and the UserDetails class was included in the Principal. (I don't know if it fits)
Just with that image
//First get Authentication from the Principal passed to the controller argument
Authentication auth = (Authentication) principal;
String authClass = auth.getPrincipal() //Get Principal
.getClass() //Get class
.getSimpleName(); //Exclude package name
This flow worked.
the first
Do I need to cast to Principal → Authentication
? ?? I felt like, but it didn't work unless I did this.
<div sec:authentication="principal"></div>
<div th:text="${#authentication.principal}"></div>
The same content is displayed for both. It's much easier to get it with a controller.
And you can see that the Principal contains User Details.
<div sec: authentication =" principal "> </div>
<div th: text =" $ {# authentication.principal} "> </div>
//controller
//Pass Principal principal as an argument
public ModelAndView userTop(eModelAndView mv, Principal principal) {
//controller
//Pass Principal principal as an argument
Authentication auth = (Authentication) principal;
//Get Principal from Authentication
String authClass = auth.getPrincipal()
.getClass()
.getSimpleName();
Recommended Posts