Get UserDetails object from Security Context in Spring MVC controller

前端 未结 6 444
栀梦
栀梦 2020-12-04 06:26

I\'m using Spring Security 3 and Spring MVC 3.05.

I would like to print username of currently logged in user,how can I fetch UserDetails in my Controller?

         


        
6条回答
  •  借酒劲吻你
    2020-12-04 07:01

    Let Spring 3 injection take care of this.

    Thanks to tsunade21 the easiest way is:

     @RequestMapping(method = RequestMethod.GET)   
     public ModelAndView anyMethodNameGoesHere(Principal principal) {
            final String loggedInUserName = principal.getName();
    
     }
    

提交回复
热议问题