can I include user information while issuing an access token?

后端 未结 6 653
無奈伤痛
無奈伤痛 2020-11-28 03:58

I have seen in some oauth2 implementations additional information on the response returned by the authorization server when it issues access tokens. I\'m wondering if there

6条回答
  •  渐次进展
    2020-11-28 04:45

    package com.security;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
    import org.springframework.security.oauth2.common.OAuth2AccessToken;
    import org.springframework.security.oauth2.provider.OAuth2Authentication;
    import org.springframework.security.oauth2.provider.token.TokenEnhancer;
    import org.springframework.stereotype.Component;
    
    @Component
    public class CustomTokenEnhancer implements TokenEnhancer {
    
    	@Override
    	public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,
    			OAuth2Authentication authentication) {
    		// TODO Auto-generated method stub
    		User user = (User) authentication.getPrincipal();
            final Map additionalInfo = new HashMap<>();
    
            additionalInfo.put("customInfo", "some_stuff_here");
            additionalInfo.put("authorities", user.getAuthorities());
    
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    
            return accessToken;
    	}
    
    }

    Following is the xml configuration:

    
    
    
    
      
      
      
      
      
      
    

    That's how I was able to add extra information to the Token.

提交回复
热议问题