Browse Source

跨域问题

hcst_sunzhen 4 years ago
parent
commit
3a9080dcfe

+ 33 - 0
src/main/java/cn/com/ctop/okr/config/WebMvcConfiguration.java

@@ -0,0 +1,33 @@
+package cn.com.ctop.okr.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * Spring Boot 2.0 解决跨域问题
+ *
+ * @Author qinfeng
+ *
+ */
+@Configuration
+public class WebMvcConfiguration implements WebMvcConfigurer {
+	@Bean
+	public CorsFilter corsFilter() {
+		final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
+		final CorsConfiguration corsConfiguration = new CorsConfiguration();
+		/* 是否允许请求带有验证信息 */
+		corsConfiguration.setAllowCredentials(true);
+		/* 允许访问的客户端域名 */
+		corsConfiguration.addAllowedOrigin("*");
+		/* 允许服务端访问的客户端请求头 */
+		corsConfiguration.addAllowedHeader("*");
+		/* 允许访问的方法名,GET POST等 */
+		corsConfiguration.addAllowedMethod("*");
+		urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
+		return new CorsFilter(urlBasedCorsConfigurationSource);
+	}
+}

+ 3 - 0
src/main/resources/application.yml

@@ -74,7 +74,10 @@ spring:
         connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
       datasource:
         master:
+          #dev
           url: jdbc:mysql://39.106.184.70:3306/okr?characterEncoding=UTF-8&useUnicode=true&useSSL=false
+          #
+
           username: hcst
           password: test@20190531
           driver-class-name: com.mysql.jdbc.Driver