Skip to content

Instantly share code, notes, and snippets.

@jackdpeterson
Created July 18, 2019 21:29
Show Gist options
  • Save jackdpeterson/d0ed05bdb076cd7f0d4e4e3bc65a741a to your computer and use it in GitHub Desktop.
Save jackdpeterson/d0ed05bdb076cd7f0d4e4e3bc65a741a to your computer and use it in GitHub Desktop.
package com.example.identity.config;
import com.example.identity.model.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 java.util.HashMap;
import java.util.Map;
public class JwtTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
Map<String, Object> additionalInfo = new HashMap<>();
User user = (User) authentication.getUserAuthentication().getPrincipal();
Integer userId = user.getId();
// Resource Server always throws a 401, cannot validate the token error if this is set here.
additionalInfo.put("sub", userId);
// works if it's this is set.
additionalInfo.put("subject", userId);
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment