mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-31 20:33:41 +00:00
增加跨域支持
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
<modules>
|
||||
<module>udf-starter-core</module>
|
||||
<module>udf-starter-rms</module>
|
||||
<module>udf-starter-cors</module>
|
||||
</modules>
|
||||
<!-- 依赖管理 -->
|
||||
<dependencyManagement>
|
||||
@@ -47,18 +48,24 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<!-- udf-core-starter -->
|
||||
<!-- udf-starter-core -->
|
||||
<dependency>
|
||||
<groupId>com.egridcloud.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
<!-- udf-core-starter -->
|
||||
<!-- udf-starter-rms -->
|
||||
<dependency>
|
||||
<groupId>com.egridcloud.udf</groupId>
|
||||
<artifactId>udf-starter-rms</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
<!-- udf-starter-cors -->
|
||||
<dependency>
|
||||
<groupId>com.egridcloud.udf</groupId>
|
||||
<artifactId>udf-starter-cors</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<!-- 构建 -->
|
||||
|
||||
@@ -51,6 +51,10 @@
|
||||
<version>${springfox-swagger-ui.version}</version>
|
||||
</dependency>
|
||||
<!-- commons -->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="true">
|
||||
<fileset name="all" enabled="true" check-config-name="checkstyle.egridcloud.com" local="false">
|
||||
<file-match-pattern match-pattern="." include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
@@ -0,0 +1,4 @@
|
||||
/target/
|
||||
/bin/
|
||||
/.classpath
|
||||
/.project
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- maven坐标 -->
|
||||
<artifactId>udf-starter-cors</artifactId>
|
||||
<name>udf-starter-cors</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.egridcloud.com</url>
|
||||
<!-- 父依赖 -->
|
||||
<parent>
|
||||
<groupId>com.egridcloud.udf</groupId>
|
||||
<artifactId>udf-starter</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- udf-core-starter -->
|
||||
<dependency>
|
||||
<groupId>com.egridcloud.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* CorsConfig.java
|
||||
* Created at 2017-04-25
|
||||
* Created by Administrator
|
||||
* Copyright (C) 2016 egridcloud.com, All rights reserved.
|
||||
*/
|
||||
package com.egridcloud.udf.core.cors;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
/**
|
||||
* 描述 : CorsConfig
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "com.egridcloud.cors")
|
||||
public class CorsConfig {
|
||||
|
||||
/**
|
||||
* 描述 : 跨域信息
|
||||
*/
|
||||
private Map<String, CorsRegistrationConfig> config;
|
||||
|
||||
/**
|
||||
* 描述 : corsConfigurer
|
||||
*
|
||||
* @return WebMvcConfigurer
|
||||
*/
|
||||
@Bean
|
||||
public WebMvcConfigurer corsConfigurer() { //NOSONAR
|
||||
return new WebMvcConfigurerAdapter() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
//扫描地址
|
||||
if (!CollectionUtils.isEmpty(config)) {
|
||||
Iterator<String> keys = config.keySet().iterator();
|
||||
while (keys.hasNext()) {
|
||||
String key = keys.next();
|
||||
CorsRegistrationConfig item = config.get(key);
|
||||
CorsRegistration cr = registry.addMapping(item.getMapping());
|
||||
if (item.getAllowCredentials() != null) {
|
||||
cr.allowCredentials(item.getAllowCredentials());
|
||||
}
|
||||
if (StringUtils.isNotBlank(item.getAllowedOrigins())) {
|
||||
String[] allowedOriginArray = item.getAllowedOrigins().split(",");
|
||||
cr.allowedOrigins(allowedOriginArray);
|
||||
}
|
||||
if (StringUtils.isNotBlank(item.getAllowedMethods())) {
|
||||
String[] allowedMethodArray = item.getAllowedMethods().split(",");
|
||||
cr.allowedMethods(allowedMethodArray);
|
||||
}
|
||||
if (StringUtils.isNotBlank(item.getAllowedHeaders())) {
|
||||
String[] allowedHeaderArray = item.getAllowedHeaders().split(",");
|
||||
cr.allowedHeaders(allowedHeaderArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取config
|
||||
*
|
||||
* @return the config
|
||||
*/
|
||||
public Map<String, CorsRegistrationConfig> getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置config
|
||||
*
|
||||
* @param config the config to set
|
||||
*/
|
||||
public void setConfig(Map<String, CorsRegistrationConfig> config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 跨域信息
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public static class CorsRegistrationConfig {
|
||||
/**
|
||||
* 描述 : 扫描地址
|
||||
*/
|
||||
private String mapping = "/**";
|
||||
|
||||
/**
|
||||
* 描述 : 允许证书
|
||||
*/
|
||||
private Boolean allowCredentials = null;
|
||||
|
||||
/**
|
||||
* 描述 : 允许的域
|
||||
*/
|
||||
private String allowedOrigins = "*";
|
||||
|
||||
/**
|
||||
* 描述 : 允许的方法
|
||||
*/
|
||||
private String allowedMethods = "POST,GET,DELETE,PUT";
|
||||
|
||||
/**
|
||||
* 描述 : 允许的头信息
|
||||
*/
|
||||
private String allowedHeaders = "*";
|
||||
|
||||
/**
|
||||
* 描述 : 获取mapping
|
||||
*
|
||||
* @return the mapping
|
||||
*/
|
||||
public String getMapping() {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置mapping
|
||||
*
|
||||
* @param mapping the mapping to set
|
||||
*/
|
||||
public void setMapping(String mapping) {
|
||||
this.mapping = mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取allowCredentials
|
||||
*
|
||||
* @return the allowCredentials
|
||||
*/
|
||||
public Boolean getAllowCredentials() {
|
||||
return allowCredentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置allowCredentials
|
||||
*
|
||||
* @param allowCredentials the allowCredentials to set
|
||||
*/
|
||||
public void setAllowCredentials(Boolean allowCredentials) {
|
||||
this.allowCredentials = allowCredentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取allowedOrigins
|
||||
*
|
||||
* @return the allowedOrigins
|
||||
*/
|
||||
public String getAllowedOrigins() {
|
||||
return allowedOrigins;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置allowedOrigins
|
||||
*
|
||||
* @param allowedOrigins the allowedOrigins to set
|
||||
*/
|
||||
public void setAllowedOrigins(String allowedOrigins) {
|
||||
this.allowedOrigins = allowedOrigins;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取allowedMethods
|
||||
*
|
||||
* @return the allowedMethods
|
||||
*/
|
||||
public String getAllowedMethods() {
|
||||
return allowedMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置allowedMethods
|
||||
*
|
||||
* @param allowedMethods the allowedMethods to set
|
||||
*/
|
||||
public void setAllowedMethods(String allowedMethods) {
|
||||
this.allowedMethods = allowedMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取allowedHeaders
|
||||
*
|
||||
* @return the allowedHeaders
|
||||
*/
|
||||
public String getAllowedHeaders() {
|
||||
return allowedHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 设置allowedHeaders
|
||||
*
|
||||
* @param allowedHeaders the allowedHeaders to set
|
||||
*/
|
||||
public void setAllowedHeaders(String allowedHeaders) {
|
||||
this.allowedHeaders = allowedHeaders;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user