You could define your own user model, and add all your preferred attributes to it.then Wrap it in your UserDetails
Implementation, for example
public class UserModel {
private username;
private password;
private firstname;
private lastname;
.
.
.
etc
}
public class MyUserDetails implements UserDetails {
private UserModel user;
public UserModel getUser(){
return user;
}
@Override
public String getUsername(){
return user.getUsername();
}
@Override
public String getPassword(){
return user.getPassword();
}
}
And whenever you need to access your UserModel
specific attributes, use the getUser()
method and access your specific fields directly
MyUserDetails myUserDetails = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserModel user = myUserDetails.getUser();
String firstname = user.getFirstname();
The good point of such an implementation is that you separate the security UserDetails
implementation from your own User
implementation which could be for example a managed entity by any ORM framework like Hibernate