GatewayCrosConfig.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.huimv.gateway.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.web.cors.CorsConfiguration;
  6. import org.springframework.web.cors.reactive.CorsWebFilter;
  7. import org.springframework.web.cors.reactive.DefaultCorsProcessor;
  8. import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
  9. import org.springframework.web.server.ServerWebExchange;
  10. import org.springframework.web.util.pattern.PathPatternParser;
  11. import java.util.Collections;
  12. /**
  13. * @Project : huimv.shiwan
  14. * @Package : com.huimv.gateway.config
  15. * @Description : TODO
  16. * @Author : yuxuexuan
  17. * @Create : 2021/4/28 0028 15:45
  18. **/
  19. @Configuration
  20. public class GatewayCrosConfig {
  21. // @Bean
  22. // public CorsWebFilter corsWebFilter(){
  23. //
  24. // UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  25. // CorsConfiguration corsConfiguration = new CorsConfiguration();
  26. // //1.配置跨域
  27. // corsConfiguration.addAllowedHeader("*");
  28. // corsConfiguration.addAllowedMethod("*");
  29. //// corsConfiguration.addAllowedOrigin("*");
  30. // corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
  31. // corsConfiguration.setAllowCredentials(true);
  32. //
  33. //
  34. // source.registerCorsConfiguration("/**",corsConfiguration);
  35. // return new CorsWebFilter(source);
  36. // }
  37. @Bean
  38. public CorsResponseHeaderFilter corsResponseHeaderFilter() {
  39. return new CorsResponseHeaderFilter();
  40. }
  41. @Bean
  42. public CorsWebFilter corsFilter() {
  43. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
  44. source.registerCorsConfiguration("/**", buildCorsConfiguration());
  45. CorsWebFilter corsWebFilter = new CorsWebFilter(source, new DefaultCorsProcessor() {
  46. @Override
  47. protected boolean handleInternal(ServerWebExchange exchange, CorsConfiguration config,
  48. boolean preFlightRequest)
  49. {
  50. // 预留扩展点
  51. // if (exchange.getRequest().getMethod() == HttpMethod.OPTIONS) {
  52. return super.handleInternal(exchange, config, preFlightRequest);
  53. // }
  54. // return true;
  55. }
  56. });
  57. return corsWebFilter;
  58. }
  59. private CorsConfiguration buildCorsConfiguration() {
  60. CorsConfiguration corsConfiguration = new CorsConfiguration();
  61. corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
  62. //corsConfiguration.addAllowedOrigin("*");
  63. corsConfiguration.addAllowedMethod(HttpMethod.OPTIONS);
  64. corsConfiguration.addAllowedMethod(HttpMethod.POST);
  65. corsConfiguration.addAllowedMethod(HttpMethod.GET);
  66. corsConfiguration.addAllowedMethod(HttpMethod.PUT);
  67. corsConfiguration.addAllowedMethod(HttpMethod.DELETE);
  68. corsConfiguration.addAllowedMethod(HttpMethod.PATCH);
  69. // corsConfiguration.addAllowedMethod("*");
  70. corsConfiguration.addAllowedHeader("*");
  71. corsConfiguration.setMaxAge(7200L);
  72. corsConfiguration.setAllowCredentials(true);
  73. return corsConfiguration;
  74. }
  75. }