[feat] 新增跨域支持

pull/445/head
hsdllcw 2024-07-25 13:41:15 +08:00
parent 6880fd38d7
commit ff22cd374f
2 changed files with 230 additions and 110 deletions

View File

@ -1,110 +1,198 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<groupId>com.ruoyi</groupId> <groupId>com.ruoyi</groupId>
<artifactId>ruoyi</artifactId> <artifactId>ruoyi</artifactId>
<version>3.6.4</version> <version>3.6.4</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-gateway</artifactId> <artifactId>ruoyi-gateway</artifactId>
<description> <description>
ruoyi-gateway网关模块 ruoyi-gateway网关模块
</description> </description>
<dependencies> <properties>
<kotlin.version>2.0.0</kotlin.version>
<!-- SpringCloud Gateway --> </properties>
<dependency>
<groupId>org.springframework.cloud</groupId> <dependencies>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency> <!-- SpringCloud Gateway -->
<dependency>
<!-- SpringCloud Alibaba Nacos --> <groupId>org.springframework.cloud</groupId>
<dependency> <artifactId>spring-cloud-starter-gateway</artifactId>
<groupId>com.alibaba.cloud</groupId> </dependency>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> <!-- SpringCloud Alibaba Nacos -->
<dependency>
<!-- SpringCloud Alibaba Nacos Config --> <groupId>com.alibaba.cloud</groupId>
<dependency> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<groupId>com.alibaba.cloud</groupId> </dependency>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> <!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<!-- SpringCloud Alibaba Sentinel --> <groupId>com.alibaba.cloud</groupId>
<dependency> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<groupId>com.alibaba.cloud</groupId> </dependency>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency> <!-- SpringCloud Alibaba Sentinel -->
<dependency>
<!-- SpringCloud Alibaba Sentinel Gateway --> <groupId>com.alibaba.cloud</groupId>
<dependency> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<groupId>com.alibaba.cloud</groupId> </dependency>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency> <!-- SpringCloud Alibaba Sentinel Gateway -->
<dependency>
<!-- Sentinel Datasource Nacos --> <groupId>com.alibaba.cloud</groupId>
<dependency> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
<groupId>com.alibaba.csp</groupId> </dependency>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency> <!-- Sentinel Datasource Nacos -->
<dependency>
<!-- SpringBoot Actuator --> <groupId>com.alibaba.csp</groupId>
<dependency> <artifactId>sentinel-datasource-nacos</artifactId>
<groupId>org.springframework.boot</groupId> </dependency>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <!-- SpringBoot Actuator -->
<dependency>
<!-- SpringCloud Loadbalancer --> <groupId>org.springframework.boot</groupId>
<dependency> <artifactId>spring-boot-starter-actuator</artifactId>
<groupId>org.springframework.cloud</groupId> </dependency>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency> <!-- SpringCloud Loadbalancer -->
<dependency>
<!--验证码 --> <groupId>org.springframework.cloud</groupId>
<dependency> <artifactId>spring-cloud-loadbalancer</artifactId>
<groupId>pro.fessional</groupId> </dependency>
<artifactId>kaptcha</artifactId>
</dependency> <!--验证码 -->
<dependency>
<!-- RuoYi Common Redis--> <groupId>pro.fessional</groupId>
<dependency> <artifactId>kaptcha</artifactId>
<groupId>com.ruoyi</groupId> </dependency>
<artifactId>ruoyi-common-redis</artifactId>
</dependency> <!-- RuoYi Common Redis-->
<dependency>
<!-- Swagger --> <groupId>com.ruoyi</groupId>
<dependency> <artifactId>ruoyi-common-redis</artifactId>
<groupId>io.springfox</groupId> </dependency>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version> <!-- Swagger -->
</dependency> <dependency>
<dependency> <groupId>io.springfox</groupId>
<groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId>
<artifactId>springfox-swagger2</artifactId> <version>${swagger.fox.version}</version>
<version>${swagger.fox.version}</version> </dependency>
</dependency> <dependency>
<groupId>io.springfox</groupId>
</dependencies> <artifactId>springfox-swagger2</artifactId>
<version>${swagger.fox.version}</version>
<build> </dependency>
<finalName>${project.artifactId}</finalName> <dependency>
<plugins> <groupId>org.jetbrains.kotlin</groupId>
<plugin> <artifactId>kotlin-stdlib-jdk8</artifactId>
<groupId>org.springframework.boot</groupId> <version>${kotlin.version}</version>
<artifactId>spring-boot-maven-plugin</artifactId> </dependency>
<executions> <dependency>
<execution> <groupId>org.jetbrains.kotlin</groupId>
<goals> <artifactId>kotlin-test</artifactId>
<goal>repackage</goal> <version>${kotlin.version}</version>
</goals> <scope>test</scope>
</execution> </dependency>
</executions>
</plugin> </dependencies>
</plugins>
</build> <build>
<finalName>${project.artifactId}</finalName>
</project> <plugins>
<!-- 指定多个源代码目录、多个资源文件目录 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,32 @@
package com.ruoyi.gateway.config
import org.apache.commons.lang3.ObjectUtils
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.server.WebFilter
@Configuration
open class CorsConfig {
@Value("\${cors.orgins}")
private val corsOrgins: String? = null
@Bean
open fun corsFilter(): WebFilter {
return WebFilter { exchange, chain ->
val response = exchange.response
val request = exchange.request
if (!ObjectUtils.isEmpty(corsOrgins)) {
response.headers["Access-Control-Allow-Origin"] = corsOrgins
} else {
response.headers["Access-Control-Allow-Origin"] = "*"
}
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Max-Age"] = "3600"
response.headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,OPTIONS,HEAD"
response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, Admin-Token, App-Token"
chain.filter(exchange.mutate().request(request.mutate().build()).build())
}
}
}