小编aks*_*dce的帖子

如何在Spring Security中配置Resource Server,以便在JWT令牌中使用其他信息

我有一个oauth2 jwt令牌服务器,配置为设置有关用户权限的其他信息.

@Configuration
@Component
public class CustomTokenEnhancer extends JwtAccessTokenConverter {

    CustomTokenEnhancer(){
        super();
    }

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        // TODO Auto-generated method stub
        MyUserDetails user = (MyUserDetails) authentication.getPrincipal();
        final Map<String, Object> additionalInfo = new HashMap<>();
        @SuppressWarnings("unchecked")
        List<GrantedAuthority> authorities= (List<GrantedAuthority>) user.getAuthorities();
        additionalInfo.put("authorities", authorities);

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

        return accessToken;
    }

}
Run Code Online (Sandbox Code Playgroud)

我不确定如何配置我的资源服务器以提取由oauth2服务器设置的用户权限,并使用该权限用于Spring Security框架中的@Secured带注释的控制器.

我的Auth服务器配置如下所示:

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Value("${config.oauth2.privateKey}")
    private String privateKey;

    @Value("${config.oauth2.publicKey}")
    private String publicKey;

    @Value("{config.clienturl}")
    private String clientUrl;

    @Autowired
    AuthenticationManager authenticationManager;

    @Bean
    public JwtAccessTokenConverter …
Run Code Online (Sandbox Code Playgroud)

spring-security jwt spring-security-oauth2

11
推荐指数
1
解决办法
2090
查看次数