Advertisement
andersonalmada2

Untitled

Aug 5th, 2022
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package br.ufc.mandacaru5.config.security;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Qualifier;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.security.authentication.AuthenticationManager;
  11. import org.springframework.security.crypto.password.PasswordEncoder;
  12. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  13. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  14. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  15. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  16. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  17. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  18. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  19. import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
  20. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  21.  
  22. @Configuration
  23. public class OAuthConfiguration {
  24.  
  25.     @Value("${security.oauth2.client.scope}")
  26.     private String scope;
  27.     @Value("${security.oauth2.client.client-id}")
  28.     private String client;
  29.     @Value("${security.oauth2.client.client-secret}")
  30.     private String secret;
  31.     @Value("${security.oauth2.client.grant-type}")
  32.     private String grantType;
  33.  
  34.     private static final String RESOURCE_ID = "mandacaru";
  35.  
  36.     @Configuration
  37.     @EnableResourceServer
  38.     protected class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  39.  
  40.         @Override
  41.         public void configure(ResourceServerSecurityConfigurer resources) {
  42.             resources.resourceId(RESOURCE_ID);
  43.         }
  44.     }
  45.  
  46.     @Configuration
  47.     @EnableAuthorizationServer
  48.     protected class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  49.  
  50.         @Autowired
  51.         @Qualifier("authenticationManagerBean")
  52.         private AuthenticationManager authenticationManager;
  53.  
  54.         @Autowired
  55.         private UserDetailsServiceImpl userDetailsService;
  56.  
  57.         @Autowired
  58.         private PasswordEncoder passwordEncoder;
  59.  
  60.         @Override
  61.         public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  62.             TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
  63.             tokenEnhancerChain.setTokenEnhancers(List.of(new CustomTokenEnhancer(), accessTokenConverter()));
  64.             endpoints.
  65.                 accessTokenConverter(accessTokenConverter()).
  66.                 tokenEnhancer(tokenEnhancerChain).
  67.                 authenticationManager(this.authenticationManager)
  68.                 .userDetailsService(userDetailsService);
  69.         }
  70.  
  71.         @Override
  72.         public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  73.             clients.inMemory().withClient(client).authorizedGrantTypes(grantType).scopes(scope).resourceIds(RESOURCE_ID)
  74.                     .secret(passwordEncoder.encode(secret)).accessTokenValiditySeconds(50000);
  75.         }
  76.        
  77.         @Bean
  78.         public JwtAccessTokenConverter accessTokenConverter() {
  79.             JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  80.             converter.setSigningKey(secret);
  81.             return converter;
  82.         }
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement