Spring security get User object -
I have implemented user authentication through the Spring security framework and everything works well I can log in and log For example, for example, I can log in for example:
string user name = (auth.getPrincipal) ()). GetUsername ();
Now I have to get an object from the database from the database (I need the User ID and other user properties).
How I have tried it yet:
user user = (user) SecurityContextHolder.getContext (). GetAuthentication (). GetPrincipal ();
Then I got an exception:
The request process failed; Nested exception java.lang Class upstation: org.springframework.security.core.userdetails.User can not be entered on net.viralpatel.contact.model.User
Here is a question - how do I user As the object can be obtained, how should I modify my classes UserDetailsServiceImpl and UserAssembler, any ideas?
and another: @Component @Transactional Public Class UserDetailsServiceImpl applies UserDetailsService {@Autowired Personal UserDAO userDAO; @ Owned Personal UserAsseller UserAssamler; Private Static Final Logar Logar = LogfinerGetLoggers (User DeveloperCPCPM Class); @ Transactional (Read Only = True) Public UserDuffload UsernameByUUN Name (String Username) Username Notfound Exceptions, Data EntactionExation {User User = Username FDBEEMM (Username); If (zero == user) new user name notfound exception ("user not found"); Return userasubable.bilduserformuesuser (user); }}
@ service ("enveloper") public class UserAssembler {@Autowired Private UserDAO userDAO; @transcensive (read only = true) public user builderframe user (net virilpatel contact.model.user user) {string roll = "ROLE_USER"; // User AntiTADO Gatorol FormUSAATTE (UserEnty); Collections & lt; GrantedAuthority & gt; Officials = new Arrestists & lt; Valid Authority & gt; (); Rights.add (new licensing authority Impl (role); New users (user.getLogin (), user.getPassword (), true, true, truthful, return the authorities); }}
Basically, you UserDetails
> Access the user .
You have two options:
-
Add your
user
as a field (you can call itorg.springframework .security.core.userdetails.User
):public class UserPrincipal ORG.fr springframework.Ssecurity extends .core.userdetails.User {Personal End User Users ; ...}
and get a
user
from that area:object prize = SecurityContextHolder.getContext (). GetAuthentication () .getPrincipal (); User user = (Principal principal (userpin)) .getUser ();
-
Create a class that implements your
user
and implementsUserDetails
: < / P>public class UserPrincipal uses user UserDetails {... public UserPrincipal (user user) {// copy fields user from}}
This approach You can see the principal
user
directly:user user = (user) SecurityContextHolder.getContext (). GetAuthentication (). GetPrincipal ();
Comments
Post a Comment