From 37a1ed63109cedfbf69d2fd8e8052675f1c7370d Mon Sep 17 00:00:00 2001 From: zzs Date: Mon, 28 Oct 2024 16:04:29 +0800 Subject: [PATCH] feat:use gateway --- ensign-gateway/pom.xml | 81 +++++++++++++++++++ .../EnsignGatewayApplication.java | 13 +++ .../ensigngateway/conf/ProxyFilter.java | 76 +++++++++++++++++ .../src/main/resources/application.yml | 27 +++++++ .../EnsignGatewayApplicationTests.java | 13 +++ .../controller/crm/AuthAuthController.java | 40 +++++++++ .../crm/module/crm/service/ProxyService.java | 2 +- 7 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 ensign-gateway/pom.xml create mode 100644 ensign-gateway/src/main/java/com/ensign/ensigngateway/EnsignGatewayApplication.java create mode 100644 ensign-gateway/src/main/java/com/ensign/ensigngateway/conf/ProxyFilter.java create mode 100644 ensign-gateway/src/main/resources/application.yml create mode 100644 ensign-gateway/src/test/java/com/ensign/ensigngateway/EnsignGatewayApplicationTests.java create mode 100644 ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/AuthAuthController.java diff --git a/ensign-gateway/pom.xml b/ensign-gateway/pom.xml new file mode 100644 index 0000000..b2a82ef --- /dev/null +++ b/ensign-gateway/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.5 + + + com.ensign + ensign-gateway + 0.0.1-SNAPSHOT + ensign-gateway + ensign-gateway + + 8 + 2023.0.3 + + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.52 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/ensign-gateway/src/main/java/com/ensign/ensigngateway/EnsignGatewayApplication.java b/ensign-gateway/src/main/java/com/ensign/ensigngateway/EnsignGatewayApplication.java new file mode 100644 index 0000000..b234c8a --- /dev/null +++ b/ensign-gateway/src/main/java/com/ensign/ensigngateway/EnsignGatewayApplication.java @@ -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); + } + +} diff --git a/ensign-gateway/src/main/java/com/ensign/ensigngateway/conf/ProxyFilter.java b/ensign-gateway/src/main/java/com/ensign/ensigngateway/conf/ProxyFilter.java new file mode 100644 index 0000000..77b54db --- /dev/null +++ b/ensign-gateway/src/main/java/com/ensign/ensigngateway/conf/ProxyFilter.java @@ -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 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); + } +} diff --git a/ensign-gateway/src/main/resources/application.yml b/ensign-gateway/src/main/resources/application.yml new file mode 100644 index 0000000..d15002d --- /dev/null +++ b/ensign-gateway/src/main/resources/application.yml @@ -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} + + 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' diff --git a/ensign-gateway/src/test/java/com/ensign/ensigngateway/EnsignGatewayApplicationTests.java b/ensign-gateway/src/test/java/com/ensign/ensigngateway/EnsignGatewayApplicationTests.java new file mode 100644 index 0000000..dccf5ad --- /dev/null +++ b/ensign-gateway/src/test/java/com/ensign/ensigngateway/EnsignGatewayApplicationTests.java @@ -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() { + } + +} diff --git a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/AuthAuthController.java b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/AuthAuthController.java new file mode 100644 index 0000000..e8b0a5d --- /dev/null +++ b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/controller/crm/AuthAuthController.java @@ -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 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); + } +} diff --git a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/service/ProxyService.java b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/service/ProxyService.java index 71c105d..d7265a9 100644 --- a/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/service/ProxyService.java +++ b/ensign-module-crm/ensign-module-crm-biz/src/main/java/com/ensign/crm/module/crm/service/ProxyService.java @@ -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); if (accessToken != null && !accessToken.isEmpty()) { return accessToken;