Get UserDetails object from Security Context in Spring MVC controller

前端 未结 6 440
栀梦
栀梦 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:12

    That's another solution (Spring Security 3):

    public String getLoggedUser() throws Exception {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return (!name.equals("anonymousUser")) ? name : null;
    }
    

提交回复
热议问题