Spring 未调用安全客户令牌增强器
Spring security customer token enhancer not invoked
我使用下面给出的 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 生成令牌的方法时,它不会进入增强方法。
如果我遗漏了什么,请提出建议。
我没看到你在任何地方分配令牌增强器。据我记得你需要这样的东西:
@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();
}
我遇到了同样的问题,尽管我执行了以下操作:
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;
}
}
令牌增强器未被调用,因为 table oauth_access_token 中有一个寄存器对应于 [的默认 table =21=] oauth,我把client-id和用户名对应的记录删除就解决了。
您的 CustomTokenEnhancer 未被调用,因为您正在使用 JdbcTokenStore 并且一些访问令牌已缓存在数据库中。
手动删除此tableoauth_access_token中的记录,然后重试。
参考这个问题:https://github.com/spring-projects/spring-security-oauth/issues/1371
我使用下面给出的 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 生成令牌的方法时,它不会进入增强方法。
如果我遗漏了什么,请提出建议。
我没看到你在任何地方分配令牌增强器。据我记得你需要这样的东西:
@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();
}
我遇到了同样的问题,尽管我执行了以下操作:
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;
}
}
令牌增强器未被调用,因为 table oauth_access_token 中有一个寄存器对应于 [的默认 table =21=] oauth,我把client-id和用户名对应的记录删除就解决了。
您的 CustomTokenEnhancer 未被调用,因为您正在使用 JdbcTokenStore 并且一些访问令牌已缓存在数据库中。
手动删除此tableoauth_access_token中的记录,然后重试。
参考这个问题:https://github.com/spring-projects/spring-security-oauth/issues/1371