java - Spring security 客户 token 增强器未调用

标签 java spring spring-security spring-oauth2

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

@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;
    }

}

位于自定义 token 增强器下方

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 方法来生成 token 时,它不会进入增强方法。

如果我遗漏了什么,请提出建议。

最佳答案

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

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;
}

}

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

关于java - Spring security 客户 token 增强器未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018227/

相关文章:

java - WebDataBinder 自定义日期编辑器不起作用

spring - 如何在 Vaadin 应用程序中处理发布请求?

java - 确保 Spring Boot 2 API 的安全

JAVA:分割字符串以获得 double 值

java - 如何为 JTextPane 设置默认背景色

java - 访问osgi bundle 资源

java - Spring 安全: activating csrf disables/logout

java - private final native 与 private native 的优势

java - 如何禁用解析作为 url 参数/从 url 传递的登录参数

java - 如何以编程方式向 Spring Bean 添加安全性