Get UserDetails object from Security Context in Spring MVC controller

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

    You can use below code to find out principal (user email who logged in)

      org.opensaml.saml2.core.impl.NameIDImpl principal =  
      (NameIDImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
      String email = principal.getValue();
    

    This code is written on top of SAML.

提交回复
热议问题