How to retrive logged in user data in spring mvc a

2019-09-14 13:52发布

问题:

I need to get current loggedin user's details along entertain session.Is there any poissible to do it in Spring and hibernate

I am new to spring and hibernate...Help me guys.

回答1:

There are serveral ways in which you can accomplish this. In a controller you can inject a Pricipal as a parameter.

@RequestMapping()
public void getPricipal(Principal principal) {
   Customer c = (Customer) pricipal;
}

You should also be able to inject the UserDetails directly as well. Spring will resolve it for you. Your Customer class must implement UserDetails

@RequestMapping()
public void getPricipal(Customer customer) {

}

As for having all details, you should be able to access the firstname, lastname etc, if you've implemented your own UserDetailsService where you return your custom UserDetails (Customer).