Skip to content

Commit

Permalink
修改跨域问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wkeyuan committed Mar 13, 2022
1 parent 51ff4d2 commit 6468881
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/main/java/net/diaowen/dwsurvey/config/CorsConfig.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
package net.diaowen.dwsurvey.config;

import org.apache.catalina.LifecycleState;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* 跨域配置类
*/
@Configuration
public class CorsConfig{
public class CorsConfig {

// private static String AllowOrigin = "*";

private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOriginPattern("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowCredentials(true);//sessionid 多次访问一致
corsConfiguration.setAllowedOriginPatterns(getAllowOrigin());
// corsConfiguration.addAllowedOrigin("*");// 允许任何域名使用
corsConfiguration.addAllowedHeader("*");// 允许任何头
corsConfiguration.addAllowedMethod("*");// 允许任何方法(post、get等)
return corsConfiguration;
}

@Bean
public CorsFilter corsFilter() {
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/file/**", buildConfig());
return new CorsFilter(source);
// source.registerCorsConfiguration("/file/**", buildConfig());
source.registerCorsConfiguration("/**", buildConfig());
CorsFilter corsFilter = new CorsFilter(source);
FilterRegistrationBean bean = new FilterRegistrationBean(corsFilter);
bean.setOrder(0);
return bean;
}

/**
* 可以根据需求调整 AllowOrigin ,默认是所有
* @return
*/
public static List<String> getAllowOrigin() {
List<String> list = new ArrayList<>();
list.add("*");//所有域名
return list;
}
}

0 comments on commit 6468881

Please sign in to comment.