spring security oauth2 TokenEnhancer加强token不生效

问题

在AuthorizationServerConfigurerAdapter.configure(AuthorizationServerEndpointsConfigurer endpoints)方法中配置了
endpoints.tokenEnhancer();但是没有生效。
要加强token是因为原始token信息带的太少,需要附加一些额外信息给调用方

解决

看了一下config里面,配置的tokenEnhancer实际上是给TokenService用的。
security默认生成一个DefaultTokenService,这里set了TokenEnhancer,但是这个系统里我是自定义的TokenService,因为要配置一些失效时间,所以默认没用了,这个endopoints配置也就没用了,到TokenService里自行配置即可

这是配置类,省略了不相关的代码

@Configuration
@AllArgsConstructor
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		TokenEnhancer customTokenEnhancer =  XXX;
		endpoints
				.tokenServices(tokenService)
				.tokenEnhancer(customTokenEnhancer );
	}
}

点进父类看一下,这个tokenEnhancer()实际在下面代码被调用了,也就是他是tokenService的一部分

private DefaultTokenServices createDefaultTokenServices() {
	DefaultTokenServices tokenServices = new DefaultTokenServices();
	tokenServices.setTokenStore(tokenStore());
	tokenServices.setSupportRefreshToken(true);
	tokenServices.setReuseRefreshToken(reuseRefreshToken);
	tokenServices.setClientDetailsService(clientDetailsService());
	tokenServices.setTokenEnhancer(tokenEnhancer());
	addUserDetailsService(tokenServices, this.userDetailsService);
	return tokenServices;
}

在其他方法中可以看到,如果tokenService被覆盖的话,这个设值是无效的

private AuthorizationServerTokenServices tokenServices() {
	if (tokenServices != null) {
		return tokenServices;
	}
	this.tokenServices = createDefaultTokenServices();
	return tokenServices;
}

因此加强token设置了之后没有生效。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security OAuth2 Authorization Server可以通过Token增强器(Token Enhancer)来为授权后返回的Access Token添加额外的信息。Token增强器是一个接口,它接收一个Access Token并返回一个增强后的Access Token。在Authorization Server配置类中,可以通过调用tokenEnhancer()方法来设置Token增强器,示例代码如下: ```java @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Autowired private DataSource dataSource; @Bean public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } @Bean public JwtAccessTokenConverter accessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); converter.setSigningKey("123456"); return converter; } @Bean public TokenEnhancer tokenEnhancer() { return new CustomTokenEnhancer(); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsService) .tokenStore(tokenStore()) .accessTokenConverter(accessTokenConverter()) .tokenEnhancer(tokenEnhancer()); } @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") .allowFormAuthenticationForClients(); } } ``` 在上面的代码中,CustomTokenEnhancer是一个自定义的Token增强器,它可以在Access Token中添加额外的信息。示例代码如下: ```java public class CustomTokenEnhancer implements TokenEnhancer { @Override public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) { Map<String, Object> additionalInfo = new HashMap<>(); additionalInfo.put("organization", authentication.getName() + "@test.com"); ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo); return accessToken; } } ``` 在上面的代码中,我们向Access Token中添加了一个名为“organization”的信息,它的值为当前用户的用户名加上@test.com。这种方式可以为Access Token添加任何我们需要的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值