未调用Spring安全客户令牌增强器



我已经使用java配置添加了一个自定义令牌增强器,如下所示

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserApprovalHandler userApprovalHandler;
    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource).withClient("abcd").secret("secret")
                .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT").scopes("read", "write", "trust") 
                .accessTokenValiditySeconds(60 * 60 * 24 * 1) 
                .refreshTokenValiditySeconds(60 * 60 * 24 * 30); 
    }
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
        endpoints.tokenStore(tokenStore()).tokenEnhancer(tokenEnhancerChain).userApprovalHandler(userApprovalHandler)
                .authenticationManager(authenticationManager);
    }
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").realm(REALM);
    }
    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }
    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
    }
}

自定义令牌增强程序

下面
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>();
        additionalInfo.put("organization", authentication.getName() + randomAlphabetic(4));
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }
}

我已经在调试上运行了应用程序,并且在CustomTokenEnhancer的增强方法上有一个调试点。现在,当我点击oauth/token方法来生成令牌时,它不会进入enhance方法。

请告诉我是否遗漏了什么

我也遇到了同样的问题,尽管我实现了以下内容:

public class CustomTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    final Map<String, Object> additionalInfo = new HashMap<>();
    additionalInfo.put("organization", authentication.getName() + randomAlphabetic(4));
    ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    return accessToken;
}

}

令牌增强器没有被调用,因为在表oauth_access_token中有一个寄存器对应于spring oauth的默认表,我解决了这个问题,只是删除了与client-id和用户名对应的记录。

您的CustomTokenEnhancer没有被调用,因为您正在使用JdbcTokenStore并且一些访问令牌已经缓存在数据库中。

手动删除本表oauth_access_token中的记录,然后重试。

参考此问题:https://github.com/spring-projects/spring-security-oauth/issues/1371

我没有看到您在任何地方分配令牌增强器。我记得你需要这样的东西:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints
        // some code here
        .tokenEnhancer(tokenEnhancer());
}
@Bean
@Primary
public AuthorizationServerTokenServices tokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    // some code here as well
    tokenServices.setTokenEnhancer(tokenEnhancer());
    return tokenServices;
}
// Beans beans beans
@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}  

之后,您应该让您的令牌增强器参与进来。

假设您的客户增强器是CustomTokenEnhancer

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints){
            TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
            enhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter));
            endpoints.tokenStore(tokenStore)
                    .accessTokenConverter(accessTokenConverter)
                    .tokenEnhancer(enhancerChain)
                    .authenticationManager(authenticationManager).tokenGranter(tokenGranter(endpoints));
        }
        @Bean
        public TokenEnhancer tokenEnhancer() {
            return new CustomTokenEnhancer();
        }

最新更新