我正在使用CustomTokenConverter
在令牌中添加其他信息。当我使用JDBCTokenStore
时,我无法在数据库中存储JWT令牌。我需要将令牌存储在数据库中,并且能够作为故障转移方案从应用程序的多个实例访问生成的令牌。
@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
ClientDetailsService clientDetailsService;
@Autowired
private Environment env;
PasswordEncoder secretEncoder = new BCryptPasswordEncoder();
@Override
public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.passwordEncoder(secretEncoder).tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService.getClientDetailsService());
}
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
final TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
endpoints.tokenStore(tokenStore()).tokenEnhancer(tokenEnhancerChain).userDetailsService(userDetailsService)
.authenticationManager(authenticationManager);
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
@Bean
public TokenEnhancer tokenEnhancer() {
return new CustomTokenConverter();
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
CustomTokenConverter converter = new CustomTokenConverter();
final KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
new FileSystemResource(env.getProperty(PJAConstants.P12_FILEPATH)),
(env.getProperty(PJAConstants.P12_PASS)).toCharArray());
converter.setKeyPair(keyStoreKeyFactory.getKeyPair(env.getProperty(PJAConstants.P12_KEY)));
return converter;
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}