Skip to content

Commit 46254f5

Browse files
committed
Server:允许跨域请求
1 parent 081400c commit 46254f5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/APIJSONApplication.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
import org.springframework.boot.SpringApplication;
1818
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.web.cors.CorsConfiguration;
21+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
22+
import org.springframework.web.filter.CorsFilter;
1923

2024
/**application
2125
* @author Lemon
@@ -38,4 +42,29 @@ public static void main(String[] args) {
3842
e.printStackTrace();
3943
}
4044
}
45+
46+
47+
48+
49+
/**
50+
* 跨域过滤器
51+
* @return
52+
*/
53+
@Bean
54+
public CorsFilter corsFilter() {
55+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
56+
source.registerCorsConfiguration("/**", buildConfig()); // 4
57+
return new CorsFilter(source);
58+
}
59+
/**CORS跨域配置
60+
* @return
61+
*/
62+
private CorsConfiguration buildConfig() {
63+
CorsConfiguration corsConfiguration = new CorsConfiguration();
64+
corsConfiguration.addAllowedOrigin("*");
65+
corsConfiguration.addAllowedHeader("*");
66+
corsConfiguration.addAllowedMethod("*");
67+
return corsConfiguration;
68+
}
69+
4170
}

0 commit comments

Comments
 (0)