-
-
Save monkey-codes/2f0ba4145ff0b436bf25e30b239932ca to your computer and use it in GitHub Desktop.
Configuring Spring Security with OAuth and JWT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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