fix:gateway ready

This commit is contained in:
zzs 2024-10-31 14:16:48 +08:00
parent 6dfa45b549
commit 1e7f7580fd
4 changed files with 76 additions and 48 deletions

View File

@ -1,8 +1,7 @@
package com.ensign.ensigngateway.conf; package com.ensign.ensigngateway.conf;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
@ -14,6 +13,9 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.List;
/** /**
* @Description: TODO * @Description: TODO
* @Date: 2024/10/28 15:20 * @Date: 2024/10/28 15:20
@ -37,7 +39,16 @@ public class ProxyFilter implements GlobalFilter {
String userId = exchange.getRequest().getHeaders().getFirst("userid"); String userId = exchange.getRequest().getHeaders().getFirst("userid");
if (path.startsWith("/ierp/kapi/")) { if (path.startsWith("/ierp/kapi/")) {
log.info("userId: {}", userId); log.info("userId: {}", userId);
List<String> unauthorizedPaths = Arrays.asList(
"/ierp/kapi/v2/yem/yem_receipt/yem_crm_marketactivity/getMarketactivities",
"/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_region/CRM_yem_crm_region",
"/ierp/kapi/v2/yem/yem_receipt/api/MarketQRCodeAdd"
);
if (StringUtils.isEmpty(userId)&& unauthorizedPaths.stream().anyMatch(unPath -> unPath.equals(path))) {
userId = "1";
}
String finalUserId = userId;
return webClient.get() return webClient.get()
.uri(authUrl + "/crm-api/auth") // 替换为实际的 API 地址 .uri(authUrl + "/crm-api/auth") // 替换为实际的 API 地址
.header("userId", userId) .header("userId", userId)
@ -50,16 +61,11 @@ public class ProxyFilter implements GlobalFilter {
.bodyToMono(String.class) .bodyToMono(String.class)
.flatMap(responseBody -> { .flatMap(responseBody -> {
log.info("Authorization response: {}", responseBody); log.info("Authorization response: {}", responseBody);
JSONObject respJson = JSON.parseObject(responseBody); exchange.getRequest().mutate()
Integer code = respJson.getInteger("code"); .header("access_token", responseBody)
if (code == 0) { .header("userid", finalUserId)
String token = respJson.getString("data"); .build();
exchange.getRequest().mutate() log.info("token: {}", responseBody);
.header("access_token", token)
.header("userid", userId)
.build();
log.info("token: {}", token);
}
return chain.filter(exchange); return chain.filter(exchange);
}) })
.onErrorResume(e -> { .onErrorResume(e -> {

View File

@ -6,18 +6,22 @@ spring:
gateway: gateway:
routes: routes:
- id: proxy_route - id: proxy_route # ??????
uri: ${kingdee.test-inner-end-point} uri: ${kingdee.test-inner-end-point}
predicates: predicates:
- Path=/crm-api/proxy/do/** - Path=/crm-api/proxy/do/**
filters: filters:
- RewritePath=/crm-api/proxy/do/(?<segment>.*), /${segment} - RewritePath=/crm-api/proxy/do/(?<segment>.*), /${segment}
- id: system-app-api - id: system-app-api # ??CRM????
uri: http://127.0.0.1:38080 uri: http://127.0.0.1:38080
predicates: predicates:
- Path=/admin-api/** - Path=/admin-api/**
- id: crm-api-proxy # ????????
uri: http://127.0.0.1:38080
predicates:
- Path=/crm-api/proxy/file/**
servlet: servlet:
multipart: multipart:
max-file-size: 20MB max-file-size: 20MB

View File

@ -1,14 +1,18 @@
package com.ensign.crm.module.crm.controller.crm; 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 com.ensign.crm.module.crm.service.ProxyService;
import com.ensign.crm.module.system.service.permission.PermissionService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.security.PermitAll; import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
/** /**
@ -25,16 +29,29 @@ public class AuthAuthController {
@Autowired @Autowired
private ProxyService proxyService; private ProxyService proxyService;
@Autowired
private PermissionService permissionService;
@GetMapping("/auth") @GetMapping("/auth")
@PermitAll @PermitAll
public CommonResult<String> auth() { public ResponseEntity<String> auth(HttpServletRequest request) {
String accessToken = null; String accessToken = null;
String userid = request.getHeader("userid");
if (StringUtils.isBlank(userid)) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
if (!"1".equals(userid)) {
boolean hassed = permissionService.hasAnyPermissions(Long.valueOf(userid), "crm:proxy:all");
if (!hassed) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
try { try {
accessToken = proxyService.initAccessToken(); accessToken = proxyService.initAccessToken();
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
return CommonResult.error(500, e.getMessage()); return ResponseEntity.status(500).body(e.getMessage());
} }
return CommonResult.success(accessToken); return ResponseEntity.ok(accessToken);
} }
} }

View File

@ -1,6 +1,5 @@
package com.ensign.crm.module.crm.controller.crm; package com.ensign.crm.module.crm.controller.crm;
import com.ensign.crm.framework.common.pojo.CommonResult;
import com.ensign.crm.module.crm.exception.AllKingdeeException; import com.ensign.crm.module.crm.exception.AllKingdeeException;
import com.ensign.crm.module.crm.service.ProxyService; import com.ensign.crm.module.crm.service.ProxyService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
@ -10,8 +9,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.security.PermitAll; import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -42,12 +43,12 @@ public class ProxyController {
proxyService.doProxy(request, response); proxyService.doProxy(request, response);
} }
@PostMapping(value = "/file/do/**") // @PostMapping(value = "/file/do/**")
@Operation(summary = "转发接口_文件") // @Operation(summary = "转发接口_文件")
@PreAuthorize("@ss.hasPermission('crm:proxy:all')") // @PreAuthorize("@ss.hasPermission('crm:proxy:all')")
public CommonResult<Object> proxyFile(HttpServletRequest request, MultipartFile file) throws IOException, URISyntaxException, AllKingdeeException { // public CommonResult<Object> proxyFile(HttpServletRequest request, MultipartFile file) throws IOException, URISyntaxException, AllKingdeeException {
return CommonResult.success(proxyService.doProxyFile(request, file)); // return CommonResult.success(proxyService.doProxyFile(request, file));
} // }
@GetMapping("/file/read/do") @GetMapping("/file/read/do")
@PermitAll @PermitAll
@ -56,27 +57,27 @@ public class ProxyController {
proxyService.doGetImage(path, response); proxyService.doGetImage(path, response);
} }
@RequestMapping(value = "/unauth/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) // @RequestMapping(value = "/unauth/do/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Operation(summary = "转发接口") // @Operation(summary = "转发接口")
@PermitAll // @PermitAll
public void proxyUnAuth(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException { // public void proxyUnAuth(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
String requestURI = request.getRequestURI(); // String requestURI = request.getRequestURI();
String[] unAuthPath = new String[]{ // String[] unAuthPath = new String[]{
// 市场活动详情 // // 市场活动详情
"/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/yem_crm_marketactivity/getMarketactivities", // "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/yem_crm_marketactivity/getMarketactivities",
//
// 获取机型 // // 获取机型
"/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_region/CRM_yem_crm_region", // "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_region/CRM_yem_crm_region",
//
// 活动登记 // // 活动登记
"/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/api/MarketQRCodeAdd" // "/crm-api/proxy/unauth/do/ierp/kapi/v2/yem/yem_receipt/api/MarketQRCodeAdd"
}; // };
if (isAuthorized(requestURI, unAuthPath)) { // if (isAuthorized(requestURI, unAuthPath)) {
proxyService.doProxy(request, response); // proxyService.doProxy(request, response);
} else { // } else {
response.sendError(HttpServletResponse.SC_FORBIDDEN); // response.sendError(HttpServletResponse.SC_FORBIDDEN);
} // }
} // }
private boolean isAuthorized(String requestURI, String[] unAuthPath) { private boolean isAuthorized(String requestURI, String[] unAuthPath) {
for (String path : unAuthPath) { for (String path : unAuthPath) {