# gate-platform **Repository Path**: owen-yue/gate-platform ## Basic Information - **Project Name**: gate-platform - **Description**: 环保门禁管控平台对接 - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2026-05-22 - **Last Updated**: 2026-05-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: 环保门禁, 门禁管控, 门禁对接, 环保对接 ## README # 环保门禁管控平台对接 > 环保门禁管控省平台对接 Spring Boot Starter > 封装国密(SM2/SM3/SM4)登录认证、请求参数加密与响应解密能力 **此代码仅简单测试,可能存在问题,代码仅作加解密与接口调用参考** **觉得有用的点个Star** ## 背景 对接江苏省环保门禁管控省平台时,平台要求使用国密算法(SM2/SM3/SM4)进行登录认证和数据传输加密。 本 Starter 将这部分通用能力封装为独立组件,开箱即用,**不包含具体业务接口**, 仅提供底层加密认证和 HTTP 请求封装。 特性: - **国密算法全套支持**:SM2(非对称加解密/签名验签)、SM3(哈希摘要)、SM4(CBC 对称加解密) - **完整登录流程封装**:TreeMap 排序 → SM3 摘要 → SM2 加密 → 登录 → SM2 解密 SM4 密钥 → SM4 解密 Token - **数据上报加密**:自动生成 SIGN 签名 + SM4 逐字段加密 - **响应解密**:GET 请求自动 SM4 解密响应体 - **Token 自动管理**:过期自动重新登录,支持自定义存储(内存/数据库/Redis) - **支持 Map 和实体两种入参方式** - **Spring Boot 自动装配**,零配置集成 ## 快速开始 ### 1. 添加依赖 ```xml com.gate.platform gate-platform-spring-boot-starter 1.0.0 ``` > 本项目未发布到 Maven 中央仓库,请先 `mvn install` 安装到本地仓库: > ```bash > cd gate-platform-spring-boot-starter > mvn clean install > ``` ### 2. 配置 application.yml ```yaml gate-platform: app-id: YOUR_APP_ID # 平台分配的应用ID secret: YOUR_SECRET # 平台分配的应用密钥 sm2-public-key: 04xxxxxx... # SM2 公钥(Hex,04开头非压缩格式) sm2-private-key: xxxxxxxx... # SM2 私钥(Hex) api-base-url: https://xxxxxxxx:8888 # 平台API基地址 # 以下为可选配置(均有默认值) login-path: /api/qymj/login # 登录接口路径(默认 /api/qymj/login) token-expire-buffer: 300 # Token 提前过期缓冲秒数(默认 300) connect-timeout: 30 # HTTP 连接超时秒数(默认 30) read-timeout: 30 # HTTP 读取超时秒数(默认 30) ``` ### 3. 注入使用 ```java @Autowired private GatePlatformClient gatePlatformClient; public void report() { Map params = new HashMap<>(); params.put("CRKBH", "G001"); params.put("CRKMC", "正门"); GatePlatformResponse resp = gatePlatformClient.postWithEncryption("/api/xxx/report", params); } ``` ### 4. 非 Spring Boot 项目使用 ```java GatePlatformProperties props = new GatePlatformProperties(); props.setAppId("YOUR_APP_ID"); props.setSecret("YOUR_SECRET"); props.setSm2PublicKey("04xxx..."); props.setSm2PrivateKey("xxx..."); props.setApiBaseUrl("https://xxx.example.cn"); GatePlatformClient client = new GatePlatformClient(props); client.login(); ``` ## API 参考 ### GatePlatformClient | 方法 | 说明 | |------|------| | `login()` | 登录省平台,获取 Token(通常自动调用) | | `postWithEncryption(apiPath, Map)` | POST JSON 请求(Map 方式) | | `postWithEncryption(apiPath, entity)` | POST JSON 请求(实体方式) | | `postMultipartWithEncryption(apiPath, Map, files)` | Multipart 请求(Map + 文件) | | `postMultipartWithEncryption(apiPath, entity, files)` | Multipart 请求(实体 + 文件) | | `getWithDecryption(apiPath)` | GET 请求 + 响应解密(无参数) | | `getWithDecryption(apiPath, Map)` | GET 请求 + 响应解密(Map 参数) | | `getWithDecryption(apiPath, entity)` | GET 请求 + 响应解密(实体参数) | ### 自定义 Token 存储 默认使用内存存储(`InMemoryTokenStore`),应用重启后 Token 丢失会自动重新登录。 如需持久化,实现 `TokenStore` 接口并注册为 Spring Bean,自动覆盖默认实现: ```java @Component public class DatabaseTokenStore implements TokenStore { @Autowired private YourTokenRepository tokenRepo; @Override public TokenInfo load() { YourTokenEntity entity = tokenRepo.findActive(); if (entity == null) return null; return new TokenInfo(entity.getToken(), entity.getSm4Key(), entity.getExpireTs()); } @Override public void save(TokenInfo tokenInfo) { tokenRepo.saveOrUpdate(tokenInfo); } } ```