feat(新增): 整合网关做跨域代理

This commit is contained in:
bunny 2024-04-04 19:40:17 +08:00
parent 040ca68248
commit 71c9b53fa1
15 changed files with 236 additions and 30 deletions

View File

@ -5,12 +5,8 @@
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/service-gateway/pom.xml" />
</set>
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">

View File

@ -2,28 +2,10 @@ package com.atguigu.ssyx.common.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {
/**
* * 解决跨域
*
* @param registry 跨域注册表
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("WebMvcConfiguration===>开始跨域注册表...");
registry.addMapping("/admin/**")// 添加路径规则
.allowCredentials(true)// 是否允许在跨域的情况下传递Cookie
.allowedOrigins("*")// 允许请求来源的域规则
.allowedMethods("*").allowedHeaders("*");// 允许所有的请求头
registry.addMapping("/api/**")// 添加路径规则
.allowCredentials(true)// 是否允许在跨域的情况下传递Cookie
.allowedOrigins("*")// 允许请求来源的域规则
.allowedMethods("*").allowedHeaders("*");// 允许所有的请求头
}
}

View File

@ -0,0 +1,21 @@
FROM openjdk:17
MAINTAINER bunny
#系统编码
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
# 设置时区,构建镜像时执行的命令
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
# 设定工作目录
WORKDIR /home/bunny
# 复制jar包
COPY target/*.jar /home/bunny/app.jar
#启动容器时的进程
ENTRYPOINT ["java","-jar","/home/bunny/app.jar"]
#暴露 8080 端口
EXPOSE 8080

47
service-gateway/pom.xml Normal file
View File

@ -0,0 +1,47 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>guigu-ssyx-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-gateway</artifactId>
<packaging>jar</packaging>
<name>service-gateway</name>
<url>https://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-util</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 服务保护组件 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- loadbalancer依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- 服务注册 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,11 @@
package com.atguigu.ssyx.geteway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceGatewayApplication.class, args);
}
}

View File

@ -0,0 +1,29 @@
package com.atguigu.ssyx.geteway.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration
@Slf4j
public class GateWayCorsConfig {
@Bean
protected CorsWebFilter addCorsMappings() {
log.info("CorsConfig===>开始跨域注册表...");
CorsConfiguration config = new CorsConfiguration();
// 设置跨域请求地址
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.addAllowedOrigin("*");
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}

View File

@ -0,0 +1,17 @@
package com.atguigu.ssyx.geteway.controller;
import com.atguigu.ssyx.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/")
@RestController
@Slf4j
public class IndexController {
@GetMapping()
public Result<String> index() {
return Result.success("欢迎访问。。。");
}
}

View File

@ -0,0 +1,8 @@
server:
port: 8200
bunny:
nacos:
server-addr: z-bunny.cn:8848
discovery:
namespace: ssyx

View File

@ -0,0 +1,82 @@
server:
port: 8200
spring:
application:
name: server-gateway
main:
web-application-type: reactive
profiles:
active: dev
cloud:
sentinel:
log:
dir: logs/${spring.application.name}/sentinel
nacos:
discovery:
namespace: ${bunny.nacos.discovery.namespace}
server-addr: ${bunny.nacos.server-addr}
log-name: logs/${spring.application.name}
gateway:
discovery:
locator:
enabled: true
routes:
- id: service-acl
uri: lb://service-acl
predicates:
- Path=/*/acl/**
- id: service-sys
uri: lb://service-sys
predicates:
- Path=/*/sys/**
- id: service-product
uri: lb://service-product
predicates:
- Path=/*/product/**
- id: service-activity
uri: lb://service-activity
predicates:
- Path=/*/activity/**
- id: service-order
uri: lb://service-order
predicates:
- Path=/*/order/**
- id: service-payment
uri: lb://service-payment
predicates:
- Path=/*/payment/**
- id: service-user
uri: lb://service-user
predicates:
- Path=/*/user/**
- id: service-search
uri: lb://service-search
predicates:
- Path=/*/search/**
- id: service-home
uri: lb://service-home
predicates:
- Path=/*/home/**
- id: service-cart
uri: lb://service-cart
predicates:
- Path=/*/cart/**
logging:
level:
com.atguigu.ssyx.acl.mapper: debug
com.atguigu.ssyx.acl.controller: info
com.atguigu.ssyx.acl.service: info
pattern:
dateformat: HH:mm:ss:SSS
file:
path: "logs/${spring.application.name}"

View File

@ -0,0 +1,16 @@
-----------------▄██-█▄---------
-----------------███▄██▄--------
-----------------███████--------
-----------------▀███████-------
-------------------██████▄▄-----
-------------------█████████▄---
-------------------██████▄████--
-------▄███████████████████████-
-----▄███████████████████████▀--
---▄██████████████████████------
---███████████████████████------
---███████████████████████------
-▄▄██████████████████████▀------
-█████████████████▀█████--------
-▀██████████████▀▀-▀█████▄------
-------▀▀▀▀▀▀▀▀▀------▀▀▀▀------

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -13,7 +13,7 @@ import org.springframework.web.multipart.MultipartFile;
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("admin/product")
@RequestMapping("/admin/product")
public class FileUploadController {
@Autowired
private FileUploadService fileUploadService;

View File

@ -2,7 +2,6 @@ package com.atguigu.ssyx.product.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@ -14,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
* @since 2024-04-03
*/
@RestController
@RequestMapping("/product/sku-attr-value")
@RequestMapping("/admin/product/sku-attr-value")
public class SkuAttrValueController {
}

View File

@ -2,7 +2,6 @@ package com.atguigu.ssyx.product.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@ -14,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
* @since 2024-04-03
*/
@RestController
@RequestMapping("/product/sku-image")
@RequestMapping("/admin/product/sku-image")
public class SkuImageController {
}

View File

@ -2,7 +2,6 @@ package com.atguigu.ssyx.product.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
@ -14,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
* @since 2024-04-03
*/
@RestController
@RequestMapping("/product/sku-poster")
@RequestMapping("/admin/product/sku-poster")
public class SkuPosterController {
}