File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
APIJSON(Server)/APIJSON(Idea)/src/main/java
apijson/demo/server/model Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -71,4 +71,4 @@ public String getDescription() {
7171 public void setDescription (String description ) {
7272 this .description = description ;
7373 }
74- }
74+ }
Original file line number Diff line number Diff line change 1616
1717import org .springframework .boot .SpringApplication ;
1818import 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}
You can’t perform that action at this time.
0 commit comments