Extract Currently Logged in User information from JWT token using Spring Security

后端 未结 2 1906
谎友^
谎友^ 2021-02-04 07:34

I have implemented JWT and LDAP Authentication using Spring Security Oauth2. It seems to be working fine and I can login with my LDAP credentials.

Now, there is one req

2条回答
  •  死守一世寂寞
    2021-02-04 08:21

    In your REST Service, add the OAuth2Authentication Class as an argument

    @RequestMapping(value = "/{id}/products", method = RequestMethod.POST)
        public ResourceResponse processProducts(OAuth2Authentication auth) {
    

    Springboot will automatically map the logged-in user details to this object. Now, you can do the following to access the username

    auth.getPrincipal().toString()
    

提交回复
热议问题