Guest User

Untitled

a guest
Jul 30th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. @Component
  2. public final class CustomFilter extends OncePerRequestFilter implements Filter, InitializingBean {
  3.  
  4. @Override
  5. protected final void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  6. throws ServletException, IOException {
  7.  
  8. response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
  9. response.setHeader("Access-Control-Allow-Credentials", "true");
  10. response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
  11. response.setHeader("Access-Control-Max-Age", "3600");
  12. response.setHeader("Access-Control-Allow-Headers",
  13. "Content-Type, Accept, X-Requested-With, remember-me, Authorization, x-auth-token");
  14.  
  15. if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
  16. response.setStatus(HttpServletResponse.SC_OK);
  17. } else {
  18. filterChain.doFilter(request, response);
  19. }
  20. }
  21. }
  22.  
  23. @Configuration
  24. @EnableResourceServer
  25. @Order(Ordered.HIGHEST_PRECEDENCE)
  26. public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  27.  
  28. @Autowired
  29. private CustomFilter customFilter;
  30.  
  31.  
  32. @Override
  33. public void configure(HttpSecurity http) throws Exception {
  34.  
  35. http
  36. .addFilterBefore(customFilter, BasicAuthenticationFilter.class)
  37. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  38.  
  39. .and().authorizeRequests()
  40.  
  41. .anyRequest().authenticated()
  42.  
  43. .and()
  44. .formLogin()
  45. .loginPage("/login")
  46. .permitAll()
  47. .successHandler(successHandler())
  48. .failureHandler(failHandler)
  49. .and()
  50.  
  51. .logout()
  52. .permitAll()
  53.  
  54. .and().exceptionHandling().authenticationEntryPoint(entryPoint)
  55.  
  56. // .and().cors().configurationSource(this.corsConfigurationSource()) - didn't work.
  57. //
  58.  
  59. .and().csrf()
  60. .disable()
  61. ;
  62. }
  63.  
  64. // @Bean
  65. // public CorsConfigurationSource corsConfigurationSource() { -- didn't work.
  66. // CorsConfiguration configuration = new CorsConfiguration();
  67. // configuration.setAllowedOrigins(Collections.singletonList("*"));
  68. // configuration.setAllowedMethods(Collections.singletonList("*"));
  69. // UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  70. // source.registerCorsConfiguration("/**", configuration);
  71. // return source;
  72. // }
  73.  
  74. @Bean
  75. public FilterRegistrationBean corsFilter() {
  76. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  77. CorsConfiguration config = new CorsConfiguration();
  78. config.setAllowCredentials(true);
  79. config.addAllowedOrigin("*");
  80. config.addAllowedHeader("*");
  81. config.addAllowedMethod("*");
  82. source.registerCorsConfiguration("/**", config);
  83. FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
  84. bean.setOrder(0);
  85. return bean;
  86. }
  87. .......
  88. }
  89.  
  90. POST /v1/oauth/token?grant_type=password&username=testuser&password=testpassword HTTP/1.1
  91. Host: localhost:8080
  92. Connection: keep-alive
  93. Content-Length: 52
  94. Accept: application/json, text/plain, */*
  95. Origin: http://localhost:3000
  96. User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
  97. Content-Type: application/json;charset=UTF-8
  98. Referer: http://localhost:3000/
  99. Accept-Encoding: gzip, deflate, br
  100. Accept-Language: en-EN,en;q=0.9,en-US;q=0.8,en;q=0.7
  101.  
  102. {"timestamp":"2018-07-30T09:07:14.736+0000","status":401,"error":"Unauthorized","message":"Unauthorized","path":"/v1/oauth/token"}
  103.  
  104. GET /v1/user/0ec9f59c-1fac-4604-8a7b-490488cbce33 HTTP/1.1
  105. Host: localhost:8080
  106. Connection: keep-alive
  107. Accept: application/json, text/plain, */*
  108. Origin: http://localhost:3000
  109. User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
  110. Referer: http://localhost:3000/
  111. Accept-Encoding: gzip, deflate, br
  112. Accept-Language: en-EN,en;q=0.9,en-US;q=0.8,en;q=0.7
  113.  
  114. {"timestamp":"2018-07-30T09:07:14.736+0000","status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/v1/users/0ec9f59c-1fac-4604-8a7b-490488cbce33"}
Add Comment
Please, Sign In to add comment