feat:use gateway
This commit is contained in:
parent
ee0da99e17
commit
37a1ed6310
81
ensign-gateway/pom.xml
Normal file
81
ensign-gateway/pom.xml
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.3.5</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.ensign</groupId>
|
||||||
|
<artifactId>ensign-gateway</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>ensign-gateway</name>
|
||||||
|
<description>ensign-gateway</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>8</java.version>
|
||||||
|
<spring-cloud.version>2023.0.3</spring-cloud.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.fastjson2</groupId>
|
||||||
|
<artifactId>fastjson2</artifactId>
|
||||||
|
<version>2.0.52</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.ensign.ensigngateway;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class EnsignGatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(EnsignGatewayApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.ensign.ensigngateway.conf;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import org.springframework.http.HttpStatusCode;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: TODO
|
||||||
|
* @Date: 2024/10/28 15:20
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class ProxyFilter implements GlobalFilter {
|
||||||
|
|
||||||
|
private final WebClient webClient = WebClient.create();
|
||||||
|
|
||||||
|
@Value("${kingdee.local}")
|
||||||
|
private String authUrl;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
String path = exchange.getRequest().getURI().getPath();
|
||||||
|
log.info("path {}", path);
|
||||||
|
|
||||||
|
if (path.startsWith("/ierp/kapi/")) {
|
||||||
|
String userId = exchange.getRequest().getHeaders().getFirst("userid");
|
||||||
|
|
||||||
|
|
||||||
|
log.info("userId: {}", userId);
|
||||||
|
|
||||||
|
return webClient.get()
|
||||||
|
.uri(authUrl + "/crm-api/auth") // 替换为实际的 API 地址
|
||||||
|
.header("userId", userId)
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.onStatus(HttpStatusCode::is4xxClientError, response -> {
|
||||||
|
return Mono.error(new RuntimeException("Unauthorized"));
|
||||||
|
})
|
||||||
|
.bodyToMono(String.class)
|
||||||
|
.flatMap(responseBody -> {
|
||||||
|
log.info("Authorization response: {}", responseBody);
|
||||||
|
JSONObject respJson = JSON.parseObject(responseBody);
|
||||||
|
Integer code = respJson.getInteger("code");
|
||||||
|
if (code == 0) {
|
||||||
|
String token = respJson.getString("data");
|
||||||
|
exchange.getRequest().mutate()
|
||||||
|
.header("access_token", token)
|
||||||
|
.build();
|
||||||
|
log.info("token: {}", token);
|
||||||
|
}
|
||||||
|
return chain.filter(exchange);
|
||||||
|
})
|
||||||
|
.onErrorResume(e -> {
|
||||||
|
log.error("Authorization failed: {}", e.getMessage());
|
||||||
|
exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
|
||||||
|
return exchange.getResponse().setComplete();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
}
|
27
ensign-gateway/src/main/resources/application.yml
Normal file
27
ensign-gateway/src/main/resources/application.yml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: ensign-gateway
|
||||||
|
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
routes:
|
||||||
|
- id: proxy_route
|
||||||
|
uri: ${kingdee.test-inner-end-point}
|
||||||
|
predicates:
|
||||||
|
- Path=/crm-api/proxy/do/**
|
||||||
|
filters:
|
||||||
|
- RewritePath=/crm-api/proxy/do/(?<segment>.*), /${segment}
|
||||||
|
|
||||||
|
servlet:
|
||||||
|
multipart:
|
||||||
|
max-file-size: 20MB
|
||||||
|
max-request-size: 20MB
|
||||||
|
server:
|
||||||
|
port: 38080
|
||||||
|
|
||||||
|
kingdee:
|
||||||
|
test-public-end-point: 'http://122.4.221.133:8022'
|
||||||
|
test-inner-end-point: 'http://10.64.112.152:8022'
|
||||||
|
prod-public-end-point: 'http://122.4.221.130:8022'
|
||||||
|
prod-inner-end-point: 'http://10.64.111.134:8022'
|
||||||
|
local: 'http://127.0.0.1:48080'
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.ensign.ensigngateway;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class EnsignGatewayApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.ensign.crm.module.crm.controller.crm;
|
||||||
|
|
||||||
|
import com.ensign.crm.framework.common.pojo.CommonResult;
|
||||||
|
import com.ensign.crm.module.crm.service.ProxyService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.security.PermitAll;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: TODO
|
||||||
|
* @Date: 2024/10/28 15:27
|
||||||
|
* @Created: by ZZSLL
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/crm-api")
|
||||||
|
@Slf4j
|
||||||
|
public class AuthAuthController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ProxyService proxyService;
|
||||||
|
|
||||||
|
@GetMapping("/auth")
|
||||||
|
@PermitAll
|
||||||
|
public CommonResult<String> auth() {
|
||||||
|
String accessToken = null;
|
||||||
|
try {
|
||||||
|
accessToken = proxyService.initAccessToken();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return CommonResult.error(500, e.getMessage());
|
||||||
|
}
|
||||||
|
return CommonResult.success(accessToken);
|
||||||
|
}
|
||||||
|
}
|
@ -318,7 +318,7 @@ public class ProxyService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String initAccessToken() throws IOException, AllKingdeeException {
|
public String initAccessToken() throws IOException, AllKingdeeException {
|
||||||
String accessToken = redisTemplate.opsForValue().get(redisKingdeeKey);
|
String accessToken = redisTemplate.opsForValue().get(redisKingdeeKey);
|
||||||
if (accessToken != null && !accessToken.isEmpty()) {
|
if (accessToken != null && !accessToken.isEmpty()) {
|
||||||
return accessToken;
|
return accessToken;
|
||||||
|
Loading…
Reference in New Issue
Block a user