Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created November 30, 2016 04:55
Show Gist options
  • Select an option

  • Save monkey-codes/2f0ba4145ff0b436bf25e30b239932ca to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/2f0ba4145ff0b436bf25e30b239932ca to your computer and use it in GitHub Desktop.
Configuring Spring Security with OAuth and JWT
@Configuration
@EnableAuthorizationServer
static class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {
@Autowired
@Qualifier('authenticationManagerBean')
AuthenticationManager authenticationManager
@Override
void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient('web-app')
.scopes('read')
.autoApprove(true)
.accessTokenValiditySeconds(600)
.refreshTokenValiditySeconds(600)
.authorizedGrantTypes('implicit', 'refresh_token', 'password', 'authorization_code')
}
@Override
void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).tokenEnhancer(jwtTokenEnhancer()).authenticationManager(authenticationManager)
}
@Bean
TokenStore tokenStore() {
new JwtTokenStore(jwtTokenEnhancer())
}
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(
new ClassPathResource('jwt.jks'), 'mySecretKey'.toCharArray())
JwtAccessTokenConverter converter = new JwtAccessTokenConverter()
converter.setKeyPair(keyStoreKeyFactory.getKeyPair('jwt'))
converter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment