From 26742060e691a814fdb1c66f2cf48b9f43b24801 Mon Sep 17 00:00:00 2001 From: zhuxinru Date: Tue, 6 Jan 2026 17:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HomeycomClientUserTokenController.java | 2 +- ...HoneycomDefaultUserDetailsServiceImpl.java | 2 +- .../service/HoneycomUserDetailsService.java | 2 +- .../msg/api/dto/MessageListUserQueryDto.java | 11 ++ .../msg/api/vo/MessageListItemDetailVo.java | 10 +- .../controller/MessageListItemController.java | 9 +- .../impl/MessageListItemServiceImpl.java | 10 ++ honeycom-spdm/pom.xml | 7 + .../spdm/HoneycomSpdmApplication.java | 2 + .../honeycom/spdm/config/RouteConfig.java | 34 ++++ .../spdm/controller/ProxyController.java | 73 ++++++++ .../spdm/controller/SpdmMsgController.java | 33 ++++ .../spdm/feign/RemoteMsgServiceFeign.java | 10 ++ .../honeycom/spdm/service/ProxyService.java | 14 ++ .../spdm/service/impl/ProxyServiceImpl.java | 163 ++++++++++++++++++ .../src/main/resources/application.yml | 32 +++- .../service/impl/SysStaffServiceImpl.java | 2 + 17 files changed, 403 insertions(+), 13 deletions(-) create mode 100644 honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/config/RouteConfig.java create mode 100644 honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/ProxyController.java create mode 100644 honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/ProxyService.java create mode 100644 honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/impl/ProxyServiceImpl.java diff --git a/honeycom-auth/src/main/java/com/honeycombis/honeycom/auth/endpoint/basic/HomeycomClientUserTokenController.java b/honeycom-auth/src/main/java/com/honeycombis/honeycom/auth/endpoint/basic/HomeycomClientUserTokenController.java index 81b95f3..4243699 100644 --- a/honeycom-auth/src/main/java/com/honeycombis/honeycom/auth/endpoint/basic/HomeycomClientUserTokenController.java +++ b/honeycom-auth/src/main/java/com/honeycombis/honeycom/auth/endpoint/basic/HomeycomClientUserTokenController.java @@ -157,7 +157,7 @@ public class HomeycomClientUserTokenController { // 第2步:构建用户信息对象 // 构建用户信息 , 这里只拼接了 UserTokenDTO 中的 username 和 authorities;TODO 现在加载了用户所有数据, - UserDetails honeycomUser = honeycomUserDetailsService.loadUserByUsername(userInfo.getPhone()); + UserDetails honeycomUser = honeycomUserDetailsService.loadUserByUsername(userInfo.getUsername()); // 第3步:创建认证对象 Authentication usernamePasswordAuthentication = diff --git a/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomDefaultUserDetailsServiceImpl.java b/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomDefaultUserDetailsServiceImpl.java index a03d2cd..44f1c2b 100644 --- a/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomDefaultUserDetailsServiceImpl.java +++ b/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomDefaultUserDetailsServiceImpl.java @@ -76,7 +76,7 @@ public class HoneycomDefaultUserDetailsServiceImpl implements HoneycomUserDetail public UserDetails loadUserByUser(HoneycomUser honeycomUser) { // 避免 C端用户通过接口调用B端接口的安全问题 if (UserTypeEnum.TOB.getStatus().equals(honeycomUser.getUserType())) { - return loadUserByUsername(honeycomUser.getPhone()); + return loadUserByUsername(honeycomUser.getUsername()); } return null; } diff --git a/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomUserDetailsService.java b/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomUserDetailsService.java index df41ca0..07e8af3 100644 --- a/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomUserDetailsService.java +++ b/honeycom-common/honeycom-common-security/src/main/java/com/honeycombis/honeycom/common/security/service/HoneycomUserDetailsService.java @@ -117,7 +117,7 @@ public interface HoneycomUserDetailsService extends UserDetailsService, Ordered * @return */ default UserDetails loadUserByUser(HoneycomUser honeycomUser) { - return this.loadUserByUsername(honeycomUser.getPhone()); + return this.loadUserByUsername(honeycomUser.getUsername()); } } diff --git a/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/dto/MessageListUserQueryDto.java b/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/dto/MessageListUserQueryDto.java index 42a9c9c..8939a54 100644 --- a/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/dto/MessageListUserQueryDto.java +++ b/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/dto/MessageListUserQueryDto.java @@ -3,6 +3,8 @@ package com.honeycombis.honeycom.msg.api.dto; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; +import java.time.LocalDateTime; + /** * @author hbm * @date 2025-01-15 10:03:21 @@ -33,4 +35,13 @@ public class MessageListUserQueryDto { @Schema(description="消息标题") private String msgTitle; + + @Schema(description="发送起始时间") + private String beginTime; + + @Schema(description="发送截止时间") + private String endTime; + + private int current; + private int size; } diff --git a/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/vo/MessageListItemDetailVo.java b/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/vo/MessageListItemDetailVo.java index f83f5dc..4ba1a1d 100644 --- a/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/vo/MessageListItemDetailVo.java +++ b/honeycom-msg/honeycom-msg-api/src/main/java/com/honeycombis/honeycom/msg/api/vo/MessageListItemDetailVo.java @@ -2,10 +2,14 @@ package com.honeycombis.honeycom.msg.api.vo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.honeycombis.honeycom.msg.api.entity.MessageListItemEntity; import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; +import java.io.Serializable; import java.time.LocalDateTime; /** @@ -15,7 +19,11 @@ import java.time.LocalDateTime; @Data @Schema(description = "消息明细列表VO") -public class MessageListItemDetailVo { +@JsonIgnoreProperties(ignoreUnknown = true) +@NoArgsConstructor +@AllArgsConstructor +public class MessageListItemDetailVo implements Serializable { + private static final long serialVersionUID = 1L; /** * 主键 */ diff --git a/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/controller/MessageListItemController.java b/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/controller/MessageListItemController.java index 94dd06f..b2153e5 100644 --- a/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/controller/MessageListItemController.java +++ b/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/controller/MessageListItemController.java @@ -6,6 +6,7 @@ import com.honeycombis.honeycom.common.core.exception.ErrorType; import com.honeycombis.honeycom.common.core.exception.HoneycomException; import com.honeycombis.honeycom.common.core.util.R; import com.honeycombis.honeycom.common.log.annotation.SysLog; +import com.honeycombis.honeycom.common.security.annotation.Inner; import com.honeycombis.honeycom.common.security.util.SecurityUtils; import com.honeycombis.honeycom.msg.api.dto.MessageListItemPageDto; import com.honeycombis.honeycom.msg.api.dto.MessageListItemUpdateDto; @@ -18,6 +19,8 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.springdoc.core.annotations.ParameterObject; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.*; @@ -43,8 +46,11 @@ public class MessageListItemController { @Operation(summary = "用户端分页查询", description = "用户端分页查询") @GetMapping("/user/page") + @Inner(false) public R> getUserMsgPageList(@ParameterObject Page page, @ParameterObject MessageListUserQueryDto query) { - query.setUserId(SecurityUtils.getUser().getId()); + if (ObjectUtils.isEmpty(query.getUserId())) { + query.setUserId(SecurityUtils.getUser().getId()); + } return R.ok(messageListItemService.getUserMsgPageList(page, query)); } @@ -60,6 +66,7 @@ public class MessageListItemController { return R.ok(messageListItemService.getUserUnreadMsgCount(SecurityUtils.getUser().getId())); } + @Inner(false) @SysLog("消息已读/忽略更新接口") @Operation(summary = "消息已读/忽略更新接口", description = "消息已读/忽略更新接口") @PostMapping("/item/isRead") diff --git a/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/service/impl/MessageListItemServiceImpl.java b/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/service/impl/MessageListItemServiceImpl.java index ef3398d..97f3223 100644 --- a/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/service/impl/MessageListItemServiceImpl.java +++ b/honeycom-msg/honeycom-msg-biz/src/main/java/com/honeycombis/honeycom/msg/service/impl/MessageListItemServiceImpl.java @@ -1,5 +1,6 @@ package com.honeycombis.honeycom.msg.service.impl; +import cn.hutool.core.util.ArrayUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -10,6 +11,7 @@ import com.honeycombis.honeycom.common.core.exception.ErrorType; import com.honeycombis.honeycom.common.core.exception.HoneycomException; import com.honeycombis.honeycom.common.data.tenant.TenantContextHolder; import com.honeycombis.honeycom.common.security.util.SecurityUtils; +import com.honeycombis.honeycom.log.api.entity.SysLog; import com.honeycombis.honeycom.msg.api.constant.MsgErrorType; import com.honeycombis.honeycom.msg.api.constant.MsgType; import com.honeycombis.honeycom.msg.api.dto.MessageListItemAddDto; @@ -111,6 +113,14 @@ public class MessageListItemServiceImpl extends ServiceImpla.eq(MessageListItemEntity::getTenantId, TenantContextHolder.getTenantId()).or().isNull(MessageListItemEntity::getTenantId)); // 已读未读查询条件过滤 wrapper.eq(Objects.nonNull(query.getIsRead()), MessageListItemEntity::getIsRead, query.getIsRead()); + + if (query.getBeginTime() != null) { + wrapper.ge(MessageListItemEntity::getSendTime, query.getBeginTime()); + } + if (query.getEndTime() != null) { + wrapper.le(MessageListItemEntity::getSendTime, query.getEndTime()); + } + // 根据创建时间正序排序 wrapper.orderByDesc(MessageListItemEntity::getSendTime); Page pageList = this.page(page, wrapper); diff --git a/honeycom-spdm/pom.xml b/honeycom-spdm/pom.xml index 704b1c8..20acfb2 100644 --- a/honeycom-spdm/pom.xml +++ b/honeycom-spdm/pom.xml @@ -56,6 +56,13 @@ 2.1.0 + + + org.springframework.boot + spring-boot-configuration-processor + true + + com.honeycombis honeycom-flow-task-api diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/HoneycomSpdmApplication.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/HoneycomSpdmApplication.java index a0e8e07..229de3a 100644 --- a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/HoneycomSpdmApplication.java +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/HoneycomSpdmApplication.java @@ -4,6 +4,7 @@ package com.honeycombis.honeycom.spdm; import com.honeycombis.honeycom.common.feign.annotation.EnableHoneycomFeignClients; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @@ -18,6 +19,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling @EnableAsync +@EnableConfigurationProperties public class HoneycomSpdmApplication { public static void main(String[] args) { SpringApplication.run(HoneycomSpdmApplication.class, args); diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/config/RouteConfig.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/config/RouteConfig.java new file mode 100644 index 0000000..3675b96 --- /dev/null +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/config/RouteConfig.java @@ -0,0 +1,34 @@ +package com.honeycombis.honeycom.spdm.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Data +@ConfigurationProperties(prefix = "proxy") +@Component +public class RouteConfig { + + @Data + public static class Route { + private String baseUrl; + private String prefix; + private String targetPrefix = ""; + private int timeout; + private boolean enabled; + private List allowedMethods = Arrays.asList("GET", "POST", "PUT", "DELETE"); + private boolean forwardHeaders = true; + private List excludeHeaders = Arrays.asList("host", "content-length"); + } + + private Map routes = new HashMap<>(); + + public Optional findRouteByPath(String path) { + return routes.entrySet().stream() + .filter(entry -> path.startsWith(entry.getValue().getPrefix())) + .map(Map.Entry::getValue) + .findFirst(); + } +} \ No newline at end of file diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/ProxyController.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/ProxyController.java new file mode 100644 index 0000000..35969b1 --- /dev/null +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/ProxyController.java @@ -0,0 +1,73 @@ +package com.honeycombis.honeycom.spdm.controller; + +import com.honeycombis.honeycom.spdm.config.RouteConfig; +import com.honeycombis.honeycom.spdm.service.ProxyService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; + +@RestController +@Slf4j +public class ProxyController { + + @Autowired + private ProxyService proxyService; + + @Autowired + private RouteConfig routeConfig; + + /** + * 统一的代理入口 + * 匹配所有以/api开头的请求 + */ + @RequestMapping("/api/**") + public ResponseEntity proxy(HttpServletRequest request, + HttpServletResponse response) { + + // 1. 查找匹配的路由配置 + String requestPath = request.getRequestURI(); + Optional routeOpt = routeConfig.findRouteByPath(requestPath); + + if (!routeOpt.isPresent()) { + return ResponseEntity.notFound().build(); + } + + RouteConfig.Route route = routeOpt.get(); + + // 2. 检查路由是否启用 + if (!route.isEnabled()) { + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE) + .body("该路由暂不可用".getBytes()); + } + + // 3. 检查HTTP方法是否允许 + if (!route.getAllowedMethods().contains(request.getMethod())) { + return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED) + .body("不支持该HTTP方法".getBytes()); + } + + // 4. 执行转发 + ResponseEntity proxyResponse = proxyService.forward(request, route); + + // 5. 复制响应头到原始响应(可选) + copyHeadersToResponse(proxyResponse, response); + + return proxyResponse; + } + + private void copyHeadersToResponse(ResponseEntity source, + HttpServletResponse target) { + source.getHeaders().forEach((key, values) -> { + if (!key.toLowerCase().equals("transfer-encoding")) { // 排除分块传输编码 + values.forEach(value -> target.addHeader(key, value)); + } + }); + } +} diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/SpdmMsgController.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/SpdmMsgController.java index e9255db..8b35190 100644 --- a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/SpdmMsgController.java +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/controller/SpdmMsgController.java @@ -19,18 +19,32 @@ package com.honeycombis.honeycom.spdm.controller; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; import com.honeycombis.honeycom.common.core.util.R; +import com.honeycombis.honeycom.msg.api.dto.MessageListItemUpdateDto; +import com.honeycombis.honeycom.msg.api.dto.MessageListUserQueryDto; import com.honeycombis.honeycom.msg.api.dto.MessageOpenApiDTO; +import com.honeycombis.honeycom.msg.api.entity.MessageListItemEntity; +import com.honeycombis.honeycom.msg.api.vo.MessageListItemDetailVo; import com.honeycombis.honeycom.spdm.dto.*; import com.honeycombis.honeycom.spdm.feign.RemoteMsgServiceFeign; +import com.honeycombis.honeycom.spdm.util.PageResult; import com.honeycombis.honeycom.spdm.util.ResponseR; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springdoc.core.annotations.ParameterObject; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.Map; @RestController @@ -43,6 +57,9 @@ public class SpdmMsgController { @Resource private RemoteMsgServiceFeign remoteMsgServiceFeign; + @Autowired + private ObjectMapper objectMapper; + @Operation(summary = "发送消息通知") @PostMapping(value = "/sendMessage") public ResponseR sendMessage(@RequestBody MessageDto messageDto) { @@ -55,4 +72,20 @@ public class SpdmMsgController { return ResponseR.ok(); } + @Operation(summary = "获取消息记录") + @GetMapping(value = "/getUserMsgPageList") + public ResponseR getUserMsgPageList(@ParameterObject MessageListUserQueryDto query, @RequestHeader String userId, @RequestHeader String tenantId) { + query.setUserId(Long.valueOf(userId)); + R> iPageR = remoteMsgServiceFeign.getUserMsgPageList(query, tenantId); + Page pageList = iPageR.getData(); + return ResponseR.ok(PageResult.of(pageList.getTotal(), pageList.getCurrent(), pageList.getSize(), pageList.getRecords())); + } + + @Operation(summary = "消息已读") + @PostMapping(value = "/setMsgReadStatus") + public ResponseR setItemReadStatus(@RequestBody MessageListItemUpdateDto dto, @RequestHeader String tenantId) { + R objectR = remoteMsgServiceFeign.setItemReadStatus(dto, tenantId); + return ResponseR.ok(objectR.getData()); + } + } diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/feign/RemoteMsgServiceFeign.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/feign/RemoteMsgServiceFeign.java index e87d09c..4535db3 100644 --- a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/feign/RemoteMsgServiceFeign.java +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/feign/RemoteMsgServiceFeign.java @@ -1,11 +1,17 @@ package com.honeycombis.honeycom.spdm.feign; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.honeycombis.honeycom.common.core.constant.CommonConstants; import com.honeycombis.honeycom.common.core.constant.ServiceNameConstants; import com.honeycombis.honeycom.common.core.util.R; import com.honeycombis.honeycom.common.feign.config.FeignConfig; +import com.honeycombis.honeycom.msg.api.dto.MessageListItemUpdateDto; +import com.honeycombis.honeycom.msg.api.dto.MessageListUserQueryDto; import com.honeycombis.honeycom.msg.api.dto.MessageOpenApiDTO; +import com.honeycombis.honeycom.msg.api.vo.MessageListItemDetailVo; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.cloud.openfeign.SpringQueryMap; import org.springframework.web.bind.annotation.*; @@ -15,5 +21,9 @@ public interface RemoteMsgServiceFeign { @PostMapping(value = "/openapi/push") R pushMessage(@RequestBody MessageOpenApiDTO dto, @RequestHeader(CommonConstants.TENANT_ID) String tenantId); + @GetMapping("/message/list/user/page") + R> getUserMsgPageList(@SpringQueryMap MessageListUserQueryDto query, @RequestHeader(CommonConstants.TENANT_ID) String tenantId); + @PostMapping("/message/list/item/isRead") + R setItemReadStatus(@RequestBody MessageListItemUpdateDto dto, @RequestHeader(CommonConstants.TENANT_ID) String tenantId); } diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/ProxyService.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/ProxyService.java new file mode 100644 index 0000000..96b1edd --- /dev/null +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/ProxyService.java @@ -0,0 +1,14 @@ +package com.honeycombis.honeycom.spdm.service; + +import com.honeycombis.honeycom.spdm.config.RouteConfig; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; + +public interface ProxyService { + ResponseEntity forward(HttpServletRequest request, RouteConfig.Route route); + + String buildTargetUrl(HttpServletRequest request, RouteConfig.Route route); + + HttpHeaders extractHeaders(HttpServletRequest request, RouteConfig.Route route); +} diff --git a/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/impl/ProxyServiceImpl.java b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/impl/ProxyServiceImpl.java new file mode 100644 index 0000000..45d1671 --- /dev/null +++ b/honeycom-spdm/src/main/java/com/honeycombis/honeycom/spdm/service/impl/ProxyServiceImpl.java @@ -0,0 +1,163 @@ +package com.honeycombis.honeycom.spdm.service.impl; + +import com.alibaba.nacos.common.utils.StringUtils; +import com.honeycombis.honeycom.spdm.config.RouteConfig; +import com.honeycombis.honeycom.spdm.service.ProxyService; +import jakarta.servlet.http.HttpServletRequest; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; + +@Service +@Slf4j +public class ProxyServiceImpl implements ProxyService { + + @Autowired + private RestTemplate restTemplate; + + @Override + public ResponseEntity forward(HttpServletRequest request, RouteConfig.Route route) { + try { + // 1. 构建目标URL + String targetUrl = buildTargetUrl(request, route); + + // 2. 提取请求头 + HttpHeaders headers = extractHeaders(request, route); + + // 3. 提取请求体 + byte[] body = extractRequestBody(request); + + // 4. 构建HttpEntity + HttpEntity entity = new HttpEntity<>(body, headers); + + // 5. 执行请求 + HttpMethod method = HttpMethod.valueOf(request.getMethod()); + + log.debug("Forwarding request: {} {} -> {}", + method, request.getRequestURI(), targetUrl); + + return restTemplate.exchange( + targetUrl, + method, + entity, + byte[].class + ); + + } catch (ResourceAccessException e) { + log.error("Connection failed to target system: {}", route.getBaseUrl(), e); + throw new RuntimeException("无法连接到目标系统", e); + } catch (Exception e) { + log.error("Forward request failed", e); + throw new RuntimeException("请求转发失败", e); + } + } + + @Override + public String buildTargetUrl(HttpServletRequest request, RouteConfig.Route route) { + // 1. 获取请求路径 + String requestPath = request.getRequestURI(); + + // 2. 移除路由配置中的prefix + String targetPath = requestPath.substring(route.getPrefix().length()); + + // 3. 确保targetPath以/开头 + if (!targetPath.startsWith("/")) { + targetPath = "/" + targetPath; + } + + // 4. 构建目标URL + StringBuilder urlBuilder = new StringBuilder(); + + // 添加baseUrl + urlBuilder.append(route.getBaseUrl()); + + // 添加目标系统的前缀(如果配置了) + if (StringUtils.hasText(route.getTargetPrefix())) { + String targetPrefix = route.getTargetPrefix(); + // 确保targetPrefix以/开头且不以/结尾 + if (!targetPrefix.startsWith("/")) { + targetPrefix = "/" + targetPrefix; + } + if (targetPrefix.endsWith("/")) { + targetPrefix = targetPrefix.substring(0, targetPrefix.length() - 1); + } + urlBuilder.append(targetPrefix); + } + + // 添加请求路径 + urlBuilder.append(targetPath); + + // 5. 添加查询参数 + String queryString = request.getQueryString(); + if (StringUtils.hasText(queryString)) { + urlBuilder.append("?").append(queryString); + } + + String targetUrl = urlBuilder.toString(); + log.debug("构建目标URL: {} -> {}", requestPath, targetUrl); + + return targetUrl; + } + + @Override + public HttpHeaders extractHeaders(HttpServletRequest request, + RouteConfig.Route route) { + HttpHeaders headers = new HttpHeaders(); + + if (!route.isForwardHeaders()) { + return headers; + } + + // 复制所有请求头 + Enumeration headerNames = request.getHeaderNames(); + while (headerNames.hasMoreElements()) { + String headerName = headerNames.nextElement().toLowerCase(); + + // 排除不需要转发的头 + if (route.getExcludeHeaders().contains(headerName)) { + continue; + } + + Enumeration headerValues = request.getHeaders(headerName); + while (headerValues.hasMoreElements()) { + headers.add(headerName, headerValues.nextElement()); + } + } + + // 添加自定义头(可选) + headers.add("X-Forwarded-For", request.getRemoteAddr()); + headers.add("X-Forwarded-Host", request.getServerName()); + headers.add("X-Forwarded-Proto", request.getScheme()); + + return headers; + } + + private byte[] extractRequestBody(HttpServletRequest request) throws IOException { + if (request.getContentLength() <= 0) { + return new byte[0]; + } + + try (InputStream inputStream = request.getInputStream(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + + return outputStream.toByteArray(); + } + } +} \ No newline at end of file diff --git a/honeycom-spdm/src/main/resources/application.yml b/honeycom-spdm/src/main/resources/application.yml index f48b372..7b860a9 100644 --- a/honeycom-spdm/src/main/resources/application.yml +++ b/honeycom-spdm/src/main/resources/application.yml @@ -5,22 +5,22 @@ server: spring: application: - name: @artifactId@ + name: honeycom-spdm cloud: nacos: - username: @nacos.username@ - password: @nacos.password@ + username: nacos + password: N@c0s_2025_Honey! discovery: server-addr: ${NACOS_HOST:honeycom-register}:${NACOS_PORT:8848} - namespace: @nacos.namespace@ + namespace: config: server-addr: ${spring.cloud.nacos.discovery.server-addr} - namespace: @nacos.namespace@ + namespace: config: import: - optional:classpath:/application-local.yml - - optional:nacos:application-@profiles.active@.yml - - optional:nacos:${spring.application.name}-@profiles.active@.yml + - optional:nacos:application-dev.yml + - optional:nacos:${spring.application.name}-dev.yml springdoc: api-docs: @@ -33,4 +33,20 @@ springdoc: tags-sorter: alpha default-flat-param-object: false cache: - disabled: true \ No newline at end of file + disabled: true + +proxy: + routes: + spdm-system: + base-url: http://192.168.65.161:7103 + prefix: /honeycom-spdm/api/simulation/system + targetPrefix: + timeout: 5000 + enabled: true + + spdm-project: + base-url: http://192.168.65.161:7101 + prefix: /honeycom-spdm/api/simulation/project + targetPrefix: + timeout: 5000 + enabled: true diff --git a/honeycom-tenant/honeycom-tenant-biz/src/main/java/com/honeycombis/honeycom/tenant/service/impl/SysStaffServiceImpl.java b/honeycom-tenant/honeycom-tenant-biz/src/main/java/com/honeycombis/honeycom/tenant/service/impl/SysStaffServiceImpl.java index a088427..80b3211 100644 --- a/honeycom-tenant/honeycom-tenant-biz/src/main/java/com/honeycombis/honeycom/tenant/service/impl/SysStaffServiceImpl.java +++ b/honeycom-tenant/honeycom-tenant-biz/src/main/java/com/honeycombis/honeycom/tenant/service/impl/SysStaffServiceImpl.java @@ -420,6 +420,8 @@ public class SysStaffServiceImpl extends ServiceImpl> userIdListByNickNameLike = honeycomUserServiceFeign.getUserIdListByNickNameLike(sysStaffQueryDTO.getNickname(), SecurityConstants.FROM_IN);; if (CollectionUtils.isNotEmpty(userIdListByNickNameLike.getData())) { queryWrapper.in(SysStaffEntity::getUserId, userIdListByNickNameLike.getData()); + } else { + queryWrapper.in(SysStaffEntity::getUserId, -1L); } }