Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. @Configuration
  2. @EnableAuthorizationServer
  3. @EnableGlobalMethodSecurity(prePostEnabled = true)
  4. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  5.     @Autowired
  6.     private DataSource dataSource;
  7.  
  8.     @Autowired
  9.     private AuthenticationManager authenticationManager;
  10.  
  11.     @Autowired
  12.     private UserService userService;
  13.  
  14.     @Autowired
  15.     private PasswordEncoder passwordEncoder;
  16.  
  17.     @Bean
  18.     public TokenStore tokenStore() {
  19.         return new JdbcTokenStore(dataSource);
  20.     }
  21.  
  22.     @Bean
  23.     public OAuth2AccessDeniedHandler oauthAccessDeniedHandler() {
  24.         return new OAuth2AccessDeniedHandler();
  25.     }
  26.  
  27.     @Override
  28.     public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
  29.         oauthServer.allowFormAuthenticationForClients()
  30.                 .tokenKeyAccess("permitAll()")
  31.                 .checkTokenAccess("isAuthenticated()")
  32.                 .passwordEncoder(passwordEncoder);
  33.     }
  34.     @Override
  35.     public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  36.         clients.jdbc(dataSource);
  37.     }
  38.  
  39.     @Override
  40.     public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
  41.         endpoints.tokenStore(tokenStore())
  42.                 .authenticationManager(authenticationManager)
  43.                 .userDetailsService(userService);
  44.     }
  45. }
  46.  
  47. @Configuration
  48. @EnableResourceServer
  49. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  50.     @Override
  51.     public void configure(HttpSecurity http) throws Exception {
  52.         http.csrf().disable().cors().and().authorizeRequests().anyRequest().permitAll();
  53.     }
  54. }
  55.  
  56. @Configuration
  57. @EnableWebSecurity
  58. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  59.     @Autowired
  60.     private UserService userService;
  61.  
  62.     @Bean
  63.     public PasswordEncoder passwordEncoder() {
  64.         return new BCryptPasswordEncoder();
  65.     }
  66.  
  67.     @Override
  68.     @Bean
  69.     public AuthenticationManager authenticationManagerBean() throws Exception {
  70.         return super.authenticationManagerBean();
  71.     }
  72.     @Override
  73.     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  74.         auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
  75.     }
  76.  
  77.     @Bean
  78.     public WebMvcConfigurer webMvcConfigurer() {
  79.         return new WebMvcConfigurer() {
  80.             @Override
  81.             public void addCorsMappings(CorsRegistry registry) {
  82.                 registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
  83.                         .allowedHeaders("*");
  84.             }
  85.         };
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement