SpringBoot 2.7.0 处理跨域的问题

1690阅读 0评论2022-05-27 fhadmin
分类:Java


点击(此处)折叠或打开


  1. package com.clickpaas.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.cors.CorsConfiguration;
  6. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  7. import org.springframework.web.filter.CorsFilter;
  8.  
  9. import java.util.Collections;
  10.  
  11. /**
  12.  * @from fhadmin.cn
  13.  * @version 1.0
  14.  * @since 2022/5/5 7:59
  15.  */
  16. @Configuration
  17. public class CorsConfig {
  18.     @Bean
  19.     public CorsFilter corsFilter() {
  20.         CorsConfiguration corsConfiguration = new CorsConfiguration();
  21.         //1,允许任何来源
  22.         corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
  23.         //2,允许任何请求头
  24.         corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
  25.         //3,允许任何方法
  26.         corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
  27.         //4,允许凭证
  28.         corsConfiguration.setAllowCredentials(true);
  29.  
  30.         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  31.         source.registerCorsConfiguration("/**", corsConfiguration);
  32.         return new CorsFilter(source);
  33.     }
  34. }


上一篇:springboot 整合国产达梦数据库配置代码
下一篇:flowable 三种方式部署流程