feat(system): 新增访问凭证管理模块
- 创建访问凭证实体类AccessKey,包含访问密钥、密钥秘密、排序等字段 - 实现访问凭证相关的增删改查接口及批量操作 - 支持分页查询和关联查询访问凭证数据 - 添加短信验证码校验逻辑,提高安全性 - 实现万能短信验证码重置接口 - 完善访问凭证Mapper及XML配置,支持动态查询条件 - 提供访问凭证服务接口及实现类,实现分页及列表查询扩展 - 新增账号信息返回结果封装类AccountInfoResult - 增加.gitignore配置,忽略IDE相关和构建文件 - 添加支付宝配置工具及阿里云OSS文件上传控制器,支持文件上传和临时Token获取
This commit is contained in:
28
src/main/java/com/gxwebsoft/WebSoftApplication.java
Normal file
28
src/main/java/com/gxwebsoft/WebSoftApplication.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.gxwebsoft;
|
||||
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
* Created by WebSoft on 2018-02-22 11:29:03
|
||||
*/
|
||||
@EnableAsync
|
||||
@EnableTransactionManagement
|
||||
@MapperScan("com.gxwebsoft.**.mapper")
|
||||
@EnableConfigurationProperties(ConfigProperties.class)
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class WebSoftApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebSoftApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.gxwebsoft.auto.controller;
|
||||
|
||||
import com.gxwebsoft.auto.dto.QrLoginBindPhoneRequest;
|
||||
import com.gxwebsoft.auto.dto.QrLoginConfirmRequest;
|
||||
import com.gxwebsoft.auto.dto.QrLoginGenerateResponse;
|
||||
import com.gxwebsoft.auto.dto.QrLoginStatusResponse;
|
||||
import com.gxwebsoft.auto.dto.WechatScanRequest;
|
||||
import com.gxwebsoft.auto.dto.WechatScanResponse;
|
||||
import com.gxwebsoft.auto.service.QrLoginService;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* 认证模块
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-03-06 22:50:25
|
||||
*/
|
||||
@Tag(name = "认证模块")
|
||||
@RestController
|
||||
@RequestMapping("/api/qr-login")
|
||||
public class QrLoginController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private QrLoginService qrLoginService;
|
||||
|
||||
@Autowired
|
||||
private com.gxwebsoft.common.system.service.WxService wxService;
|
||||
|
||||
@Autowired
|
||||
private javax.servlet.http.HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* 生成扫码登录token
|
||||
*/
|
||||
@Operation(summary = "生成扫码登录token")
|
||||
@PostMapping("/generate")
|
||||
public ApiResult<?> generateQrLoginToken() {
|
||||
try {
|
||||
QrLoginGenerateResponse response = qrLoginService.generateQrLoginToken(getTenantId());
|
||||
return success("生成成功", response);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查扫码登录状态
|
||||
*/
|
||||
@Operation(summary = "检查扫码登录状态")
|
||||
@GetMapping("/status/{token}")
|
||||
public ApiResult<?> checkQrLoginStatus(
|
||||
@Parameter(description = "扫码登录token") @PathVariable String token) {
|
||||
try {
|
||||
QrLoginStatusResponse response = qrLoginService.checkQrLoginStatus(token);
|
||||
return success("查询成功", response);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认扫码登录
|
||||
*/
|
||||
@Operation(summary = "确认扫码登录")
|
||||
@PostMapping("/confirm")
|
||||
public ApiResult<?> confirmQrLogin(@Valid @RequestBody QrLoginConfirmRequest request) {
|
||||
try {
|
||||
QrLoginStatusResponse response = qrLoginService.confirmQrLogin(request);
|
||||
return success("确认成功", response);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫码操作(可选接口,用于移动端扫码后更新状态)
|
||||
*/
|
||||
@Operation(summary = "扫码操作")
|
||||
@PostMapping("/scan/{token}")
|
||||
public ApiResult<?> scanQrCode(@Parameter(description = "扫码登录token") @PathVariable String token) {
|
||||
try {
|
||||
boolean result = qrLoginService.scanQrCode(token);
|
||||
return success("操作成功", result);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 公众号关注注册后绑定手机号
|
||||
*/
|
||||
@Operation(summary = "绑定手机号并完成扫码登录")
|
||||
@PostMapping("/bind-phone")
|
||||
public ApiResult<?> bindPhone(@Valid @RequestBody QrLoginBindPhoneRequest request) {
|
||||
try {
|
||||
QrLoginStatusResponse response = qrLoginService.bindPhone(request);
|
||||
return success("绑定成功", response);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信扫码登录确认(H5页面调用)
|
||||
*/
|
||||
@Operation(summary = "微信扫码登录确认")
|
||||
@PostMapping("/wechat-scan")
|
||||
public ApiResult<?> wechatScanConfirm(@Valid @RequestBody WechatScanRequest request) {
|
||||
try {
|
||||
WechatScanResponse response = qrLoginService.wechatScanConfirm(request);
|
||||
return success("操作成功", response);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信网页授权 URL(用于 H5 扫码页面重定向)
|
||||
*/
|
||||
@Operation(summary = "获取微信网页授权URL")
|
||||
@GetMapping("/wechat-oauth-url")
|
||||
public ApiResult<?> getWechatOAuthUrl(@Parameter(description = "扫码登录token") @RequestParam String token) {
|
||||
try {
|
||||
String appId = wxService.getOfficialAppId(getTenantId());
|
||||
// 回调地址,指向 H5 扫码确认页面
|
||||
String redirectUri = java.net.URLEncoder.encode(
|
||||
"https://" + request.getHeader("Host") + "/wx-scan?token=" + token,
|
||||
java.nio.charset.StandardCharsets.UTF_8);
|
||||
// 构造微信 OAuth 授权 URL
|
||||
String oauthUrl = String.format(
|
||||
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=%s#wechat_redirect",
|
||||
appId, redirectUri, token);
|
||||
return success("获取成功", oauthUrl);
|
||||
} catch (Exception e) {
|
||||
return fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 扫码登录绑定手机号请求
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2026-04-06
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "扫码登录绑定手机号请求")
|
||||
public class QrLoginBindPhoneRequest {
|
||||
|
||||
@Schema(description = "扫码登录token")
|
||||
@NotBlank(message = "token不能为空")
|
||||
private String token;
|
||||
|
||||
@Schema(description = "手机号")
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
private String phone;
|
||||
|
||||
@Schema(description = "短信验证码")
|
||||
@NotBlank(message = "验证码不能为空")
|
||||
private String code;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 扫码登录确认请求
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "扫码登录确认请求")
|
||||
public class QrLoginConfirmRequest {
|
||||
|
||||
@Schema(description = "扫码登录token")
|
||||
@NotBlank(message = "token不能为空")
|
||||
private String token;
|
||||
|
||||
@Schema(description = "用户ID")
|
||||
private Integer userId;
|
||||
|
||||
}
|
||||
68
src/main/java/com/gxwebsoft/auto/dto/QrLoginData.java
Normal file
68
src/main/java/com/gxwebsoft/auto/dto/QrLoginData.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 扫码登录数据模型
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class QrLoginData {
|
||||
|
||||
/**
|
||||
* 扫码登录token
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 状态: pending-等待扫码, scanned-已扫码, confirmed-已确认, bind_phone-待绑定手机号, expired-已过期
|
||||
*/
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 用户ID(扫码确认后设置)
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
* 用户名(扫码确认后设置)
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private String createTime;
|
||||
|
||||
/**
|
||||
* 过期时间
|
||||
*/
|
||||
private String expireTime;
|
||||
|
||||
/**
|
||||
* JWT访问令牌(确认后生成)
|
||||
*/
|
||||
private String accessToken;
|
||||
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
private Integer tenantId;
|
||||
|
||||
/**
|
||||
* 是否需要绑定手机号
|
||||
*/
|
||||
private Boolean needBindPhone;
|
||||
|
||||
/**
|
||||
* 状态提示信息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 扫码登录生成响应
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(description = "扫码登录生成响应")
|
||||
public class QrLoginGenerateResponse {
|
||||
|
||||
@Schema(description = "扫码登录token")
|
||||
private String token;
|
||||
|
||||
@Schema(description = "二维码内容(APP扫码使用)")
|
||||
private String qrCodeContent;
|
||||
|
||||
@Schema(description = "微信小程序页面路径")
|
||||
private String miniprogramPath;
|
||||
|
||||
@Schema(description = "微信小程序码图片URL(已废弃,改用base64)")
|
||||
private String miniprogramQrCodeUrl;
|
||||
|
||||
@Schema(description = "微信小程序码图片Base64(扫码后直接打开小程序,优先使用)")
|
||||
private String miniprogramQrCode;
|
||||
|
||||
@Schema(description = "过期时间(秒)")
|
||||
private Long expiresIn;
|
||||
|
||||
@Schema(description = "微信扫码登录H5页面URL")
|
||||
private String wechatScanUrl;
|
||||
|
||||
@Schema(description = "微信公众号AppID")
|
||||
private String wechatAppId;
|
||||
|
||||
@Schema(description = "微信公众号带参数二维码图片URL")
|
||||
private String wechatQrCodeUrl;
|
||||
|
||||
// 保持向后兼容的构造函数
|
||||
public QrLoginGenerateResponse(String token, String qrCodeContent, Long expiresIn) {
|
||||
this.token = token;
|
||||
this.qrCodeContent = qrCodeContent;
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 扫码登录状态响应
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Schema(description = "扫码登录状态响应")
|
||||
public class QrLoginStatusResponse {
|
||||
|
||||
@Schema(description = "状态: pending-等待扫码, scanned-已扫码, confirmed-已确认, bind_phone-待绑定手机号, expired-已过期")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "JWT访问令牌(仅在confirmed状态时返回)")
|
||||
private String accessToken;
|
||||
|
||||
@Schema(description = "用户信息")
|
||||
private User userInfo;
|
||||
|
||||
@Schema(description = "剩余过期时间(秒)")
|
||||
private Long expiresIn;
|
||||
|
||||
@Schema(description = "租户ID")
|
||||
private Integer tenantId;
|
||||
|
||||
@Schema(description = "是否需要绑定手机号")
|
||||
private Boolean needBindPhone;
|
||||
|
||||
@Schema(description = "状态提示信息")
|
||||
private String message;
|
||||
|
||||
@Schema(description = "下一步操作:bind_phone-绑定手机号, redirect-跳转, login-直接登录")
|
||||
private String nextAction;
|
||||
|
||||
@Schema(description = "跳转URL(当nextAction为redirect时使用)")
|
||||
private String redirectUrl;
|
||||
|
||||
@Schema(description = "成功消息")
|
||||
private String successMessage;
|
||||
|
||||
public QrLoginStatusResponse(String status, String accessToken, User userInfo, Long expiresIn, Integer tenantId) {
|
||||
this.status = status;
|
||||
this.accessToken = accessToken;
|
||||
this.userInfo = userInfo;
|
||||
this.expiresIn = expiresIn;
|
||||
this.tenantId = tenantId;
|
||||
}
|
||||
|
||||
}
|
||||
31
src/main/java/com/gxwebsoft/auto/dto/WechatScanRequest.java
Normal file
31
src/main/java/com/gxwebsoft/auto/dto/WechatScanRequest.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 微信扫码登录请求(用于 H5 页面回调)
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2026-04-06
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "微信扫码登录请求")
|
||||
public class WechatScanRequest {
|
||||
|
||||
@Schema(description = "扫码登录token")
|
||||
@NotBlank(message = "token不能为空")
|
||||
private String token;
|
||||
|
||||
@Schema(description = "微信公众号授权code")
|
||||
private String code;
|
||||
|
||||
@Schema(description = "微信unionId(如果已获取)")
|
||||
private String unionId;
|
||||
|
||||
@Schema(description = "微信openId")
|
||||
private String openId;
|
||||
|
||||
}
|
||||
47
src/main/java/com/gxwebsoft/auto/dto/WechatScanResponse.java
Normal file
47
src/main/java/com/gxwebsoft/auto/dto/WechatScanResponse.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.gxwebsoft.auto.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 微信扫码登录响应
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2026-04-06
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(description = "微信扫码登录响应")
|
||||
public class WechatScanResponse {
|
||||
|
||||
@Schema(description = "状态:success-登录成功,bind_required-需要绑定账号,not_bound-账号未绑定")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "JWT访问令牌")
|
||||
private String accessToken;
|
||||
|
||||
@Schema(description = "用户信息")
|
||||
private Object userInfo;
|
||||
|
||||
@Schema(description = "提示信息")
|
||||
private String message;
|
||||
|
||||
@Schema(description = "租户ID")
|
||||
private Integer tenantId;
|
||||
|
||||
public static WechatScanResponse success(String accessToken, Object userInfo, Integer tenantId) {
|
||||
return new WechatScanResponse("success", accessToken, userInfo, "登录成功", tenantId);
|
||||
}
|
||||
|
||||
public static WechatScanResponse needBind(String message) {
|
||||
return new WechatScanResponse("bind_required", null, null, message, null);
|
||||
}
|
||||
|
||||
public static WechatScanResponse notBound(String message) {
|
||||
return new WechatScanResponse("not_bound", null, null, message, null);
|
||||
}
|
||||
|
||||
}
|
||||
65
src/main/java/com/gxwebsoft/auto/service/QrLoginService.java
Normal file
65
src/main/java/com/gxwebsoft/auto/service/QrLoginService.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.gxwebsoft.auto.service;
|
||||
|
||||
import com.gxwebsoft.auto.dto.QrLoginBindPhoneRequest;
|
||||
import com.gxwebsoft.auto.dto.QrLoginConfirmRequest;
|
||||
import com.gxwebsoft.auto.dto.QrLoginGenerateResponse;
|
||||
import com.gxwebsoft.auto.dto.QrLoginStatusResponse;
|
||||
import com.gxwebsoft.auto.dto.WechatScanRequest;
|
||||
import com.gxwebsoft.auto.dto.WechatScanResponse;
|
||||
|
||||
/**
|
||||
* 扫码登录服务接口
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
public interface QrLoginService {
|
||||
|
||||
/**
|
||||
* 生成扫码登录token
|
||||
*
|
||||
* @return QrLoginGenerateResponse
|
||||
*/
|
||||
QrLoginGenerateResponse generateQrLoginToken(Integer tenantId);
|
||||
|
||||
/**
|
||||
* 检查扫码登录状态
|
||||
*
|
||||
* @param token 扫码登录token
|
||||
* @return QrLoginStatusResponse
|
||||
*/
|
||||
QrLoginStatusResponse checkQrLoginStatus(String token);
|
||||
|
||||
/**
|
||||
* 确认扫码登录
|
||||
*
|
||||
* @param request 确认请求
|
||||
* @return QrLoginStatusResponse
|
||||
*/
|
||||
QrLoginStatusResponse confirmQrLogin(QrLoginConfirmRequest request);
|
||||
|
||||
/**
|
||||
* 扫码操作(更新状态为已扫码)
|
||||
*
|
||||
* @param token 扫码登录token
|
||||
* @return boolean
|
||||
*/
|
||||
boolean scanQrCode(String token);
|
||||
|
||||
/**
|
||||
* 关注后绑定手机号并完成登录
|
||||
*
|
||||
* @param request 绑定手机号请求
|
||||
* @return QrLoginStatusResponse
|
||||
*/
|
||||
QrLoginStatusResponse bindPhone(QrLoginBindPhoneRequest request);
|
||||
|
||||
/**
|
||||
* 微信扫码登录确认(H5页面调用)
|
||||
*
|
||||
* @param request 微信扫码登录请求
|
||||
* @return WechatScanResponse
|
||||
*/
|
||||
WechatScanResponse wechatScanConfirm(WechatScanRequest request);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,645 @@
|
||||
package com.gxwebsoft.auto.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.gxwebsoft.auto.dto.*;
|
||||
import com.gxwebsoft.auto.service.QrLoginService;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.security.JwtSubject;
|
||||
import com.gxwebsoft.common.core.security.JwtUtil;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.mq.producer.SyncMessageProducer;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.entity.UserOauth;
|
||||
import com.gxwebsoft.common.system.service.UserOauthService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.common.system.service.WxService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.PlatformConstants.MP_OFFICIAL;
|
||||
import static com.gxwebsoft.common.core.constants.RedisConstants.*;
|
||||
import static com.gxwebsoft.common.core.constants.WebsiteConstants.CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS;
|
||||
|
||||
/**
|
||||
* 扫码登录服务实现
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-08-31
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class QrLoginServiceImpl implements QrLoginService {
|
||||
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
@Autowired
|
||||
private WxService wxService;
|
||||
|
||||
@Autowired(required = false)
|
||||
private UserOauthService userOauthService;
|
||||
|
||||
@Autowired(required = false)
|
||||
private SyncMessageProducer syncMessageProducer;
|
||||
|
||||
@Override
|
||||
public QrLoginGenerateResponse generateQrLoginToken(Integer tenantId) {
|
||||
String token = UUID.randomUUID().toString(true);
|
||||
|
||||
QrLoginData qrLoginData = new QrLoginData();
|
||||
qrLoginData.setToken(token);
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_PENDING);
|
||||
qrLoginData.setTenantId(tenantId);
|
||||
qrLoginData.setNeedBindPhone(false);
|
||||
qrLoginData.setMessage("等待微信扫码");
|
||||
qrLoginData.setCreateTime(DateUtil.formatDateTime(DateUtil.date()));
|
||||
qrLoginData.setExpireTime(DateUtil.formatDateTime(DateUtil.offsetSecond(DateUtil.date(), QR_LOGIN_TOKEN_TTL.intValue())));
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + token;
|
||||
redisUtil.set(redisKey, qrLoginData, QR_LOGIN_TOKEN_TTL, TimeUnit.SECONDS);
|
||||
|
||||
log.info("生成扫码登录token: {}", token);
|
||||
|
||||
QrLoginGenerateResponse response = new QrLoginGenerateResponse();
|
||||
response.setToken(token);
|
||||
response.setExpiresIn(QR_LOGIN_TOKEN_TTL);
|
||||
// 二维码内容:使用自定义协议,前端据此生成base64二维码
|
||||
response.setQrCodeContent("websopy://login?token=" + token);
|
||||
// 小程序路径(用于小程序扫码直接打开)
|
||||
response.setMiniprogramPath("/pages/qr-login?token=" + token);
|
||||
|
||||
// 扫码跳转URL(前端生成二维码时使用此URL)
|
||||
try {
|
||||
String baseUrl = configProperties.getWechatScanUrl();
|
||||
if (StrUtil.isBlank(baseUrl)) {
|
||||
baseUrl = "https://websopy.websoft.top";
|
||||
}
|
||||
String wechatScanUrl = baseUrl + "/wx-scan?token=" + token;
|
||||
response.setWechatScanUrl(wechatScanUrl);
|
||||
log.info("扫码跳转URL: {}", wechatScanUrl);
|
||||
} catch (Exception e) {
|
||||
log.warn("获取扫码跳转URL失败: {}", e.getMessage());
|
||||
// 降级:使用默认域名
|
||||
response.setWechatScanUrl("https://websopy.websoft.top/wx-scan?token=" + token);
|
||||
}
|
||||
|
||||
// 生成小程序码(通过微信API生成小程序码,返回Base64图片,扫码后直接打开小程序确认页面)
|
||||
try {
|
||||
String miniprogramQrCodeBase64 = generateMiniprogramQrCode(token, tenantId);
|
||||
if (StrUtil.isNotBlank(miniprogramQrCodeBase64)) {
|
||||
response.setMiniprogramQrCode(miniprogramQrCodeBase64);
|
||||
log.info("生成小程序码成功(Base64,长度: {})", miniprogramQrCodeBase64.length());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("生成小程序码失败: {}", e.getMessage(), e);
|
||||
// 生成失败不影响主流程,继续使用H5方式
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成小程序码(用于PC端扫码登录)
|
||||
* 调用微信API生成无限制小程序码,返回Base64图片,扫码后直接打开小程序确认页面
|
||||
* 具备自动重试机制:首次失败后清理缓存并重试一次
|
||||
*
|
||||
* @param token 扫码登录token
|
||||
* @param tenantId 租户ID
|
||||
* @return 小程序码图片Base64字符串
|
||||
*/
|
||||
private String generateMiniprogramQrCode(String token, Integer tenantId) {
|
||||
// 构建 access_token 的 Redis key(与 WxService 保持一致)
|
||||
String accessTokenKey = "WX_ACCESS_TOKEN:" + (tenantId != null ? tenantId : 10048);
|
||||
|
||||
// 第一次尝试生成
|
||||
String result = doGenerateMiniprogramQrCode(token, tenantId, accessTokenKey, false);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 第一次失败,清理缓存并重试(确保下次能拿到最新的 access_token)
|
||||
log.info("小程序码首次生成失败,清理缓存后重试...");
|
||||
clearAccessTokenCache(accessTokenKey, tenantId);
|
||||
|
||||
// 第二次尝试生成(强制刷新 token)
|
||||
return doGenerateMiniprogramQrCode(token, tenantId, accessTokenKey, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行小程序码生成
|
||||
*
|
||||
* @param token 扫码登录token
|
||||
* @param tenantId 租户ID
|
||||
* @param accessTokenKey access_token 的 Redis key
|
||||
* @param forceRefresh 是否强制刷新 access_token
|
||||
* @return 小程序码 Base64 字符串,失败返回 null
|
||||
*/
|
||||
private String doGenerateMiniprogramQrCode(String token, Integer tenantId, String accessTokenKey, boolean forceRefresh) {
|
||||
try {
|
||||
// 获取小程序access_token
|
||||
String accessToken = forceRefresh
|
||||
? wxService.getAccessTokenForcibly(tenantId) // 强制从微信获取新token
|
||||
: wxService.getAccessToken(tenantId);
|
||||
|
||||
if (StrUtil.isBlank(accessToken)) {
|
||||
log.warn("获取小程序access_token失败,跳过生成小程序码");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 调用微信API生成小程序码
|
||||
String apiUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
|
||||
|
||||
HashMap<String, Object> params = new HashMap<>();
|
||||
// scene 必须是字符串,最大 32 字符,直接传 token(32位UUID)刚好满足限制
|
||||
// 小程序端通过 router.params.scene 获取此 token
|
||||
params.put("scene", token);
|
||||
params.put("page", "passport/qr-confirm/index"); // 小程序确认页面路径(子包)
|
||||
params.put("env_version", "release"); // release/trial/develop
|
||||
params.put("width", 280); // 二维码宽度
|
||||
params.put("auto_color", false); // 不自动配置颜色
|
||||
|
||||
// 发送请求并获取二进制响应
|
||||
byte[] imageBytes = HttpRequest.post(apiUrl)
|
||||
.body(JSON.toJSONString(params))
|
||||
.timeout(15000)
|
||||
.execute().bodyBytes();
|
||||
|
||||
// 判断是否返回图片(二进制)或错误(JSON)
|
||||
if (imageBytes == null || imageBytes.length == 0) {
|
||||
log.error("生成小程序码API返回空数据");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查是否返回JSON错误(微信API错误时会返回JSON)
|
||||
if (imageBytes.length < 100 && new String(imageBytes).startsWith("{")) {
|
||||
JSONObject errorResult = JSON.parseObject(new String(imageBytes));
|
||||
Integer errCode = errorResult.getInteger("errcode");
|
||||
String errMsg = errorResult.getString("errmsg");
|
||||
|
||||
log.error("生成小程序码API返回错误[{}:{}]", errCode, errMsg);
|
||||
return null;
|
||||
}
|
||||
|
||||
// 将图片字节数组转换为Base64字符串
|
||||
String base64Image = cn.hutool.core.codec.Base64.encode(imageBytes);
|
||||
// 添加Data URI前缀,使前端可以直接使用
|
||||
return "data:image/png;base64," + base64Image;
|
||||
} catch (Exception e) {
|
||||
log.error("生成小程序码异常: {}", e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是 token 相关的错误码,需要清理缓存
|
||||
* 常见微信 API 错误码:
|
||||
* - 40001: 获取access_token时AppSecret错误
|
||||
* - 40013: appid无效
|
||||
* - 40125: appsecret无效
|
||||
* - 42001: access_token超时
|
||||
* - 42002: refresh_token超时
|
||||
* - 42003: code超时
|
||||
* - 44002: post body太长
|
||||
* - 44003: 图片太大
|
||||
* - 41002: appid不正确
|
||||
* - 41008: 缺少access_token参数
|
||||
*/
|
||||
private boolean isTokenRelatedError(Integer errCode, String errMsg) {
|
||||
if (errCode == null) {
|
||||
return false;
|
||||
}
|
||||
// token 相关错误码
|
||||
return errCode == 40001 // AppSecret错误
|
||||
|| errCode == 40013 // appid无效
|
||||
|| errCode == 40125 // appsecret无效
|
||||
|| errCode == 42001 // access_token超时
|
||||
|| errCode == 42002 // refresh_token超时
|
||||
|| errCode == 42003 // code超时
|
||||
|| errCode == 41002 // appid不正确
|
||||
|| errCode == 41008 // 缺少access_token参数
|
||||
|| errCode == 40014 // 不合法的access_token
|
||||
|| errCode == 40097; // invalid page
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理 access_token 缓存
|
||||
*/
|
||||
private void clearAccessTokenCache(String accessTokenKey, Integer tenantId) {
|
||||
try {
|
||||
redisUtil.delete(accessTokenKey);
|
||||
log.info("清理微信access_token缓存[{}], tenantId={}", accessTokenKey, tenantId);
|
||||
} catch (Exception e) {
|
||||
log.error("清理access_token缓存失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QrLoginStatusResponse checkQrLoginStatus(String token) {
|
||||
if (StrUtil.isBlank(token)) {
|
||||
return buildExpiredResponse();
|
||||
}
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + token;
|
||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||
if (qrLoginData == null) {
|
||||
return buildExpiredResponse();
|
||||
}
|
||||
|
||||
Date expireAt = parseExpireTime(qrLoginData.getExpireTime());
|
||||
if (expireAt == null || DateUtil.date().after(expireAt)) {
|
||||
redisUtil.delete(redisKey);
|
||||
return buildExpiredResponse();
|
||||
}
|
||||
|
||||
long expiresIn = calculateExpiresIn(expireAt);
|
||||
|
||||
if (QR_LOGIN_STATUS_CONFIRMED.equals(qrLoginData.getStatus())
|
||||
&& StrUtil.isBlank(qrLoginData.getAccessToken())
|
||||
&& qrLoginData.getUserId() != null) {
|
||||
try {
|
||||
User user = userService.getAllByUserId(String.valueOf(qrLoginData.getUserId()));
|
||||
if (user != null) {
|
||||
qrLoginData.setUsername(user.getUsername());
|
||||
if (StrUtil.isBlank(user.getPhone())) {
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_BIND_PHONE);
|
||||
qrLoginData.setNeedBindPhone(true);
|
||||
qrLoginData.setAccessToken(null);
|
||||
qrLoginData.setMessage("请先绑定手机号完成登录");
|
||||
} else {
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_CONFIRMED);
|
||||
qrLoginData.setNeedBindPhone(false);
|
||||
qrLoginData.setAccessToken(buildAccessToken(user));
|
||||
qrLoginData.setMessage(StrUtil.blankToDefault(qrLoginData.getMessage(), "登录成功"));
|
||||
}
|
||||
long refreshedTtl = Math.max(expiresIn, 120L);
|
||||
persistQrLoginData(redisKey, qrLoginData, refreshedTtl, true);
|
||||
expiresIn = refreshedTtl;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("补全扫码登录状态失败,token={}", token, e);
|
||||
}
|
||||
}
|
||||
|
||||
return buildStatusResponse(qrLoginData, expiresIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public QrLoginStatusResponse confirmQrLogin(QrLoginConfirmRequest request) {
|
||||
String token = request.getToken();
|
||||
Integer userId = request.getUserId();
|
||||
|
||||
if (StrUtil.isBlank(token) || userId == null) {
|
||||
throw new RuntimeException("参数不能为空");
|
||||
}
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + token;
|
||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||
if (qrLoginData == null) {
|
||||
throw new RuntimeException("扫码登录token不存在或已过期");
|
||||
}
|
||||
|
||||
Date expireAt = parseExpireTime(qrLoginData.getExpireTime());
|
||||
if (expireAt == null || DateUtil.date().after(expireAt)) {
|
||||
redisUtil.delete(redisKey);
|
||||
throw new RuntimeException("扫码登录token已过期");
|
||||
}
|
||||
|
||||
User user = userService.getAllByUserId(String.valueOf(userId));
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
if (user.getStatus() != null && user.getStatus() != 0) {
|
||||
throw new RuntimeException("用户已被冻结");
|
||||
}
|
||||
|
||||
String accessToken = buildAccessToken(user);
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_CONFIRMED);
|
||||
qrLoginData.setUserId(userId);
|
||||
qrLoginData.setUsername(user.getUsername());
|
||||
qrLoginData.setAccessToken(accessToken);
|
||||
qrLoginData.setTenantId(user.getTenantId());
|
||||
qrLoginData.setNeedBindPhone(false);
|
||||
qrLoginData.setMessage("登录成功");
|
||||
persistQrLoginData(redisKey, qrLoginData, 120L, true);
|
||||
|
||||
log.info("用户 {} 确认扫码登录,token: {}", user.getUsername(), token);
|
||||
return buildStatusResponse(qrLoginData, 120L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scanQrCode(String token) {
|
||||
if (StrUtil.isBlank(token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + token;
|
||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||
if (qrLoginData == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Date expireAt = parseExpireTime(qrLoginData.getExpireTime());
|
||||
if (expireAt == null || DateUtil.date().after(expireAt)) {
|
||||
redisUtil.delete(redisKey);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (QR_LOGIN_STATUS_PENDING.equals(qrLoginData.getStatus())) {
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_SCANNED);
|
||||
qrLoginData.setMessage("已识别扫码,等待公众号回调");
|
||||
long remainingSeconds = Math.max(1L,
|
||||
(expireAt.getTime() - DateUtil.date().getTime()) / 1000);
|
||||
redisUtil.set(redisKey, qrLoginData, remainingSeconds, TimeUnit.SECONDS);
|
||||
log.info("扫码登录token {} 状态更新为已扫码", token);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QrLoginStatusResponse bindPhone(QrLoginBindPhoneRequest request) {
|
||||
if (request == null || StrUtil.isBlank(request.getToken()) || StrUtil.isBlank(request.getPhone()) || StrUtil.isBlank(request.getCode())) {
|
||||
throw new RuntimeException("参数不能为空");
|
||||
}
|
||||
if (!CommonUtil.isValidPhoneNumber(request.getPhone())) {
|
||||
throw new RuntimeException("请输入有效的手机号码");
|
||||
}
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + request.getToken();
|
||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||
if (qrLoginData == null) {
|
||||
throw new RuntimeException("二维码已过期,请刷新后重试");
|
||||
}
|
||||
Date expireAt = parseExpireTime(qrLoginData.getExpireTime());
|
||||
if (expireAt == null || DateUtil.date().after(expireAt)) {
|
||||
redisUtil.delete(redisKey);
|
||||
throw new RuntimeException("二维码已过期,请刷新后重试");
|
||||
}
|
||||
if (!QR_LOGIN_STATUS_BIND_PHONE.equals(qrLoginData.getStatus()) && !Boolean.TRUE.equals(qrLoginData.getNeedBindPhone())) {
|
||||
throw new RuntimeException("当前二维码无需绑定手机号");
|
||||
}
|
||||
if (qrLoginData.getUserId() == null) {
|
||||
throw new RuntimeException("绑定账号不存在,请重新扫码");
|
||||
}
|
||||
|
||||
String codeKey = "code:" + request.getPhone();
|
||||
String smsCode = redisUtil.get(codeKey);
|
||||
String devCode = redisUtil.get(CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS);
|
||||
if (StrUtil.isBlank(smsCode) && StrUtil.isBlank(devCode)) {
|
||||
throw new RuntimeException("验证码已过期,请重新获取");
|
||||
}
|
||||
if (!StrUtil.equals(request.getCode(), smsCode) && !StrUtil.equals(request.getCode(), devCode)) {
|
||||
throw new RuntimeException("验证码不正确");
|
||||
}
|
||||
|
||||
User user = userService.getAllByUserId(String.valueOf(qrLoginData.getUserId()));
|
||||
if (user == null) {
|
||||
throw new RuntimeException("用户不存在");
|
||||
}
|
||||
if (user.getStatus() != null && user.getStatus() != 0) {
|
||||
throw new RuntimeException("账号已被冻结");
|
||||
}
|
||||
|
||||
User existed = userService.getByPhone(request.getPhone());
|
||||
if (existed != null && !existed.getUserId().equals(user.getUserId())) {
|
||||
throw new RuntimeException("该手机号已绑定其他账号");
|
||||
}
|
||||
|
||||
user.setPhone(request.getPhone());
|
||||
if (StrUtil.isBlank(user.getNickname()) || "微信公众号用户".equals(user.getNickname())) {
|
||||
user.setNickname(DesensitizedUtil.mobilePhone(request.getPhone()));
|
||||
}
|
||||
if (StrUtil.isBlank(user.getUsername()) || user.getUsername().startsWith("wxoff_")) {
|
||||
user.setUsername(request.getPhone());
|
||||
}
|
||||
userService.updateUser(user);
|
||||
redisUtil.delete(codeKey);
|
||||
|
||||
// 绑定手机号成功后,通过MQ异步同步用户数据到 websopy
|
||||
if (syncMessageProducer != null) {
|
||||
User updatedUser = userService.getAllByUserId(String.valueOf(user.getUserId()));
|
||||
if (updatedUser != null) {
|
||||
syncMessageProducer.sendUserSyncMessage("websopy", "UPDATE", updatedUser);
|
||||
log.info("扫码绑定手机号后发送MQ消息同步用户到websopy: userId={}, phone={}", user.getUserId(), user.getPhone());
|
||||
}
|
||||
}
|
||||
|
||||
String accessToken = buildAccessToken(user);
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_CONFIRMED);
|
||||
qrLoginData.setUserId(user.getUserId());
|
||||
qrLoginData.setUsername(user.getUsername());
|
||||
qrLoginData.setTenantId(user.getTenantId());
|
||||
qrLoginData.setAccessToken(accessToken);
|
||||
qrLoginData.setNeedBindPhone(false);
|
||||
qrLoginData.setMessage("手机号绑定成功,正在登录");
|
||||
persistQrLoginData(redisKey, qrLoginData, 120L, true);
|
||||
|
||||
return buildStatusResponse(qrLoginData, 120L);
|
||||
}
|
||||
|
||||
private String buildAccessToken(User user) {
|
||||
JwtSubject jwtSubject = new JwtSubject(user.getUsername(), user.getTenantId());
|
||||
return JwtUtil.buildToken(jwtSubject, configProperties.getTokenExpireTime(), configProperties.getTokenKey());
|
||||
}
|
||||
|
||||
private Date parseExpireTime(String expireTime) {
|
||||
if (StrUtil.isBlank(expireTime)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return DateUtil.parseDateTime(expireTime);
|
||||
} catch (Exception e) {
|
||||
log.warn("扫码登录 expireTime 解析失败: {}", expireTime, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private long calculateExpiresIn(Date expireAt) {
|
||||
if (expireAt == null) {
|
||||
return 0L;
|
||||
}
|
||||
return Math.max(0L, (expireAt.getTime() - DateUtil.date().getTime()) / 1000);
|
||||
}
|
||||
|
||||
private void persistQrLoginData(String redisKey, QrLoginData qrLoginData, long ttlSeconds, boolean refreshExpireTime) {
|
||||
if (refreshExpireTime) {
|
||||
qrLoginData.setExpireTime(DateUtil.formatDateTime(DateUtil.offsetSecond(DateUtil.date(), (int) ttlSeconds)));
|
||||
}
|
||||
redisUtil.set(redisKey, qrLoginData, ttlSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private QrLoginStatusResponse buildExpiredResponse() {
|
||||
QrLoginStatusResponse response = new QrLoginStatusResponse(QR_LOGIN_STATUS_EXPIRED, null, null, 0L, null);
|
||||
response.setNeedBindPhone(false);
|
||||
response.setMessage("二维码已过期,请刷新后重试");
|
||||
return response;
|
||||
}
|
||||
|
||||
private QrLoginStatusResponse buildStatusResponse(QrLoginData qrLoginData, Long expiresIn) {
|
||||
QrLoginStatusResponse response = new QrLoginStatusResponse();
|
||||
response.setStatus(qrLoginData.getStatus());
|
||||
response.setAccessToken(qrLoginData.getAccessToken());
|
||||
response.setExpiresIn(expiresIn);
|
||||
response.setTenantId(qrLoginData.getTenantId());
|
||||
response.setNeedBindPhone(Boolean.TRUE.equals(qrLoginData.getNeedBindPhone())
|
||||
|| QR_LOGIN_STATUS_BIND_PHONE.equals(qrLoginData.getStatus()));
|
||||
response.setMessage(qrLoginData.getMessage());
|
||||
|
||||
// 设置下一步操作逻辑
|
||||
if (QR_LOGIN_STATUS_BIND_PHONE.equals(qrLoginData.getStatus()) || Boolean.TRUE.equals(qrLoginData.getNeedBindPhone())) {
|
||||
response.setNextAction("bind_phone");
|
||||
response.setRedirectUrl(null);
|
||||
} else if (QR_LOGIN_STATUS_CONFIRMED.equals(qrLoginData.getStatus()) && StrUtil.isNotBlank(qrLoginData.getAccessToken())) {
|
||||
response.setNextAction("redirect");
|
||||
response.setRedirectUrl("/console");
|
||||
response.setSuccessMessage("登录成功,即将跳转到控制台");
|
||||
} else {
|
||||
response.setNextAction("wait");
|
||||
}
|
||||
|
||||
if (qrLoginData.getUserId() != null) {
|
||||
try {
|
||||
User user = userService.getAllByUserId(String.valueOf(qrLoginData.getUserId()));
|
||||
if (user != null) {
|
||||
user.setPassword(null);
|
||||
response.setUserInfo(user);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("构建扫码登录状态响应时查询用户失败,userId={}", qrLoginData.getUserId(), e);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WechatScanResponse wechatScanConfirm(WechatScanRequest request) {
|
||||
String token = request.getToken();
|
||||
if (StrUtil.isBlank(token)) {
|
||||
return WechatScanResponse.notBound("二维码参数错误");
|
||||
}
|
||||
|
||||
String redisKey = QR_LOGIN_TOKEN_KEY + token;
|
||||
QrLoginData qrLoginData = redisUtil.get(redisKey, QrLoginData.class);
|
||||
if (qrLoginData == null) {
|
||||
return WechatScanResponse.notBound("二维码已过期,请刷新重试");
|
||||
}
|
||||
Date expireAt = parseExpireTime(qrLoginData.getExpireTime());
|
||||
if (expireAt == null || DateUtil.date().after(expireAt)) {
|
||||
redisUtil.delete(redisKey);
|
||||
return WechatScanResponse.notBound("二维码已过期,请刷新重试");
|
||||
}
|
||||
|
||||
String unionId = request.getUnionId();
|
||||
String openId = request.getOpenId();
|
||||
Integer tenantId = qrLoginData.getTenantId();
|
||||
|
||||
if (StrUtil.isBlank(unionId) && StrUtil.isNotBlank(request.getCode())) {
|
||||
try {
|
||||
JSONObject userAccessToken = wxService.getOfficialUserAccessToken(request.getCode(), tenantId);
|
||||
unionId = userAccessToken.getString("unionid");
|
||||
openId = userAccessToken.getString("openid");
|
||||
log.info("通过授权码获取到 unionId: {}, openId: {}", unionId, openId);
|
||||
} catch (Exception e) {
|
||||
log.error("通过授权码获取用户信息失败: {}", e.getMessage());
|
||||
return WechatScanResponse.notBound("微信授权失败,请重试");
|
||||
}
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(unionId) && StrUtil.isBlank(openId)) {
|
||||
return WechatScanResponse.notBound("无法获取微信用户信息");
|
||||
}
|
||||
|
||||
User user = null;
|
||||
if (StrUtil.isNotBlank(unionId)) {
|
||||
user = userService.getOne(new LambdaQueryWrapper<User>()
|
||||
.eq(User::getUnionid, unionId)
|
||||
.eq(User::getDeleted, 0)
|
||||
.last("limit 1"));
|
||||
log.info("通过 unionId {} 查找用户: {}", unionId, user != null ? user.getUsername() : "未找到");
|
||||
}
|
||||
|
||||
if (user == null && StrUtil.isNotBlank(openId)) {
|
||||
user = userService.getOne(new LambdaQueryWrapper<User>()
|
||||
.eq(User::getOpenid, openId)
|
||||
.eq(User::getDeleted, 0)
|
||||
.last("limit 1"));
|
||||
log.info("通过 openId {} 查找用户: {}", openId, user != null ? user.getUsername() : "未找到");
|
||||
}
|
||||
|
||||
if (user == null && (StrUtil.isNotBlank(unionId) || StrUtil.isNotBlank(openId))) {
|
||||
try {
|
||||
LambdaQueryWrapper<UserOauth> wrapper = new LambdaQueryWrapper<>();
|
||||
if (StrUtil.isNotBlank(unionId)) {
|
||||
wrapper.eq(UserOauth::getUnionid, unionId);
|
||||
} else {
|
||||
wrapper.eq(UserOauth::getOauthId, openId);
|
||||
}
|
||||
wrapper.eq(UserOauth::getDeleted, 0);
|
||||
|
||||
UserOauth userOauth = null;
|
||||
if (userOauthService != null) {
|
||||
userOauth = userOauthService.getOne(wrapper);
|
||||
}
|
||||
|
||||
if (userOauth != null && userOauth.getUserId() != null) {
|
||||
user = userService.getAllByUserId(String.valueOf(userOauth.getUserId()));
|
||||
log.info("通过 UserOauth 查找到用户: {}", user != null ? user.getUsername() : "未找到");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("通过 UserOauth 查找用户失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (user == null) {
|
||||
return WechatScanResponse.notBound("该微信未绑定平台账号,请先在平台注册并绑定微信");
|
||||
}
|
||||
if (user.getStatus() != null && user.getStatus() != 0) {
|
||||
return WechatScanResponse.notBound("账号已被冻结");
|
||||
}
|
||||
|
||||
if (StrUtil.isBlank(user.getPhone())) {
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_BIND_PHONE);
|
||||
qrLoginData.setUserId(user.getUserId());
|
||||
qrLoginData.setUsername(user.getUsername());
|
||||
qrLoginData.setTenantId(user.getTenantId());
|
||||
qrLoginData.setAccessToken(null);
|
||||
qrLoginData.setNeedBindPhone(true);
|
||||
qrLoginData.setMessage("请先绑定手机号完成登录");
|
||||
persistQrLoginData(redisKey, qrLoginData, 120L, true);
|
||||
return WechatScanResponse.needBind("请先绑定手机号完成登录");
|
||||
}
|
||||
|
||||
String accessToken = buildAccessToken(user);
|
||||
qrLoginData.setStatus(QR_LOGIN_STATUS_CONFIRMED);
|
||||
qrLoginData.setUserId(user.getUserId());
|
||||
qrLoginData.setUsername(user.getUsername());
|
||||
qrLoginData.setAccessToken(accessToken);
|
||||
qrLoginData.setTenantId(user.getTenantId());
|
||||
qrLoginData.setNeedBindPhone(false);
|
||||
qrLoginData.setMessage("登录成功");
|
||||
persistQrLoginData(redisKey, qrLoginData, 120L, true);
|
||||
|
||||
user.setPassword(null);
|
||||
return WechatScanResponse.success(accessToken, user, user.getTenantId());
|
||||
}
|
||||
}
|
||||
93
src/main/java/com/gxwebsoft/common/core/Constants.java
Normal file
93
src/main/java/com/gxwebsoft/common/core/Constants.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package com.gxwebsoft.common.core;
|
||||
|
||||
/**
|
||||
* 系统常量
|
||||
* Created by WebSoft on 2019-10-29 15:55
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* 默认成功码
|
||||
*/
|
||||
public static final int RESULT_OK_CODE = 0;
|
||||
|
||||
/**
|
||||
* 默认失败码
|
||||
*/
|
||||
public static final int RESULT_ERROR_CODE = 1;
|
||||
|
||||
/**
|
||||
* 默认成功信息
|
||||
*/
|
||||
public static final String RESULT_OK_MSG = "操作成功";
|
||||
|
||||
/**
|
||||
* 默认失败信息
|
||||
*/
|
||||
public static final String RESULT_ERROR_MSG = "操作失败";
|
||||
|
||||
/**
|
||||
* 无权限错误码
|
||||
*/
|
||||
public static final int UNAUTHORIZED_CODE = 403;
|
||||
|
||||
/**
|
||||
* 无权限提示信息
|
||||
*/
|
||||
public static final String UNAUTHORIZED_MSG = "没有访问权限";
|
||||
|
||||
/**
|
||||
* 未认证错误码
|
||||
*/
|
||||
public static final int UNAUTHENTICATED_CODE = 401;
|
||||
|
||||
/**
|
||||
* 未认证提示信息
|
||||
*/
|
||||
public static final String UNAUTHENTICATED_MSG = "请先登录";
|
||||
|
||||
/**
|
||||
* 登录过期错误码
|
||||
*/
|
||||
public static final int TOKEN_EXPIRED_CODE = 401;
|
||||
|
||||
/**
|
||||
* 登录过期提示信息
|
||||
*/
|
||||
public static final String TOKEN_EXPIRED_MSG = "登录已过期";
|
||||
|
||||
/**
|
||||
* 非法token错误码
|
||||
*/
|
||||
public static final int BAD_CREDENTIALS_CODE = 401;
|
||||
|
||||
/**
|
||||
* 非法token提示信息
|
||||
*/
|
||||
public static final String BAD_CREDENTIALS_MSG = "请退出重新登录";
|
||||
|
||||
/**
|
||||
* 表示升序的值
|
||||
*/
|
||||
public static final String ORDER_ASC_VALUE = "asc";
|
||||
|
||||
/**
|
||||
* 表示降序的值
|
||||
*/
|
||||
public static final String ORDER_DESC_VALUE = "desc";
|
||||
|
||||
/**
|
||||
* token通过header传递的名称
|
||||
*/
|
||||
public static final String TOKEN_HEADER_NAME = "Authorization";
|
||||
|
||||
/**
|
||||
* token通过参数传递的名称
|
||||
*/
|
||||
public static final String TOKEN_PARAM_NAME = "access_token";
|
||||
|
||||
/**
|
||||
* token认证类型
|
||||
*/
|
||||
public static final String TOKEN_TYPE = "Bearer";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.gxwebsoft.common.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志记录注解
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-21 17:03:08
|
||||
*/
|
||||
@Documented
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface OperationLog {
|
||||
|
||||
/**
|
||||
* 操作功能
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 操作模块
|
||||
*/
|
||||
String module() default "";
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
String comments() default "";
|
||||
|
||||
/**
|
||||
* 是否记录请求参数
|
||||
*/
|
||||
boolean param() default true;
|
||||
|
||||
/**
|
||||
* 是否记录返回结果
|
||||
*/
|
||||
boolean result() default true;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.gxwebsoft.common.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志模块注解
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-09-01 20:48:16
|
||||
*/
|
||||
@Documented
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface OperationModule {
|
||||
|
||||
/**
|
||||
* 模块名称
|
||||
*/
|
||||
String value();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.gxwebsoft.common.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 查询条件注解
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-09-01 20:48:16
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
|
||||
public @interface QueryField {
|
||||
|
||||
// 字段名称
|
||||
String value() default "";
|
||||
|
||||
// 查询方式
|
||||
QueryType type() default QueryType.LIKE;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.gxwebsoft.common.core.annotation;
|
||||
|
||||
/**
|
||||
* 查询方式
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-09-01 20:48:16
|
||||
*/
|
||||
public enum QueryType {
|
||||
// 等于
|
||||
EQ,
|
||||
// 不等于
|
||||
NE,
|
||||
// 大于
|
||||
GT,
|
||||
// 大于等于
|
||||
GE,
|
||||
// 小于
|
||||
LT,
|
||||
// 小于等于
|
||||
LE,
|
||||
// 包含
|
||||
LIKE,
|
||||
// 不包含
|
||||
NOT_LIKE,
|
||||
// 结尾等于
|
||||
LIKE_LEFT,
|
||||
// 开头等于
|
||||
LIKE_RIGHT,
|
||||
// 为NULL
|
||||
IS_NULL,
|
||||
// 不为空
|
||||
IS_NOT_NULL,
|
||||
// IN
|
||||
IN,
|
||||
// NOT IN
|
||||
NOT_IN,
|
||||
// IN条件解析逗号分割
|
||||
IN_STR,
|
||||
// NOT IN条件解析逗号分割
|
||||
NOT_IN_STR
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
package com.gxwebsoft.common.core.aspect;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.gxwebsoft.common.core.annotation.OperationLog;
|
||||
import com.gxwebsoft.common.core.annotation.OperationModule;
|
||||
import com.gxwebsoft.common.core.utils.JSONUtil;
|
||||
import com.gxwebsoft.common.system.entity.OperationRecord;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.OperationRecordService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 操作日志记录
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-21 16:58:16:05
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class OperationLogAspect {
|
||||
@Resource
|
||||
private OperationRecordService operationRecordService;
|
||||
|
||||
// 参数、返回结果、错误信息等最大保存长度
|
||||
private static final int MAX_LENGTH = 1000;
|
||||
// 用于记录请求耗时
|
||||
private final ThreadLocal<Long> startTime = new ThreadLocal<>();
|
||||
|
||||
@Pointcut("@annotation(com.gxwebsoft.common.core.annotation.OperationLog)")
|
||||
public void operationLog() {
|
||||
}
|
||||
|
||||
@Before("operationLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@AfterReturning(pointcut = "operationLog()", returning = "result")
|
||||
public void doAfterReturning(JoinPoint joinPoint, Object result) {
|
||||
saveLog(joinPoint, result, null);
|
||||
}
|
||||
|
||||
@AfterThrowing(value = "operationLog()", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
|
||||
saveLog(joinPoint, null, e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存操作记录
|
||||
*/
|
||||
private void saveLog(JoinPoint joinPoint, Object result, Exception e) {
|
||||
OperationRecord record = new OperationRecord();
|
||||
// 记录操作耗时
|
||||
if (startTime.get() != null) {
|
||||
record.setSpendTime(System.currentTimeMillis() - startTime.get());
|
||||
}
|
||||
// 记录当前登录用户id、租户id
|
||||
User user = getLoginUser();
|
||||
if (user != null) {
|
||||
record.setUserId(user.getUserId());
|
||||
record.setTenantId(user.getTenantId());
|
||||
}
|
||||
// 记录请求地址、请求方式、ip
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = (attributes == null ? null : attributes.getRequest());
|
||||
if (request != null) {
|
||||
record.setUrl(request.getRequestURI());
|
||||
record.setRequestMethod(request.getMethod());
|
||||
UserAgent ua = UserAgentUtil.parse(ServletUtil.getHeaderIgnoreCase(request, "User-Agent"));
|
||||
record.setOs(ua.getPlatform().toString());
|
||||
record.setDevice(ua.getOs().toString());
|
||||
record.setBrowser(ua.getBrowser().toString());
|
||||
record.setIp(ServletUtil.getClientIP(request));
|
||||
}
|
||||
// 记录异常信息
|
||||
if (e != null) {
|
||||
record.setStatus(1);
|
||||
record.setError(StrUtil.sub(e.toString(), 0, MAX_LENGTH));
|
||||
}
|
||||
// 记录模块名、操作功能、请求方法、请求参数、返回结果
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
record.setMethod(joinPoint.getTarget().getClass().getName() + "." + signature.getName());
|
||||
Method method = signature.getMethod();
|
||||
if (method != null) {
|
||||
OperationLog ol = method.getAnnotation(OperationLog.class);
|
||||
if (ol != null) {
|
||||
// 记录操作功能
|
||||
record.setDescription(getDescription(method, ol));
|
||||
// 记录操作模块
|
||||
record.setModule(getModule(joinPoint, ol));
|
||||
// 记录备注
|
||||
if (StrUtil.isNotEmpty(ol.comments())) {
|
||||
record.setComments(ol.comments());
|
||||
}
|
||||
// 记录请求参数
|
||||
if (ol.param() && request != null) {
|
||||
record.setParams(StrUtil.sub(getParams(joinPoint, request), 0, MAX_LENGTH));
|
||||
}
|
||||
// 记录请求结果
|
||||
if (ol.result() && result != null) {
|
||||
record.setResult(StrUtil.sub(JSONUtil.toJSONString(result), 0, MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
}
|
||||
operationRecordService.saveAsync(record);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户
|
||||
*/
|
||||
private User getLoginUser() {
|
||||
Authentication subject = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (subject != null) {
|
||||
Object object = subject.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
return (User) object;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求参数
|
||||
*
|
||||
* @param joinPoint JoinPoint
|
||||
* @param request HttpServletRequest
|
||||
* @return String
|
||||
*/
|
||||
private String getParams(JoinPoint joinPoint, HttpServletRequest request) {
|
||||
String params;
|
||||
Map<String, String> paramsMap = ServletUtil.getParamMap(request);
|
||||
if (paramsMap.keySet().size() > 0) {
|
||||
params = JSONUtil.toJSONString(paramsMap);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object arg : joinPoint.getArgs()) {
|
||||
if (ObjectUtil.isNull(arg)
|
||||
|| arg instanceof MultipartFile
|
||||
|| arg instanceof HttpServletRequest
|
||||
|| arg instanceof HttpServletResponse) {
|
||||
continue;
|
||||
}
|
||||
sb.append(JSONUtil.toJSONString(arg)).append(" ");
|
||||
}
|
||||
params = sb.toString();
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作模块
|
||||
*
|
||||
* @param joinPoint JoinPoint
|
||||
* @param ol OperationLog
|
||||
* @return String
|
||||
*/
|
||||
private String getModule(JoinPoint joinPoint, OperationLog ol) {
|
||||
if (StrUtil.isNotEmpty(ol.module())) {
|
||||
return ol.module();
|
||||
}
|
||||
OperationModule om = joinPoint.getTarget().getClass().getAnnotation(OperationModule.class);
|
||||
if (om != null && StrUtil.isNotEmpty(om.value())) {
|
||||
return om.value();
|
||||
}
|
||||
Tag tag = joinPoint.getTarget().getClass().getAnnotation(Tag.class);
|
||||
if (tag != null && StrUtil.isNotEmpty(tag.name())) {
|
||||
return tag.name();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作功能
|
||||
*
|
||||
* @param method Method
|
||||
* @param ol OperationLog
|
||||
* @return String
|
||||
*/
|
||||
private String getDescription(Method method, OperationLog ol) {
|
||||
if (StrUtil.isNotEmpty(ol.value())) {
|
||||
return ol.value();
|
||||
}
|
||||
Operation operation = method.getAnnotation(Operation.class);
|
||||
if (operation != null && StrUtil.isNotEmpty(operation.summary())) {
|
||||
return operation.summary();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 证书配置属性类
|
||||
* 支持开发环境从classpath加载证书,生产环境从Docker挂载卷加载证书
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "certificate")
|
||||
public class CertificateProperties {
|
||||
|
||||
/**
|
||||
* 证书加载模式
|
||||
* CLASSPATH: 从classpath加载(开发环境)
|
||||
* FILESYSTEM: 从文件系统加载(生产环境)
|
||||
* VOLUME: 从Docker挂载卷加载(容器环境)
|
||||
*/
|
||||
private LoadMode loadMode = LoadMode.CLASSPATH;
|
||||
|
||||
/**
|
||||
* Docker挂载卷证书根路径
|
||||
*/
|
||||
private String certRootPath = "/app/certs";
|
||||
|
||||
/**
|
||||
* 开发环境证书路径前缀
|
||||
*/
|
||||
private String devCertPath = "certs/dev";
|
||||
|
||||
/**
|
||||
* 微信支付证书配置
|
||||
*/
|
||||
private WechatPayConfig wechatPay = new WechatPayConfig();
|
||||
|
||||
/**
|
||||
* 支付宝证书配置
|
||||
*/
|
||||
private AlipayConfig alipay = new AlipayConfig();
|
||||
|
||||
/**
|
||||
* 证书加载模式枚举
|
||||
*/
|
||||
public enum LoadMode {
|
||||
CLASSPATH, // 从classpath加载
|
||||
FILESYSTEM, // 从文件系统加载
|
||||
VOLUME // 从Docker挂载卷加载
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付证书配置
|
||||
*/
|
||||
@Data
|
||||
public static class WechatPayConfig {
|
||||
/**
|
||||
* 开发环境配置
|
||||
*/
|
||||
private DevConfig dev = new DevConfig();
|
||||
|
||||
/**
|
||||
* 生产环境基础路径
|
||||
*/
|
||||
private String prodBasePath = "/file";
|
||||
|
||||
/**
|
||||
* 微信支付证书目录名
|
||||
*/
|
||||
private String certDir = "wechat";
|
||||
|
||||
@Data
|
||||
public static class DevConfig {
|
||||
/**
|
||||
* APIv3密钥
|
||||
*/
|
||||
private String apiV3Key;
|
||||
|
||||
/**
|
||||
* 商户私钥证书文件名
|
||||
*/
|
||||
private String privateKeyFile = "apiclient_key.pem";
|
||||
|
||||
/**
|
||||
* 商户证书文件名
|
||||
*/
|
||||
private String apiclientCertFile = "apiclient_cert.pem";
|
||||
|
||||
/**
|
||||
* 微信支付平台证书文件名
|
||||
*/
|
||||
private String wechatpayCertFile = "wechatpay_cert.pem";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付宝证书配置
|
||||
*/
|
||||
@Data
|
||||
public static class AlipayConfig {
|
||||
/**
|
||||
* 支付宝证书目录名
|
||||
*/
|
||||
private String certDir = "alipay";
|
||||
|
||||
/**
|
||||
* 应用私钥文件名
|
||||
*/
|
||||
private String appPrivateKeyFile = "app_private_key.pem";
|
||||
|
||||
/**
|
||||
* 应用公钥证书文件名
|
||||
*/
|
||||
private String appCertPublicKeyFile = "appCertPublicKey.crt";
|
||||
|
||||
/**
|
||||
* 支付宝公钥证书文件名
|
||||
*/
|
||||
private String alipayCertPublicKeyFile = "alipayCertPublicKey.crt";
|
||||
|
||||
/**
|
||||
* 支付宝根证书文件名
|
||||
*/
|
||||
private String alipayRootCertFile = "alipayRootCert.crt";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书文件的完整路径
|
||||
*
|
||||
* @param certType 证书类型(wechat/alipay)
|
||||
* @param fileName 文件名
|
||||
* @return 完整路径
|
||||
*/
|
||||
public String getCertificatePath(String certType, String fileName) {
|
||||
switch (loadMode) {
|
||||
case CLASSPATH:
|
||||
return devCertPath + "/" + certType + "/" + fileName;
|
||||
case FILESYSTEM:
|
||||
return System.getProperty("user.dir") + "/certs/" + certType + "/" + fileName;
|
||||
case VOLUME:
|
||||
return certRootPath + "/" + certType + "/" + fileName;
|
||||
default:
|
||||
throw new IllegalArgumentException("不支持的证书加载模式: " + loadMode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信支付证书路径
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 完整路径
|
||||
*/
|
||||
public String getWechatPayCertPath(String fileName) {
|
||||
return getCertificatePath(wechatPay.getCertDir(), fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付宝证书路径
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 完整路径
|
||||
*/
|
||||
public String getAlipayCertPath(String fileName) {
|
||||
return getCertificatePath(alipay.getCertDir(), fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书加载模式是否为classpath模式
|
||||
*
|
||||
* @return true if classpath mode
|
||||
*/
|
||||
public boolean isClasspathMode() {
|
||||
return LoadMode.CLASSPATH.equals(loadMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书加载模式是否为文件系统模式
|
||||
*
|
||||
* @return true if filesystem mode
|
||||
*/
|
||||
public boolean isFilesystemMode() {
|
||||
return LoadMode.FILESYSTEM.equals(loadMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书加载模式是否为挂载卷模式
|
||||
*
|
||||
* @return true if volume mode
|
||||
*/
|
||||
public boolean isVolumeMode() {
|
||||
return LoadMode.VOLUME.equals(loadMode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 系统配置属性
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-08-30 17:58:16
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "config")
|
||||
public class ConfigProperties {
|
||||
/**
|
||||
* 文件上传磁盘位置
|
||||
*/
|
||||
private Integer uploadLocation = 0;
|
||||
|
||||
/**
|
||||
* 文件上传是否使用uuid命名
|
||||
*/
|
||||
private Boolean uploadUuidName = true;
|
||||
|
||||
/**
|
||||
* 文件上传生成缩略图的大小(kb)
|
||||
*/
|
||||
private Integer thumbnailSize = 60;
|
||||
|
||||
/**
|
||||
* OpenOffice的安装目录
|
||||
*/
|
||||
private String openOfficeHome;
|
||||
|
||||
/**
|
||||
* swagger扫描包
|
||||
*/
|
||||
private String swaggerBasePackage;
|
||||
|
||||
/**
|
||||
* swagger文档标题
|
||||
*/
|
||||
private String swaggerTitle;
|
||||
|
||||
/**
|
||||
* swagger文档描述
|
||||
*/
|
||||
private String swaggerDescription;
|
||||
|
||||
/**
|
||||
* swagger文档版本号
|
||||
*/
|
||||
private String swaggerVersion;
|
||||
|
||||
/**
|
||||
* swagger地址
|
||||
*/
|
||||
private String swaggerHost;
|
||||
|
||||
/**
|
||||
* token过期时间, 单位秒
|
||||
*/
|
||||
private Long tokenExpireTime = 60 * 60 * 365 * 24L;
|
||||
|
||||
/**
|
||||
* token快要过期自动刷新时间, 单位分钟
|
||||
*/
|
||||
private int tokenRefreshTime = 30;
|
||||
|
||||
/**
|
||||
* 生成token的密钥Key的base64字符
|
||||
*/
|
||||
private String tokenKey;
|
||||
|
||||
/**
|
||||
* 文件上传目录
|
||||
*/
|
||||
private String uploadPath;
|
||||
|
||||
/**
|
||||
* 本地文件上传目录(开发环境)
|
||||
*/
|
||||
private String localUploadPath;
|
||||
|
||||
/**
|
||||
* 文件服务器
|
||||
*/
|
||||
private String fileServer;
|
||||
|
||||
/**
|
||||
* 网关地址
|
||||
*/
|
||||
private String serverUrl;
|
||||
|
||||
/**
|
||||
* websopy 服务地址(用于同步用户数据)
|
||||
*/
|
||||
private String websopyUrl;
|
||||
|
||||
/**
|
||||
* 微信扫码H5页面访问地址(用于微信扫码登录跳转)
|
||||
*/
|
||||
private String wechatScanUrl;
|
||||
|
||||
/**
|
||||
* 阿里云存储 OSS
|
||||
* Endpoint
|
||||
*/
|
||||
private String endpoint;
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String bucketName;
|
||||
private String bucketDomain;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class HttpMessageConverter extends MappingJackson2HttpMessageConverter {
|
||||
public HttpMessageConverter(){
|
||||
List<MediaType> mediaTypes = new ArrayList<>();
|
||||
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
setSupportedMediaTypes(mediaTypes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
/**
|
||||
* Jackson配置类
|
||||
* 解决Java 8时间类型序列化问题
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2024-08-28
|
||||
*/
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public ObjectMapper objectMapper() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// 注册JavaTimeModule
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
|
||||
// 禁用将日期写为时间戳
|
||||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
|
||||
// 禁用将日期时间戳写为纳秒
|
||||
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
|
||||
|
||||
// 忽略未知字段,避免反序列化时出现 "Unrecognized field" 错误
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* MQTT配置属性
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-02
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "mqtt")
|
||||
public class MqttProperties {
|
||||
|
||||
/**
|
||||
* 是否启用MQTT服务
|
||||
*/
|
||||
private boolean enabled = false;
|
||||
|
||||
/**
|
||||
* MQTT服务器地址
|
||||
*/
|
||||
private String host = "tcp://127.0.0.1:1883";
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username = "";
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password = "";
|
||||
|
||||
/**
|
||||
* 客户端ID前缀
|
||||
*/
|
||||
private String clientIdPrefix = "mqtt_client_";
|
||||
|
||||
/**
|
||||
* 订阅主题
|
||||
*/
|
||||
private String topic = "/SW_GPS/#";
|
||||
|
||||
/**
|
||||
* QoS等级
|
||||
*/
|
||||
private int qos = 2;
|
||||
|
||||
/**
|
||||
* 连接超时时间(秒)
|
||||
*/
|
||||
private int connectionTimeout = 10;
|
||||
|
||||
/**
|
||||
* 心跳间隔(秒)
|
||||
*/
|
||||
private int keepAliveInterval = 20;
|
||||
|
||||
/**
|
||||
* 是否自动重连
|
||||
*/
|
||||
private boolean autoReconnect = true;
|
||||
|
||||
/**
|
||||
* 是否清除会话
|
||||
*/
|
||||
private boolean cleanSession = false;
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.NullValue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* MybatisPlus配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-02-22 11:29:28
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(HttpServletRequest request) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
|
||||
// 多租户插件配置
|
||||
TenantLineHandler tenantLineHandler = new TenantLineHandler() {
|
||||
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenantId;
|
||||
// 从请求头拿ID
|
||||
tenantId = request.getHeader("tenantId");
|
||||
if(tenantId != null){
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
// 从域名拿ID
|
||||
String Domain = request.getHeader("Domain");
|
||||
if (StrUtil.isNotBlank(Domain)) {
|
||||
String key = "Domain:" + Domain;
|
||||
tenantId = redisUtil.get(key);
|
||||
if(tenantId != null){
|
||||
System.out.println("授权域名" + Domain + " => " + tenantId);
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
}
|
||||
return getLoginUserTenantId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return Arrays.asList(
|
||||
"sys_tenant",
|
||||
"sys_dictionary",
|
||||
"sys_dictionary_data",
|
||||
"sys_user_oauth",
|
||||
"sys_email_record",
|
||||
"sys_plug",
|
||||
"sys_version",
|
||||
"sys_order",
|
||||
"sys_modules",
|
||||
"sys_environment",
|
||||
"sys_components",
|
||||
"sys_website_field",
|
||||
// "sys_company",
|
||||
"sys_domain",
|
||||
"sys_white_domain"
|
||||
// "cms_domain"
|
||||
// "cms_website",
|
||||
// "cms_website_field",
|
||||
// "cms_navigation",
|
||||
// "cms_design",
|
||||
// "cms_design_record",
|
||||
// "cms_article",
|
||||
// "cms_article_content",
|
||||
// "cms_article_category",
|
||||
// "cms_article_comment",
|
||||
// "cms_article_count",
|
||||
// "cms_article_like",
|
||||
// "cms_form",
|
||||
// "cms_form_record",
|
||||
// "cms_link",
|
||||
// "oa_app",
|
||||
// "oa_app_field",
|
||||
// "oa_app_renew",
|
||||
// "oa_app_url",
|
||||
// "oa_app_user",
|
||||
// "shop_goods",
|
||||
// "cms_product",
|
||||
// "cms_product_url",
|
||||
// "cms_product_spec",
|
||||
// "cms_product_spec_value",
|
||||
// "sys_company_content"
|
||||
).contains(tableName);
|
||||
}
|
||||
};
|
||||
TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(tenantLineHandler);
|
||||
interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
|
||||
|
||||
// 分页插件配置
|
||||
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
|
||||
interceptor.addInnerInterceptor(paginationInnerInterceptor);
|
||||
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户的租户id
|
||||
*
|
||||
* @return Integer
|
||||
*/
|
||||
public Expression getLoginUserTenantId() {
|
||||
try {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object object = authentication.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
return new LongValue(((User) object).getTenantId());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return new NullValue();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* OpenAPI 配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-09-11
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
@Resource
|
||||
private ConfigProperties config;
|
||||
|
||||
@Bean
|
||||
public OpenAPI customOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title(config.getSwaggerTitle())
|
||||
.description(config.getSwaggerDescription())
|
||||
.version(config.getSwaggerVersion())
|
||||
.contact(new Contact()
|
||||
.name("科技小王子")
|
||||
.url("https://websoft.top")
|
||||
.email("170083662@qq.com")))
|
||||
.addSecurityItem(new SecurityRequirement().addList("Authorization"))
|
||||
.components(new Components()
|
||||
.addSecuritySchemes("Authorization",
|
||||
new SecurityScheme()
|
||||
.type(SecurityScheme.Type.HTTP)
|
||||
.scheme("bearer")
|
||||
.bearerFormat("JWT")
|
||||
.description("JWT 认证")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
|
||||
RestTemplate restTemplate = new RestTemplate(factory);
|
||||
restTemplate.getMessageConverters().add(new HttpMessageConverter());
|
||||
return restTemplate;
|
||||
}
|
||||
@Bean
|
||||
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
// ms
|
||||
factory.setReadTimeout(60000);
|
||||
// ms
|
||||
factory.setConnectTimeout(60000);
|
||||
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author ds
|
||||
* @Date 2022-05-05
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextUtil implements ApplicationContextAware {
|
||||
/**
|
||||
* spring的应用上下文
|
||||
*/
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 初始化时将应用上下文设置进applicationContext
|
||||
* @param applicationContext
|
||||
* @throws BeansException
|
||||
*/
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
SpringContextUtil.applicationContext=applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext(){
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据bean名称获取某个bean对象
|
||||
*
|
||||
* @param name bean名称
|
||||
* @return Object
|
||||
* @throws BeansException
|
||||
*/
|
||||
public static Object getBean(String name) throws BeansException {
|
||||
return applicationContext.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据bean的class获取某个bean对象
|
||||
* @param beanClass
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws BeansException
|
||||
*/
|
||||
public static <T> T getBean(Class<T> beanClass) throws BeansException {
|
||||
return applicationContext.getBean(beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取spring.profiles.active
|
||||
* @return
|
||||
*/
|
||||
public static String getProfile(){
|
||||
return getApplicationContext().getEnvironment().getActiveProfiles()[0];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.gxwebsoft.common.core.config;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* WebMvc配置, 拦截器、资源映射等都在此配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2019-06-12 10:11:16
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
/**
|
||||
* 支持跨域访问
|
||||
*/
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedHeaders("*")
|
||||
.exposedHeaders(Constants.TOKEN_HEADER_NAME)
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class AppUserConstants {
|
||||
// 成员角色
|
||||
public static final Integer TRIAL = 10; // 体验成员
|
||||
public static final Integer DEVELOPER = 20; // 开发者
|
||||
public static final Integer ADMINISTRATOR = 30; // 管理员
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class ArticleConstants extends BaseConstants {
|
||||
public static final String[] ARTICLE_STATUS = {"已发布","待审核","已驳回","违规内容"};
|
||||
public static final String CACHE_KEY_ARTICLE = "Article:";
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class BalanceConstants {
|
||||
// 余额变动场景
|
||||
public static final Integer BALANCE_RECHARGE = 10; // 用户充值
|
||||
public static final Integer BALANCE_USE = 20; // 用户消费
|
||||
public static final Integer BALANCE_RE_LET = 21; // 续租
|
||||
public static final Integer BALANCE_ADMIN = 30; // 管理员操作
|
||||
public static final Integer BALANCE_REFUND = 40; // 订单退款
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class BaseConstants {
|
||||
public static final String[] STATUS = {"未定义","显示","隐藏"};
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class DomainConstants {
|
||||
public static final String ROOT_DOMAIN = "websoft.top"; // 根域名
|
||||
public static final String PREFIX = "https://"; // 域名前缀
|
||||
public static final String ADMIN_SUFFIX = ".".concat(ROOT_DOMAIN); // 后台管理域名拼接
|
||||
public static final String WEB_SUFFIX = ".wsdns.cn"; // 应用域名拼接
|
||||
public static final String DOMAIN = PREFIX.concat(ROOT_DOMAIN); // 完整域名
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class OrderConstants {
|
||||
// 支付方式
|
||||
public static final String PAY_METHOD_BALANCE = "10"; // 余额支付
|
||||
public static final String PAY_METHOD_WX = "20"; // 微信支付
|
||||
public static final String PAY_METHOD_ALIPAY = "30"; // 支付宝支付
|
||||
public static final String PAY_METHOD_OTHER = "40"; // 其他支付
|
||||
|
||||
// 付款状态
|
||||
public static final Integer PAY_STATUS_NO_PAY = 10; // 未付款
|
||||
public static final Integer PAY_STATUS_SUCCESS = 20; // 已付款
|
||||
|
||||
// 发货状态
|
||||
public static final Integer DELIVERY_STATUS_NO = 10; // 未发货
|
||||
public static final Integer DELIVERY_STATUS_YES = 20; // 已发货
|
||||
public static final Integer DELIVERY_STATUS_30 = 30; // 部分发货
|
||||
|
||||
// 收货状态
|
||||
public static final Integer RECEIPT_STATUS_NO = 10; // 未收货
|
||||
public static final Integer RECEIPT_STATUS_YES = 20; // 已收货
|
||||
public static final Integer RECEIPT_STATUS_RETURN = 30; // 已退货
|
||||
|
||||
// 订单状态
|
||||
public static final Integer ORDER_STATUS_DOING = 10; // 进行中
|
||||
public static final Integer ORDER_STATUS_CANCEL = 20; // 已取消
|
||||
public static final Integer ORDER_STATUS_TO_CANCEL = 21; // 待取消
|
||||
public static final Integer ORDER_STATUS_COMPLETED = 30; // 已完成
|
||||
|
||||
// 订单结算状态
|
||||
public static final Integer ORDER_SETTLED_YES = 1; // 已结算
|
||||
public static final Integer ORDER_SETTLED_NO = 0; // 未结算
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class PlatformConstants {
|
||||
public static final String MP_OFFICIAL = "MP-OFFICIAL"; // 微信公众号
|
||||
public static final String MP_WEIXIN = "MP-WEIXIN"; // 微信小程序
|
||||
public static final String MP_ALIPAY = "MP-ALIPAY"; // 支付宝小程序
|
||||
public static final String WEB = "WEB"; // web(同H5)
|
||||
public static final String H5 = "H5"; // H5(推荐使用 WEB)
|
||||
public static final String APP = "APP"; // App
|
||||
public static final String MP_BAIDU = "MP-BAIDU"; // 百度小程序
|
||||
public static final String MP_TOUTIAO = "MP-TOUTIAO"; // 百度小程序
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class ProfitConstants {
|
||||
// 收益类型
|
||||
public static final Integer PROFIT_TYPE10 = 10; // 推广收益
|
||||
public static final Integer PROFIT_TYPE20 = 20; // 团队收益
|
||||
public static final Integer PROFIT_TYPE30 = 30; // 门店收益
|
||||
public static final Integer PROFIT_TYPE40 = 30; // 区域收益
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class QRCodeConstants {
|
||||
// 二维码类型
|
||||
public static final String USER_QRCODE = "user"; // 用户二维码
|
||||
public static final String TASK_QRCODE = "task"; // 工单二维码
|
||||
public static final String ARTICLE_QRCODE = "article"; // 文章二维码
|
||||
public static final String GOODS_QRCODE = "goods"; // 商品二维码
|
||||
public static final String DIY_QRCODE = "diy"; // 工单二维码
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class RedisConstants {
|
||||
// 短信验证码Key
|
||||
public static final String SMS_CODE_KEY = "sms";
|
||||
// 验证码过期时间
|
||||
public static final Long SMS_CODE_TTL = 5L;
|
||||
// 微信凭证access-token
|
||||
public static final String ACCESS_TOKEN_KEY = "access-token";
|
||||
// 空值防止击穿数据库
|
||||
public static final Long CACHE_NULL_TTL = 2L;
|
||||
// 商户信息
|
||||
public static final String MERCHANT_KEY = "merchant";
|
||||
// 添加商户定位点
|
||||
public static final String MERCHANT_GEO_KEY = "merchant-geo";
|
||||
|
||||
// token
|
||||
public static final String TOKEN_USER_ID = "cache:token:";
|
||||
// 排行榜
|
||||
public static final String USER_RANKING_BY_APPS = "userRankingByApps";
|
||||
// 搜索历史
|
||||
public static final String SEARCH_HISTORY = "searchHistory";
|
||||
// 租户系统设置信息
|
||||
public static final String TEN_ANT_SETTING_KEY = "setting";
|
||||
// 排行榜Key
|
||||
public static final String USER_RANKING_BY_APPS_5 = "cache5:userRankingByApps";
|
||||
|
||||
|
||||
|
||||
// 扫码登录相关key
|
||||
public static final String QR_LOGIN_TOKEN_KEY = "qr-login:token:"; // 扫码登录token前缀
|
||||
public static final Long QR_LOGIN_TOKEN_TTL = 300L; // 扫码登录token过期时间(5分钟)
|
||||
public static final String QR_LOGIN_STATUS_PENDING = "pending"; // 等待扫码
|
||||
public static final String QR_LOGIN_STATUS_SCANNED = "scanned"; // 已扫码
|
||||
public static final String QR_LOGIN_STATUS_CONFIRMED = "confirmed"; // 已确认
|
||||
public static final String QR_LOGIN_STATUS_BIND_PHONE = "bind_phone"; // 待绑定手机号
|
||||
public static final String QR_LOGIN_STATUS_EXPIRED = "expired"; // 已过期
|
||||
|
||||
// 哗啦啦key
|
||||
public static final String getAllShop = "allShop";
|
||||
public static final String getBaseInfo = "baseInfo";
|
||||
public static final String getFoodClassCategory = "foodCategory";
|
||||
public static final String getOpenFood = "openFood";
|
||||
public static final String haulalaGeoKey = "cache10:hualala-geo";
|
||||
public static final String HLL_CART_KEY = "hll-cart"; // hll-cart[shopId]:[userId]
|
||||
public static final String HLL_CART_FOOD_KEY = "hll-cart-list"; // hll-cart-list[shopId]:[userId]
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class TaskConstants {
|
||||
// 工单进度
|
||||
public static final Integer TOBEARRANGED = 0; // 待安排
|
||||
public static final Integer PENDING = 1; // 待处理
|
||||
public static final Integer PROCESSING = 2; // 处理中
|
||||
public static final Integer TOBECONFIRMED = 3; // 待评价
|
||||
public static final Integer COMPLETED = 4; // 已完成
|
||||
public static final Integer CLOSED = 5; // 已关闭
|
||||
|
||||
// 工单状态
|
||||
public static final Integer TASK_STATUS_0 = 0; // 待处理
|
||||
public static final Integer TASK_STATUS_1 = 1; // 已完成
|
||||
|
||||
// 操作类型
|
||||
public static final String ACTION_1 = "派单";
|
||||
public static final String ACTION_2 = "已解决";
|
||||
public static final String ACTION_3 = "关单";
|
||||
public static final String ACTION_4 = "分享";
|
||||
public static final String ACTION_5 = "编辑";
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.gxwebsoft.common.core.constants;
|
||||
|
||||
public class WebsiteConstants extends BaseConstants {
|
||||
// 运行状态 0未开通 1运行中 2维护中 3已关闭 4已欠费停机 5违规关停
|
||||
public static final String[] WEBSITE_STATUS_NAME = {"未开通","运行中","维护中","已关闭","已欠费停机","违规关停"};
|
||||
// 状态图标
|
||||
public static final String[] WEBSITE_STATUS_ICON = {"error","success","warning","error","error","error"};
|
||||
// 关闭原因
|
||||
public static final String[] WEBSITE_STATUS_TEXT = {"产品未开通","","系统升级维护","","已欠费停机","违规关停"};
|
||||
// 跳转地址
|
||||
public static final String[] WEBSITE_STATUS_URL = {"https://websoft.top","","","","https://websoft.top/user","https://websoft.top/user"};
|
||||
// 跳转按钮文字
|
||||
public static final String[] WEBSITE_STATUS_BTN_TEXT = {"立即开通","","","","立即续费","申请解封"};
|
||||
|
||||
|
||||
// 站点信息
|
||||
public static final String CACHE_KEY_ROOT_SITE_INFO = "RootSiteInfo:";
|
||||
|
||||
// 万能登录密码
|
||||
public static final String CACHE_KEY_UNIVERSAL_PASSWORD = "UniversalPassword:";
|
||||
|
||||
// 万能短信验证码:VerificationCodeByDevSMS
|
||||
public static final String CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS = "VerificationCodeByDevSMS:";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.gxwebsoft.common.core.context;
|
||||
|
||||
/**
|
||||
* 租户上下文管理器
|
||||
*
|
||||
* 用于在特定场景下临时禁用租户隔离
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-01-26
|
||||
*/
|
||||
public class TenantContext {
|
||||
|
||||
private static final ThreadLocal<Boolean> IGNORE_TENANT = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置忽略租户隔离
|
||||
*/
|
||||
public static void setIgnoreTenant(boolean ignore) {
|
||||
IGNORE_TENANT.set(ignore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略租户隔离
|
||||
*/
|
||||
public static boolean isIgnoreTenant() {
|
||||
Boolean ignore = IGNORE_TENANT.get();
|
||||
return ignore != null && ignore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除租户上下文
|
||||
*/
|
||||
public static void clear() {
|
||||
IGNORE_TENANT.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 在忽略租户隔离的上下文中执行操作
|
||||
*
|
||||
* @param runnable 要执行的操作
|
||||
*/
|
||||
public static void runIgnoreTenant(Runnable runnable) {
|
||||
boolean originalIgnore = isIgnoreTenant();
|
||||
try {
|
||||
setIgnoreTenant(true);
|
||||
runnable.run();
|
||||
} finally {
|
||||
setIgnoreTenant(originalIgnore);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在忽略租户隔离的上下文中执行操作并返回结果
|
||||
*
|
||||
* @param supplier 要执行的操作
|
||||
* @return 操作结果
|
||||
*/
|
||||
public static <T> T callIgnoreTenant(java.util.function.Supplier<T> supplier) {
|
||||
boolean originalIgnore = isIgnoreTenant();
|
||||
try {
|
||||
setIgnoreTenant(true);
|
||||
return supplier.get();
|
||||
} finally {
|
||||
setIgnoreTenant(originalIgnore);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package com.gxwebsoft.common.core.controller;
|
||||
|
||||
import com.gxwebsoft.common.core.service.CertificateHealthService;
|
||||
import com.gxwebsoft.common.core.service.CertificateService;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 证书管理控制器
|
||||
* 提供证书状态查询、健康检查等功能
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Tag(name = "证书管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/system/certificate")
|
||||
public class CertificateController extends BaseController {
|
||||
|
||||
@Resource
|
||||
private CertificateService certificateService;
|
||||
|
||||
@Resource
|
||||
private CertificateHealthService certificateHealthService;
|
||||
|
||||
@Operation(summary = "获取所有证书状态")
|
||||
@GetMapping("/status")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> getCertificateStatus() {
|
||||
try {
|
||||
Map<String, Object> status = certificateService.getAllCertificateStatus();
|
||||
return success("获取证书状态成功", status);
|
||||
} catch (Exception e) {
|
||||
log.error("获取证书状态失败", e);
|
||||
return new ApiResult<>(1, "获取证书状态失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "证书健康检查")
|
||||
@GetMapping("/health")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> healthCheck() {
|
||||
try {
|
||||
CertificateHealthService.HealthResult health = certificateHealthService.health();
|
||||
Map<String, Object> result = Map.of(
|
||||
"status", health.getStatus(),
|
||||
"details", health.getDetails()
|
||||
);
|
||||
return success("证书健康检查完成", result);
|
||||
} catch (Exception e) {
|
||||
log.error("证书健康检查失败", e);
|
||||
return new ApiResult<>(1, "证书健康检查失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取证书诊断信息")
|
||||
@GetMapping("/diagnostic")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> getDiagnosticInfo() {
|
||||
try {
|
||||
Map<String, Object> diagnostic = certificateHealthService.getDiagnosticInfo();
|
||||
return success("获取证书诊断信息成功", diagnostic);
|
||||
} catch (Exception e) {
|
||||
log.error("获取证书诊断信息失败", e);
|
||||
return new ApiResult<>(1, "获取证书诊断信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "检查特定证书")
|
||||
@GetMapping("/check/{certType}/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> checkSpecificCertificate(
|
||||
@Parameter(description = "证书类型", example = "wechat") @PathVariable String certType,
|
||||
@Parameter(description = "文件名", example = "apiclient_key.pem") @PathVariable String fileName) {
|
||||
try {
|
||||
Map<String, Object> result = certificateHealthService.checkSpecificCertificate(certType, fileName);
|
||||
return success("检查证书完成", result);
|
||||
} catch (Exception e) {
|
||||
log.error("检查证书失败: {}/{}", certType, fileName, e);
|
||||
return new ApiResult<>(1, "检查证书失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "验证证书文件")
|
||||
@GetMapping("/validate/{certType}/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<CertificateService.CertificateInfo> validateCertificate(
|
||||
@Parameter(description = "证书类型", example = "wechat") @PathVariable String certType,
|
||||
@Parameter(description = "文件名", example = "apiclient_cert.pem") @PathVariable String fileName) {
|
||||
try {
|
||||
CertificateService.CertificateInfo certInfo =
|
||||
certificateService.validateX509Certificate(certType, fileName);
|
||||
|
||||
if (certInfo != null) {
|
||||
return success("证书验证成功", certInfo);
|
||||
} else {
|
||||
return new ApiResult<>(1, "证书验证失败,可能不是有效的X509证书");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("验证证书失败: {}/{}", certType, fileName, e);
|
||||
return new ApiResult<>(1, "验证证书失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "检查证书文件是否存在")
|
||||
@GetMapping("/exists/{certType}/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Boolean> checkCertificateExists(
|
||||
@Parameter(description = "证书类型", example = "alipay") @PathVariable String certType,
|
||||
@Parameter(description = "文件名", example = "appCertPublicKey.crt") @PathVariable String fileName) {
|
||||
try {
|
||||
boolean exists = certificateService.certificateExists(certType, fileName);
|
||||
String message = exists ? "证书文件存在" : "证书文件不存在";
|
||||
return success(message, exists);
|
||||
} catch (Exception e) {
|
||||
log.error("检查证书文件存在性失败: {}/{}", certType, fileName, e);
|
||||
return new ApiResult<>(1, "检查证书文件存在性失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取证书文件路径")
|
||||
@GetMapping("/path/{certType}/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<String> getCertificatePath(
|
||||
@Parameter(description = "证书类型", example = "wechat") @PathVariable String certType,
|
||||
@Parameter(description = "文件名", example = "wechatpay_cert.pem") @PathVariable String fileName) {
|
||||
try {
|
||||
String path = certificateService.getCertificateFilePath(certType, fileName);
|
||||
return success("获取证书路径成功", path);
|
||||
} catch (Exception e) {
|
||||
log.error("获取证书路径失败: {}/{}", certType, fileName, e);
|
||||
return new ApiResult<>(1, "获取证书路径失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取微信支付证书路径")
|
||||
@GetMapping("/wechat-path/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<String> getWechatPayCertPath(
|
||||
@Parameter(description = "文件名", example = "apiclient_key.pem") @PathVariable String fileName) {
|
||||
try {
|
||||
String path = certificateService.getWechatPayCertPath(fileName);
|
||||
return success("获取微信支付证书路径成功", path);
|
||||
} catch (Exception e) {
|
||||
log.error("获取微信支付证书路径失败: {}", fileName, e);
|
||||
return new ApiResult<>(1, "获取微信支付证书路径失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取支付宝证书路径")
|
||||
@GetMapping("/alipay-path/{fileName}")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<String> getAlipayCertPath(
|
||||
@Parameter(description = "文件名", example = "appCertPublicKey.crt") @PathVariable String fileName) {
|
||||
try {
|
||||
String path = certificateService.getAlipayCertPath(fileName);
|
||||
return success("获取支付宝证书路径成功", path);
|
||||
} catch (Exception e) {
|
||||
log.error("获取支付宝证书路径失败: {}", fileName, e);
|
||||
return new ApiResult<>(1, "获取支付宝证书路径失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "检查数据库证书配置")
|
||||
@GetMapping("/database-check")
|
||||
@PreAuthorize("hasAuthority('system:certificate:view')")
|
||||
public ApiResult<Map<String, Object>> checkDatabaseCertificates() {
|
||||
try {
|
||||
Map<String, Object> result = certificateHealthService.checkDatabaseCertificates();
|
||||
return success("数据库证书检查完成", result);
|
||||
} catch (Exception e) {
|
||||
log.error("检查数据库证书配置失败", e);
|
||||
return new ApiResult<>(1, "检查数据库证书配置失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "刷新证书缓存")
|
||||
@PostMapping("/refresh")
|
||||
@PreAuthorize("hasAuthority('system:certificate:manage')")
|
||||
public ApiResult<String> refreshCertificateCache() {
|
||||
try {
|
||||
// 这里可以添加刷新证书缓存的逻辑
|
||||
log.info("证书缓存刷新请求,操作用户: {}", getLoginUser().getUsername());
|
||||
return new ApiResult<>(0, "证书缓存刷新成功", "success");
|
||||
} catch (Exception e) {
|
||||
log.error("刷新证书缓存失败", e);
|
||||
return new ApiResult<>(1, "刷新证书缓存失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package com.gxwebsoft.common.core.controller;
|
||||
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 日志分析控制器
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-01-20
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/log-analysis")
|
||||
@Tag(name = "日志分析", description = "日志分析和诊断接口")
|
||||
public class LogAnalysisController extends BaseController {
|
||||
|
||||
@Operation(summary = "分析系统日志")
|
||||
@GetMapping("/analyze")
|
||||
@PreAuthorize("hasAuthority('system:log:view')")
|
||||
public ApiResult<Map<String, Object>> analyzeSystemLogs(
|
||||
@Parameter(description = "查询的小时数,默认24小时") @RequestParam(defaultValue = "24") int hours,
|
||||
@Parameter(description = "日志级别过滤,如ERROR,WARN") @RequestParam(required = false) String level) {
|
||||
|
||||
try {
|
||||
Map<String, Object> analysis = new HashMap<>();
|
||||
|
||||
// 分析错误日志
|
||||
List<Map<String, Object>> errorLogs = analyzeErrorLogs(hours);
|
||||
analysis.put("errorLogs", errorLogs);
|
||||
|
||||
// 分析安全事件
|
||||
List<Map<String, Object>> securityEvents = analyzeSecurityEvents(hours);
|
||||
analysis.put("securityEvents", securityEvents);
|
||||
|
||||
// 分析性能问题
|
||||
List<Map<String, Object>> performanceIssues = analyzePerformanceIssues(hours);
|
||||
analysis.put("performanceIssues", performanceIssues);
|
||||
|
||||
// 统计信息
|
||||
Map<String, Integer> statistics = getLogStatistics(hours);
|
||||
analysis.put("statistics", statistics);
|
||||
|
||||
// 建议
|
||||
List<String> recommendations = generateRecommendations(errorLogs, securityEvents, performanceIssues);
|
||||
analysis.put("recommendations", recommendations);
|
||||
|
||||
return success("日志分析完成", analysis);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("分析系统日志失败", e);
|
||||
return new ApiResult<>(1, "分析日志失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取实时日志")
|
||||
@GetMapping("/real-time")
|
||||
@PreAuthorize("hasAuthority('system:log:view')")
|
||||
public ApiResult<List<String>> getRealTimeLogs(
|
||||
@Parameter(description = "获取的行数") @RequestParam(defaultValue = "100") int lines) {
|
||||
|
||||
try {
|
||||
List<String> recentLogs = getRecentLogLines(lines);
|
||||
return success("获取实时日志成功", recentLogs);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取实时日志失败", e);
|
||||
return new ApiResult<>(1, "获取实时日志失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "搜索日志")
|
||||
@GetMapping("/search")
|
||||
@PreAuthorize("hasAuthority('system:log:view')")
|
||||
public ApiResult<List<Map<String, Object>>> searchLogs(
|
||||
@Parameter(description = "搜索关键词") @RequestParam String keyword,
|
||||
@Parameter(description = "开始时间 yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) String startTime,
|
||||
@Parameter(description = "结束时间 yyyy-MM-dd HH:mm:ss") @RequestParam(required = false) String endTime) {
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> searchResults = searchLogsByKeyword(keyword, startTime, endTime);
|
||||
return success("搜索日志成功", searchResults);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("搜索日志失败", e);
|
||||
return new ApiResult<>(1, "搜索日志失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "清理旧日志")
|
||||
@PostMapping("/cleanup")
|
||||
@PreAuthorize("hasAuthority('system:log:delete')")
|
||||
public ApiResult<Map<String, Object>> cleanupOldLogs(
|
||||
@Parameter(description = "保留天数") @RequestParam(defaultValue = "30") int keepDays) {
|
||||
|
||||
try {
|
||||
Map<String, Object> result = performLogCleanup(keepDays);
|
||||
return success("日志清理完成", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("清理日志失败", e);
|
||||
return new ApiResult<>(1, "清理日志失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析错误日志
|
||||
*/
|
||||
private List<Map<String, Object>> analyzeErrorLogs(int hours) {
|
||||
List<Map<String, Object>> errorLogs = new ArrayList<>();
|
||||
// 这里实现错误日志分析逻辑
|
||||
// 可以读取日志文件,解析ERROR级别的日志
|
||||
return errorLogs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析安全事件
|
||||
*/
|
||||
private List<Map<String, Object>> analyzeSecurityEvents(int hours) {
|
||||
List<Map<String, Object>> securityEvents = new ArrayList<>();
|
||||
// 这里实现安全事件分析逻辑
|
||||
// 可以检查登录失败、权限拒绝等安全相关事件
|
||||
return securityEvents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析性能问题
|
||||
*/
|
||||
private List<Map<String, Object>> analyzePerformanceIssues(int hours) {
|
||||
List<Map<String, Object>> performanceIssues = new ArrayList<>();
|
||||
// 这里实现性能问题分析逻辑
|
||||
// 可以检查慢查询、长时间处理的请求等
|
||||
return performanceIssues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日志统计信息
|
||||
*/
|
||||
private Map<String, Integer> getLogStatistics(int hours) {
|
||||
Map<String, Integer> statistics = new HashMap<>();
|
||||
statistics.put("totalLogs", 0);
|
||||
statistics.put("errorCount", 0);
|
||||
statistics.put("warnCount", 0);
|
||||
statistics.put("infoCount", 0);
|
||||
// 这里实现统计逻辑
|
||||
return statistics;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成建议
|
||||
*/
|
||||
private List<String> generateRecommendations(List<Map<String, Object>> errorLogs,
|
||||
List<Map<String, Object>> securityEvents,
|
||||
List<Map<String, Object>> performanceIssues) {
|
||||
List<String> recommendations = new ArrayList<>();
|
||||
|
||||
if (!errorLogs.isEmpty()) {
|
||||
recommendations.add("检测到错误日志,建议检查系统异常情况");
|
||||
}
|
||||
|
||||
if (!securityEvents.isEmpty()) {
|
||||
recommendations.add("检测到安全事件,建议加强安全监控");
|
||||
}
|
||||
|
||||
if (!performanceIssues.isEmpty()) {
|
||||
recommendations.add("检测到性能问题,建议优化系统性能");
|
||||
}
|
||||
|
||||
if (recommendations.isEmpty()) {
|
||||
recommendations.add("系统运行正常,无异常发现");
|
||||
}
|
||||
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最近的日志行
|
||||
*/
|
||||
private List<String> getRecentLogLines(int lines) throws IOException {
|
||||
List<String> recentLogs = new ArrayList<>();
|
||||
File logFile = new File("logs/websoft-core.log");
|
||||
|
||||
if (logFile.exists()) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
|
||||
String line;
|
||||
LinkedList<String> buffer = new LinkedList<>();
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
buffer.add(line);
|
||||
if (buffer.size() > lines) {
|
||||
buffer.removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
recentLogs.addAll(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
return recentLogs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据关键词搜索日志
|
||||
*/
|
||||
private List<Map<String, Object>> searchLogsByKeyword(String keyword, String startTime, String endTime) {
|
||||
List<Map<String, Object>> results = new ArrayList<>();
|
||||
// 这里实现关键词搜索逻辑
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行日志清理
|
||||
*/
|
||||
private Map<String, Object> performLogCleanup(int keepDays) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("cleaned", false);
|
||||
result.put("message", "日志清理功能待实现");
|
||||
// 这里实现日志清理逻辑
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.gxwebsoft.common.core.controller;
|
||||
|
||||
import com.gxwebsoft.common.core.utils.WechatCertAutoConfig;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信支付证书自动配置测试控制器
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/wechat-cert-test")
|
||||
@Tag(name = "微信支付证书自动配置测试")
|
||||
public class WechatCertTestController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private WechatCertAutoConfig wechatCertAutoConfig;
|
||||
|
||||
@Operation(summary = "测试默认开发环境证书配置")
|
||||
@PostMapping("/test-default")
|
||||
public ApiResult<Map<String, Object>> testDefaultConfig() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
try {
|
||||
log.info("开始测试默认开发环境证书配置...");
|
||||
|
||||
// 创建自动证书配置
|
||||
Config config = wechatCertAutoConfig.createDefaultDevConfig();
|
||||
|
||||
// 测试配置
|
||||
boolean testResult = wechatCertAutoConfig.testConfig(config);
|
||||
|
||||
result.put("success", true);
|
||||
result.put("configCreated", config != null);
|
||||
result.put("testPassed", testResult);
|
||||
result.put("message", "默认证书配置测试完成");
|
||||
result.put("instructions", wechatCertAutoConfig.getUsageInstructions());
|
||||
|
||||
log.info("✅ 默认证书配置测试成功");
|
||||
return success("测试成功", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 默认证书配置测试失败: {}", e.getMessage(), e);
|
||||
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
result.put("message", "证书配置测试失败");
|
||||
result.put("troubleshooting", getTroubleshootingInfo());
|
||||
|
||||
return fail("测试失败: " + e.getMessage(), result);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "测试自定义证书配置")
|
||||
@PostMapping("/test-custom")
|
||||
public ApiResult<Map<String, Object>> testCustomConfig(
|
||||
@Parameter(description = "商户号") @RequestParam String merchantId,
|
||||
@Parameter(description = "私钥文件路径") @RequestParam String privateKeyPath,
|
||||
@Parameter(description = "证书序列号") @RequestParam String merchantSerialNumber,
|
||||
@Parameter(description = "APIv3密钥") @RequestParam String apiV3Key) {
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
try {
|
||||
log.info("开始测试自定义证书配置...");
|
||||
log.info("商户号: {}", merchantId);
|
||||
log.info("私钥路径: {}", privateKeyPath);
|
||||
|
||||
// 创建自动证书配置
|
||||
Config config = wechatCertAutoConfig.createAutoConfig(
|
||||
merchantId, privateKeyPath, merchantSerialNumber, apiV3Key);
|
||||
|
||||
// 测试配置
|
||||
boolean testResult = wechatCertAutoConfig.testConfig(config);
|
||||
|
||||
result.put("success", true);
|
||||
result.put("configCreated", config != null);
|
||||
result.put("testPassed", testResult);
|
||||
result.put("message", "自定义证书配置测试完成");
|
||||
result.put("merchantId", merchantId);
|
||||
result.put("privateKeyPath", privateKeyPath);
|
||||
|
||||
log.info("✅ 自定义证书配置测试成功");
|
||||
return success("测试成功", result);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 自定义证书配置测试失败: {}", e.getMessage(), e);
|
||||
|
||||
result.put("success", false);
|
||||
result.put("error", e.getMessage());
|
||||
result.put("message", "证书配置测试失败");
|
||||
result.put("troubleshooting", getTroubleshootingInfo());
|
||||
|
||||
return fail("测试失败: " + e.getMessage(), result);
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "获取使用说明")
|
||||
@GetMapping("/instructions")
|
||||
public ApiResult<String> getInstructions() {
|
||||
String instructions = wechatCertAutoConfig.getUsageInstructions();
|
||||
return success("获取使用说明成功", instructions);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取故障排除信息")
|
||||
@GetMapping("/troubleshooting")
|
||||
public ApiResult<Map<String, Object>> getTroubleshooting() {
|
||||
Map<String, Object> troubleshooting = getTroubleshootingInfo();
|
||||
return success("获取故障排除信息成功", troubleshooting);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取故障排除信息
|
||||
*/
|
||||
private Map<String, Object> getTroubleshootingInfo() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
|
||||
info.put("commonIssues", Map.of(
|
||||
"404错误", "商户平台未开启API安全功能或未申请使用微信支付公钥",
|
||||
"证书序列号错误", "请检查商户平台中的证书序列号是否正确",
|
||||
"APIv3密钥错误", "请确认APIv3密钥是否正确设置",
|
||||
"私钥文件不存在", "请检查私钥文件路径是否正确",
|
||||
"网络连接问题", "请检查网络连接是否正常"
|
||||
));
|
||||
|
||||
info.put("solutions", Map.of(
|
||||
"开启API安全", "登录微信商户平台 -> 账户中心 -> API安全 -> 申请使用微信支付公钥",
|
||||
"获取证书序列号", "在API安全页面查看或重新下载证书",
|
||||
"设置APIv3密钥", "在API安全页面设置APIv3密钥",
|
||||
"检查私钥文件", "确保apiclient_key.pem文件存在且路径正确"
|
||||
));
|
||||
|
||||
info.put("advantages", Map.of(
|
||||
"自动下载", "RSAAutoCertificateConfig会自动下载平台证书",
|
||||
"自动更新", "证书过期时会自动更新",
|
||||
"简化管理", "无需手动管理wechatpay_cert.pem文件",
|
||||
"官方推荐", "微信支付官方推荐的证书管理方式"
|
||||
));
|
||||
|
||||
info.put("documentation", "https://pay.weixin.qq.com/doc/v3/merchant/4012153196");
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.gxwebsoft.common.core.enums;
|
||||
|
||||
public enum ChatMessageType {
|
||||
TEXT( 1, "text"),
|
||||
IMAGE(2, "image"),
|
||||
VOICE(3, "voice"),
|
||||
CARD(4, "card"),
|
||||
;
|
||||
|
||||
private int index;
|
||||
|
||||
private String name;
|
||||
ChatMessageType(int i, String text) {
|
||||
this.name = text;
|
||||
this.index = i;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.gxwebsoft.common.core.enums;
|
||||
|
||||
public enum GreenWebType {
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
nickname_detection,
|
||||
/**
|
||||
* 聊天互动
|
||||
*/
|
||||
chat_detection,
|
||||
/**
|
||||
* 动态评论
|
||||
*/
|
||||
comment_detection,
|
||||
/**
|
||||
* 教学无聊
|
||||
*/
|
||||
pgc_detection,
|
||||
/**
|
||||
* 图片检测service: baselineCheck通用基线检测
|
||||
*/
|
||||
baselineCheck,
|
||||
/**
|
||||
* 视频检测
|
||||
*/
|
||||
videoDetection;
|
||||
|
||||
public enum ChatMessageType {
|
||||
TEXT( 1, "text"),
|
||||
IMAGE(2, "image"),
|
||||
VOICE(3, "voice"),
|
||||
CARD(4, "card"),
|
||||
;
|
||||
|
||||
private int index;
|
||||
|
||||
private String name;
|
||||
ChatMessageType(int i, String text) {
|
||||
this.name = text;
|
||||
this.index = i;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.gxwebsoft.common.core.exception;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
|
||||
/**
|
||||
* 自定义业务异常
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-02-22 11:29:28
|
||||
*/
|
||||
public class BusinessException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer code;
|
||||
|
||||
public BusinessException() {
|
||||
this(Constants.RESULT_ERROR_MSG);
|
||||
}
|
||||
|
||||
public BusinessException(String message) {
|
||||
this(Constants.RESULT_ERROR_CODE, message);
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public BusinessException(Integer code, String message, Throwable cause,
|
||||
boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.gxwebsoft.common.core.exception;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-02-22 11:29:30
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public ApiResult<?> methodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e,
|
||||
HttpServletResponse response) {
|
||||
CommonUtil.addCrossHeaders(response);
|
||||
return new ApiResult<>(Constants.RESULT_ERROR_CODE, "请求方式不正确").setError(e.toString());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public ApiResult<?> accessDeniedExceptionHandler(AccessDeniedException e, HttpServletResponse response) {
|
||||
CommonUtil.addCrossHeaders(response);
|
||||
return new ApiResult<>(Constants.UNAUTHORIZED_CODE, Constants.UNAUTHORIZED_MSG).setError(e.toString());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public ApiResult<?> businessExceptionHandler(BusinessException e, HttpServletResponse response) {
|
||||
CommonUtil.addCrossHeaders(response);
|
||||
return new ApiResult<>(e.getCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public ApiResult<?> exceptionHandler(Throwable e, HttpServletResponse response) {
|
||||
logger.error(e.getMessage(), e);
|
||||
CommonUtil.addCrossHeaders(response);
|
||||
return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG).setError(e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 没有访问权限异常处理
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-25 00:35:03
|
||||
*/
|
||||
@Component
|
||||
public class JwtAccessDeniedHandler implements AccessDeniedHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e)
|
||||
throws IOException, ServletException {
|
||||
CommonUtil.responseError(response, Constants.UNAUTHORIZED_CODE, Constants.UNAUTHORIZED_MSG, e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 没有登录异常处理
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-25 00:35:03
|
||||
*/
|
||||
@Component
|
||||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
|
||||
throws IOException, ServletException {
|
||||
CommonUtil.responseError(response, Constants.UNAUTHENTICATED_CODE, Constants.UNAUTHENTICATED_MSG,
|
||||
e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import com.gxwebsoft.common.core.utils.LogAnalysisUtil;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.core.utils.SignCheckUtil;
|
||||
import com.gxwebsoft.common.system.entity.LoginRecord;
|
||||
import com.gxwebsoft.common.system.entity.Menu;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.LoginRecordService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.ExpiredJwtException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 处理携带token的请求过滤器
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-30 20:48:05
|
||||
*/
|
||||
@Component
|
||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
@Resource
|
||||
private LoginRecordService loginRecordService;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
||||
throws ServletException, IOException {
|
||||
String access_token = JwtUtil.getAccessToken(request);
|
||||
if (StrUtil.isNotBlank(access_token)) {
|
||||
try {
|
||||
// 解析token
|
||||
Claims claims = JwtUtil.parseToken(access_token, configProperties.getTokenKey());
|
||||
JwtSubject jwtSubject = JwtUtil.getJwtSubject(claims);
|
||||
|
||||
// 校验服务器域名白名单
|
||||
final SignCheckUtil checkUtil = new SignCheckUtil();
|
||||
String key = "WhiteDomain:" + jwtSubject.getTenantId();
|
||||
List<String> whiteDomains = redisUtil.get(key, List.class);
|
||||
if (!checkUtil.checkWhiteDomains(whiteDomains, request.getServerName()) && !"localhost".equals(request.getServerName()) && !"server.gxwebsoft.com".equals(request.getServerName())) {
|
||||
throw new UsernameNotFoundException("The requested domain name is not on the whitelist");
|
||||
}
|
||||
|
||||
User user = userService.getByUsername(jwtSubject.getUsername(), jwtSubject.getTenantId());
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("Username not found");
|
||||
}
|
||||
List<Menu> authorities = user.getAuthorities().stream()
|
||||
.filter(m -> StrUtil.isNotBlank(m.getAuthority())).collect(Collectors.toList());
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
user, null, authorities);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
// token将要过期签发新token, 防止突然退出登录
|
||||
long expiration = (claims.getExpiration().getTime() - new Date().getTime()) / 1000 / 60;
|
||||
if (expiration < configProperties.getTokenRefreshTime()) {
|
||||
String token = JwtUtil.buildToken(jwtSubject, configProperties.getTokenExpireTime(),
|
||||
configProperties.getTokenKey());
|
||||
response.addHeader(Constants.TOKEN_HEADER_NAME, token);
|
||||
loginRecordService.saveAsync(user.getUsername(), LoginRecord.TYPE_REFRESH, null,
|
||||
user.getTenantId(), request);
|
||||
}
|
||||
} catch (ExpiredJwtException e) {
|
||||
LogAnalysisUtil.logSecurityEvent("JWT_TOKEN_EXPIRED", "unknown", e.getMessage(), request);
|
||||
CommonUtil.responseError(response, Constants.TOKEN_EXPIRED_CODE, Constants.TOKEN_EXPIRED_MSG,
|
||||
e.getMessage());
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
LogAnalysisUtil.logSecurityEvent("JWT_AUTHENTICATION_FAILED", "unknown", e.getMessage(), request);
|
||||
LogAnalysisUtil.logExceptionDetails(e, "JWT认证过程");
|
||||
CommonUtil.responseError(response, Constants.BAD_CREDENTIALS_CODE, Constants.BAD_CREDENTIALS_MSG,
|
||||
e.toString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Jwt载体
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-09-03 00:11:12
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class JwtSubject implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 租户id
|
||||
*/
|
||||
private Integer tenantId;
|
||||
|
||||
}
|
||||
141
src/main/java/com/gxwebsoft/common/core/security/JwtUtil.java
Normal file
141
src/main/java/com/gxwebsoft/common/core/security/JwtUtil.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.utils.JSONUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.io.Decoders;
|
||||
import io.jsonwebtoken.io.Encoders;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.security.Key;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* JWT工具类
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-01-21 16:30:59
|
||||
*/
|
||||
public class JwtUtil {
|
||||
|
||||
/**
|
||||
* 获取请求中的access_token
|
||||
*
|
||||
* @param request HttpServletRequest
|
||||
* @return String
|
||||
*/
|
||||
public static String getAccessToken(HttpServletRequest request) {
|
||||
String access_token = ServletUtil.getHeaderIgnoreCase(request, Constants.TOKEN_HEADER_NAME);
|
||||
if (StrUtil.isNotBlank(access_token)) {
|
||||
if (access_token.startsWith(Constants.TOKEN_TYPE)) {
|
||||
access_token = StrUtil.removePrefix(access_token, Constants.TOKEN_TYPE).trim();
|
||||
}
|
||||
} else {
|
||||
access_token = request.getParameter(Constants.TOKEN_PARAM_NAME);
|
||||
}
|
||||
return access_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token
|
||||
*
|
||||
* @param subject 载体
|
||||
* @param expire 过期时间
|
||||
* @param base64EncodedKey base64编码的Key
|
||||
* @return token
|
||||
*/
|
||||
public static String buildToken(JwtSubject subject, Long expire, String base64EncodedKey) {
|
||||
return buildToken(JSONUtil.toJSONString(subject), expire, decodeKey(base64EncodedKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成token
|
||||
*
|
||||
* @param subject 载体
|
||||
* @param expire 过期时间
|
||||
* @param key 密钥
|
||||
* @return token
|
||||
*/
|
||||
public static String buildToken(String subject, Long expire, Key key) {
|
||||
Date expireDate = new Date(new Date().getTime() + 1000 * expire);
|
||||
return Jwts.builder()
|
||||
.setSubject(subject)
|
||||
.setExpiration(expireDate)
|
||||
.setIssuedAt(new Date())
|
||||
.signWith(key)
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析token
|
||||
*
|
||||
* @param token token
|
||||
* @param base64EncodedKey base64编码的Key
|
||||
* @return Claims
|
||||
*/
|
||||
public static Claims parseToken(String token, String base64EncodedKey) {
|
||||
return parseToken(token, decodeKey(base64EncodedKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析token
|
||||
*
|
||||
* @param token token
|
||||
* @param key 密钥
|
||||
* @return Claims
|
||||
*/
|
||||
public static Claims parseToken(String token, Key key) {
|
||||
return Jwts.parserBuilder()
|
||||
.setSigningKey(key)
|
||||
.build()
|
||||
.parseClaimsJws(token)
|
||||
.getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取JwtSubject
|
||||
*
|
||||
* @param claims Claims
|
||||
* @return JwtSubject
|
||||
*/
|
||||
public static JwtSubject getJwtSubject(Claims claims) {
|
||||
return JSONUtil.parseObject(claims.getSubject(), JwtSubject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成Key
|
||||
*
|
||||
* @return Key
|
||||
*/
|
||||
public static Key randomKey() {
|
||||
return Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
||||
}
|
||||
|
||||
/**
|
||||
* base64编码key
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String encodeKey(Key key) {
|
||||
return Encoders.BASE64.encode(key.getEncoded());
|
||||
}
|
||||
|
||||
/**
|
||||
* base64编码Key
|
||||
*
|
||||
* @param base64EncodedKey base64编码的key
|
||||
* @return Key
|
||||
*/
|
||||
public static Key decodeKey(String base64EncodedKey) {
|
||||
if (StrUtil.isBlank(base64EncodedKey)) {
|
||||
return null;
|
||||
}
|
||||
return Keys.hmacShaKeyFor(Decoders.BASE64.decode(base64EncodedKey));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.gxwebsoft.common.core.security;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* Spring Security配置
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-23 18:04:52
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Resource
|
||||
private JwtAccessDeniedHandler jwtAccessDeniedHandler;
|
||||
@Resource
|
||||
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
|
||||
@Resource
|
||||
private JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers(HttpMethod.OPTIONS, "/**")
|
||||
.permitAll()
|
||||
.antMatchers(HttpMethod.GET, "/api/file/**","/**", "/api/captcha", "/")
|
||||
.permitAll()
|
||||
.antMatchers(
|
||||
"/api/login",
|
||||
"/api/qr-login/**",
|
||||
"/api/loginByUserId",
|
||||
"/api/register",
|
||||
"/api/superAdminRegister",
|
||||
"/api/findAccountByPhone",
|
||||
"/api/resetPassword",
|
||||
"/api/checkPhoneRegistered",
|
||||
"/api/existence",
|
||||
"/api/oss/upload",
|
||||
"/druid/**",
|
||||
"/swagger-resources/**",
|
||||
"/webjars/**",
|
||||
"/hxz/v1/**",
|
||||
"/api/sendSmsCaptcha",
|
||||
"/api/loginBySms",
|
||||
"/api/system/user/regByPhone",
|
||||
"/api/parseToken/*",
|
||||
"/api/login-alipay/*",
|
||||
"/api/wx-login/loginByMpWxPhone",
|
||||
"/api/wx-login/getAccessToken",
|
||||
"/api/wx-login/loginByOpenId",
|
||||
"/api/wx-login/getOpenId",
|
||||
"/api/wx-login/getWxOpenIdOnly",
|
||||
"/api/system/wx-native-pay/**",
|
||||
"/api/system/wx-pay/**",
|
||||
"/api/wxWorkQrConnect",
|
||||
"/api/sys/user-plan-log/wx-pay/**",
|
||||
"/api/wx-official/**",
|
||||
"/api/system/user/loginByPhoneForTest",
|
||||
"/api/system/user/updateUserBalanceWithoutLogin",
|
||||
"/api/system/user/addUserBalanceWithoutLogin",
|
||||
"/api/system/user/getUserWithoutLogin",
|
||||
"/api/system/user/batchBackUserId",
|
||||
"/api/system/user/getByPhone/**",
|
||||
"/api/system/user/getByUserId/**",
|
||||
"/api/system/user/getByUnionid/**",
|
||||
"/api/system/user/updateUserOfficeOpenidWithoutLogin",
|
||||
"/api/system/user/updateWithoutLogin",
|
||||
"/api/system/user-referee/getReferee/**",
|
||||
"/api/system/dict-data/page",
|
||||
"/api/system/organization",
|
||||
"/api/system/tenant/saveByPhone",
|
||||
"/api/system/user-referee/getRefereeNum",
|
||||
"/api/system/user-referee/getRefereeNumByUidList",
|
||||
"/api/system/setting/getByKey/**",
|
||||
"/api/system/setting/updateByKey/**",
|
||||
"/lvQ4EoivKJ.txt",
|
||||
"/api/wechat-cert-test",
|
||||
"/MP_verify_joj96VBHPtL9YROj.txt"
|
||||
)
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.sessionManagement()
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
.and()
|
||||
.csrf()
|
||||
.disable()
|
||||
.cors()
|
||||
.and()
|
||||
.logout()
|
||||
.disable()
|
||||
.headers()
|
||||
.frameOptions()
|
||||
.disable()
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.accessDeniedHandler(jwtAccessDeniedHandler)
|
||||
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
|
||||
.and()
|
||||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder bCryptPasswordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,423 @@
|
||||
package com.gxwebsoft.common.core.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import com.gxwebsoft.common.system.service.PaymentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 证书健康检查服务
|
||||
* 提供证书状态检查和健康监控功能
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CertificateHealthService {
|
||||
|
||||
private final CertificateService certificateService;
|
||||
private final CertificateProperties certificateProperties;
|
||||
|
||||
@Resource
|
||||
private PaymentService paymentService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
@Value("${spring.profiles.active:dev}")
|
||||
private String active;
|
||||
|
||||
public CertificateHealthService(CertificateService certificateService,
|
||||
CertificateProperties certificateProperties) {
|
||||
this.certificateService = certificateService;
|
||||
this.certificateProperties = certificateProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户ID
|
||||
*/
|
||||
private Integer getCurrentTenantId() {
|
||||
try {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.getPrincipal() instanceof com.gxwebsoft.common.system.entity.User) {
|
||||
return ((com.gxwebsoft.common.system.entity.User) authentication.getPrincipal()).getTenantId();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取当前租户ID失败: {}", e.getMessage());
|
||||
}
|
||||
return 1; // 默认租户ID
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义健康检查结果类
|
||||
*/
|
||||
public static class HealthResult {
|
||||
private final String status;
|
||||
private final Map<String, Object> details;
|
||||
|
||||
public HealthResult(String status, Map<String, Object> details) {
|
||||
this.status = status;
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public static HealthResult up(Map<String, Object> details) {
|
||||
return new HealthResult("UP", details);
|
||||
}
|
||||
|
||||
public static HealthResult down(Map<String, Object> details) {
|
||||
return new HealthResult("DOWN", details);
|
||||
}
|
||||
}
|
||||
|
||||
public HealthResult health() {
|
||||
try {
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
boolean allHealthy = true;
|
||||
|
||||
// 检查微信支付证书(配置文件模式)
|
||||
Map<String, Object> wechatHealth = checkWechatPayCertificates();
|
||||
details.put("wechatPay", wechatHealth);
|
||||
if (!(Boolean) wechatHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 检查支付宝证书(配置文件模式)
|
||||
Map<String, Object> alipayHealth = checkAlipayCertificates();
|
||||
details.put("alipay", alipayHealth);
|
||||
if (!(Boolean) alipayHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 检查数据库中的支付配置证书路径
|
||||
Map<String, Object> databaseCertHealth = checkDatabaseCertificates();
|
||||
details.put("databaseCertificates", databaseCertHealth);
|
||||
if (!(Boolean) databaseCertHealth.get("healthy")) {
|
||||
allHealthy = false;
|
||||
}
|
||||
|
||||
// 添加系统信息
|
||||
details.put("loadMode", certificateProperties.getLoadMode());
|
||||
details.put("certRootPath", certificateProperties.getCertRootPath());
|
||||
details.put("currentEnvironment", active);
|
||||
|
||||
if (allHealthy) {
|
||||
return HealthResult.up(details);
|
||||
} else {
|
||||
return HealthResult.down(details);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("证书健康检查失败", e);
|
||||
Map<String, Object> errorDetails = new HashMap<>();
|
||||
errorDetails.put("error", e.getMessage());
|
||||
return HealthResult.down(errorDetails);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查微信支付证书健康状态
|
||||
*/
|
||||
private Map<String, Object> checkWechatPayCertificates() {
|
||||
Map<String, Object> health = new HashMap<>();
|
||||
boolean healthy = true;
|
||||
Map<String, Object> certificates = new HashMap<>();
|
||||
|
||||
CertificateProperties.WechatPayConfig wechatConfig = certificateProperties.getWechatPay();
|
||||
|
||||
// 检查私钥证书
|
||||
String privateKeyFile = wechatConfig.getDev().getPrivateKeyFile();
|
||||
boolean privateKeyExists = certificateService.certificateExists("wechat", privateKeyFile);
|
||||
certificates.put("privateKey", Map.of(
|
||||
"file", privateKeyFile,
|
||||
"exists", privateKeyExists,
|
||||
"path", certificateService.getWechatPayCertPath(privateKeyFile)
|
||||
));
|
||||
if (!privateKeyExists) healthy = false;
|
||||
|
||||
// 检查商户证书
|
||||
String apiclientCertFile = wechatConfig.getDev().getApiclientCertFile();
|
||||
boolean apiclientCertExists = certificateService.certificateExists("wechat", apiclientCertFile);
|
||||
certificates.put("apiclientCert", Map.of(
|
||||
"file", apiclientCertFile,
|
||||
"exists", apiclientCertExists,
|
||||
"path", certificateService.getWechatPayCertPath(apiclientCertFile)
|
||||
));
|
||||
if (!apiclientCertExists) healthy = false;
|
||||
|
||||
// 检查微信支付平台证书
|
||||
String wechatpayCertFile = wechatConfig.getDev().getWechatpayCertFile();
|
||||
boolean wechatpayCertExists = certificateService.certificateExists("wechat", wechatpayCertFile);
|
||||
certificates.put("wechatpayCert", Map.of(
|
||||
"file", wechatpayCertFile,
|
||||
"exists", wechatpayCertExists,
|
||||
"path", certificateService.getWechatPayCertPath(wechatpayCertFile)
|
||||
));
|
||||
if (!wechatpayCertExists) healthy = false;
|
||||
|
||||
health.put("healthy", healthy);
|
||||
health.put("certificates", certificates);
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查支付宝证书健康状态
|
||||
*/
|
||||
private Map<String, Object> checkAlipayCertificates() {
|
||||
Map<String, Object> health = new HashMap<>();
|
||||
boolean healthy = true;
|
||||
Map<String, Object> certificates = new HashMap<>();
|
||||
|
||||
CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();
|
||||
|
||||
// 检查应用私钥
|
||||
String appPrivateKeyFile = alipayConfig.getAppPrivateKeyFile();
|
||||
boolean appPrivateKeyExists = certificateService.certificateExists("alipay", appPrivateKeyFile);
|
||||
certificates.put("appPrivateKey", Map.of(
|
||||
"file", appPrivateKeyFile,
|
||||
"exists", appPrivateKeyExists,
|
||||
"path", certificateService.getAlipayCertPath(appPrivateKeyFile)
|
||||
));
|
||||
if (!appPrivateKeyExists) healthy = false;
|
||||
|
||||
// 检查应用公钥证书
|
||||
String appCertPublicKeyFile = alipayConfig.getAppCertPublicKeyFile();
|
||||
boolean appCertExists = certificateService.certificateExists("alipay", appCertPublicKeyFile);
|
||||
certificates.put("appCertPublicKey", Map.of(
|
||||
"file", appCertPublicKeyFile,
|
||||
"exists", appCertExists,
|
||||
"path", certificateService.getAlipayCertPath(appCertPublicKeyFile)
|
||||
));
|
||||
if (!appCertExists) healthy = false;
|
||||
|
||||
// 检查支付宝公钥证书
|
||||
String alipayCertPublicKeyFile = alipayConfig.getAlipayCertPublicKeyFile();
|
||||
boolean alipayCertExists = certificateService.certificateExists("alipay", alipayCertPublicKeyFile);
|
||||
certificates.put("alipayCertPublicKey", Map.of(
|
||||
"file", alipayCertPublicKeyFile,
|
||||
"exists", alipayCertExists,
|
||||
"path", certificateService.getAlipayCertPath(alipayCertPublicKeyFile)
|
||||
));
|
||||
if (!alipayCertExists) healthy = false;
|
||||
|
||||
// 检查支付宝根证书
|
||||
String alipayRootCertFile = alipayConfig.getAlipayRootCertFile();
|
||||
boolean rootCertExists = certificateService.certificateExists("alipay", alipayRootCertFile);
|
||||
certificates.put("alipayRootCert", Map.of(
|
||||
"file", alipayRootCertFile,
|
||||
"exists", rootCertExists,
|
||||
"path", certificateService.getAlipayCertPath(alipayRootCertFile)
|
||||
));
|
||||
if (!rootCertExists) healthy = false;
|
||||
|
||||
health.put("healthy", healthy);
|
||||
health.put("certificates", certificates);
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详细的证书诊断信息
|
||||
*/
|
||||
public Map<String, Object> getDiagnosticInfo() {
|
||||
Map<String, Object> diagnostic = new HashMap<>();
|
||||
|
||||
try {
|
||||
// 基本系统信息
|
||||
diagnostic.put("loadMode", certificateProperties.getLoadMode());
|
||||
diagnostic.put("certRootPath", certificateProperties.getCertRootPath());
|
||||
diagnostic.put("devCertPath", certificateProperties.getDevCertPath());
|
||||
|
||||
// 获取所有证书状态
|
||||
diagnostic.put("certificateStatus", certificateService.getAllCertificateStatus());
|
||||
|
||||
// 健康检查结果
|
||||
HealthResult health = health();
|
||||
diagnostic.put("healthStatus", health.getStatus());
|
||||
diagnostic.put("healthDetails", health.getDetails());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("获取证书诊断信息失败", e);
|
||||
diagnostic.put("error", e.getMessage());
|
||||
}
|
||||
|
||||
return diagnostic;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查特定证书的详细信息
|
||||
*/
|
||||
public Map<String, Object> checkSpecificCertificate(String certType, String fileName) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
try {
|
||||
boolean exists = certificateService.certificateExists(certType, fileName);
|
||||
String path = certificateService.getCertificateFilePath(certType, fileName);
|
||||
|
||||
result.put("certType", certType);
|
||||
result.put("fileName", fileName);
|
||||
result.put("exists", exists);
|
||||
result.put("path", path);
|
||||
|
||||
if (exists && (fileName.endsWith(".crt") || fileName.endsWith(".pem"))) {
|
||||
// 尝试验证证书
|
||||
CertificateService.CertificateInfo certInfo =
|
||||
certificateService.validateX509Certificate(certType, fileName);
|
||||
result.put("certificateInfo", certInfo);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("检查证书失败: {}/{}", certType, fileName, e);
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查数据库中存储的证书路径
|
||||
*/
|
||||
public Map<String, Object> checkDatabaseCertificates() {
|
||||
Map<String, Object> health = new HashMap<>();
|
||||
boolean healthy = true;
|
||||
Map<String, Object> certificates = new HashMap<>();
|
||||
|
||||
try {
|
||||
Integer tenantId = getCurrentTenantId();
|
||||
log.info("检查租户 {} 的数据库证书配置", tenantId);
|
||||
|
||||
// 查询微信支付配置
|
||||
List<Payment> wechatPayments = paymentService.list(
|
||||
new LambdaQueryWrapper<Payment>()
|
||||
.eq(Payment::getCode, "wxPay")
|
||||
.eq(Payment::getStatus, true)
|
||||
.eq(Payment::getTenantId, tenantId)
|
||||
);
|
||||
|
||||
Map<String, Object> wechatDbCerts = new HashMap<>();
|
||||
if (!wechatPayments.isEmpty()) {
|
||||
Payment wechatPayment = wechatPayments.get(0);
|
||||
log.info("找到微信支付配置: 商户号={}, 序列号={}", wechatPayment.getMchId(), wechatPayment.getMerchantSerialNumber());
|
||||
|
||||
// 检查微信支付证书路径
|
||||
String apiclientKey = wechatPayment.getApiclientKey();
|
||||
String apiclientCert = wechatPayment.getApiclientCert();
|
||||
|
||||
if (apiclientKey != null && !apiclientKey.isEmpty()) {
|
||||
String keyPath = getAbsoluteCertPath(apiclientKey);
|
||||
boolean keyExists = new File(keyPath).exists();
|
||||
wechatDbCerts.put("privateKey", Map.of(
|
||||
"relativePath", apiclientKey,
|
||||
"absolutePath", keyPath,
|
||||
"exists", keyExists
|
||||
));
|
||||
log.info("微信支付私钥证书 - 相对路径: {}, 绝对路径: {}, 存在: {}", apiclientKey, keyPath, keyExists);
|
||||
if (!keyExists) healthy = false;
|
||||
} else {
|
||||
wechatDbCerts.put("privateKey", Map.of("error", "私钥路径未配置"));
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
if (apiclientCert != null && !apiclientCert.isEmpty()) {
|
||||
String certPath = getAbsoluteCertPath(apiclientCert);
|
||||
boolean certExists = new File(certPath).exists();
|
||||
wechatDbCerts.put("certificate", Map.of(
|
||||
"relativePath", apiclientCert,
|
||||
"absolutePath", certPath,
|
||||
"exists", certExists
|
||||
));
|
||||
log.info("微信支付证书 - 相对路径: {}, 绝对路径: {}, 存在: {}", apiclientCert, certPath, certExists);
|
||||
if (!certExists) healthy = false;
|
||||
} else {
|
||||
wechatDbCerts.put("certificate", Map.of("error", "证书路径未配置"));
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
wechatDbCerts.put("merchantId", wechatPayment.getMchId());
|
||||
wechatDbCerts.put("serialNumber", wechatPayment.getMerchantSerialNumber());
|
||||
wechatDbCerts.put("apiV3Key", wechatPayment.getApiKey() != null ? "已配置" : "未配置");
|
||||
|
||||
} else {
|
||||
wechatDbCerts.put("error", "未找到微信支付配置");
|
||||
healthy = false;
|
||||
log.warn("租户 {} 未找到微信支付配置", tenantId);
|
||||
}
|
||||
|
||||
certificates.put("wechatPay", wechatDbCerts);
|
||||
|
||||
// 查询支付宝配置(如果有的话)
|
||||
List<Payment> alipayPayments = paymentService.list(
|
||||
new LambdaQueryWrapper<Payment>()
|
||||
.eq(Payment::getCode, "alipay")
|
||||
.eq(Payment::getStatus, true)
|
||||
.eq(Payment::getTenantId, tenantId)
|
||||
);
|
||||
|
||||
Map<String, Object> alipayDbCerts = new HashMap<>();
|
||||
if (!alipayPayments.isEmpty()) {
|
||||
Payment alipayPayment = alipayPayments.get(0);
|
||||
log.info("找到支付宝配置: 应用ID={}", alipayPayment.getAppId());
|
||||
|
||||
// 这里可以添加支付宝证书路径检查逻辑
|
||||
alipayDbCerts.put("appId", alipayPayment.getAppId());
|
||||
alipayDbCerts.put("status", "支付宝配置存在,但证书路径检查需要根据具体字段实现");
|
||||
} else {
|
||||
alipayDbCerts.put("status", "未找到支付宝配置");
|
||||
log.info("租户 {} 未找到支付宝配置", tenantId);
|
||||
}
|
||||
|
||||
certificates.put("alipay", alipayDbCerts);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("检查数据库证书配置失败", e);
|
||||
certificates.put("error", e.getMessage());
|
||||
healthy = false;
|
||||
}
|
||||
|
||||
health.put("healthy", healthy);
|
||||
health.put("certificates", certificates);
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书的完整绝对路径
|
||||
*/
|
||||
private String getAbsoluteCertPath(String relativePath) {
|
||||
if (relativePath == null || relativePath.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// 如果是生产环境,证书存储在上传目录
|
||||
if (!"dev".equals(active)) {
|
||||
String uploadPath = configProperties.getUploadPath();
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
String fullPath = uploadPath + "file" + relativePath;
|
||||
log.debug("生产环境证书路径构建 - 上传根路径: {}, 相对路径: {}, 完整路径: {}",
|
||||
uploadPath, relativePath, fullPath);
|
||||
return fullPath;
|
||||
} else {
|
||||
// 开发环境,可能需要不同的处理逻辑
|
||||
log.debug("开发环境证书路径: {}", relativePath);
|
||||
return relativePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
package com.gxwebsoft.common.core.service;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 证书管理服务
|
||||
* 负责处理不同环境下的证书加载、验证和管理
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CertificateService {
|
||||
|
||||
private final CertificateProperties certificateProperties;
|
||||
|
||||
public CertificateService(CertificateProperties certificateProperties) {
|
||||
this.certificateProperties = certificateProperties;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("证书服务初始化,当前加载模式: {}", certificateProperties.getLoadMode());
|
||||
log.info("证书根路径: {}", certificateProperties.getCertRootPath());
|
||||
|
||||
// 检查证书目录和文件
|
||||
checkCertificateDirectories();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书文件的输入流
|
||||
*
|
||||
* @param certType 证书类型(wechat/alipay)
|
||||
* @param fileName 文件名
|
||||
* @return 输入流
|
||||
* @throws IOException 文件读取异常
|
||||
*/
|
||||
public InputStream getCertificateInputStream(String certType, String fileName) throws IOException {
|
||||
String certPath = certificateProperties.getCertificatePath(certType, fileName);
|
||||
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
// 从classpath加载
|
||||
Resource resource = new ClassPathResource(certPath);
|
||||
if (!resource.exists()) {
|
||||
throw new IOException("证书文件不存在: " + certPath);
|
||||
}
|
||||
log.debug("从classpath加载证书: {}", certPath);
|
||||
return resource.getInputStream();
|
||||
} else {
|
||||
// 从文件系统加载
|
||||
File file = new File(certPath);
|
||||
if (!file.exists()) {
|
||||
throw new IOException("证书文件不存在: " + certPath);
|
||||
}
|
||||
log.debug("从文件系统加载证书: {}", certPath);
|
||||
return Files.newInputStream(file.toPath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书文件路径
|
||||
*
|
||||
* @param certType 证书类型
|
||||
* @param fileName 文件名
|
||||
* @return 文件路径
|
||||
*/
|
||||
public String getCertificateFilePath(String certType, String fileName) {
|
||||
return certificateProperties.getCertificatePath(certType, fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书文件是否存在
|
||||
*
|
||||
* @param certType 证书类型
|
||||
* @param fileName 文件名
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean certificateExists(String certType, String fileName) {
|
||||
try {
|
||||
String certPath = certificateProperties.getCertificatePath(certType, fileName);
|
||||
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
Resource resource = new ClassPathResource(certPath);
|
||||
return resource.exists();
|
||||
} else {
|
||||
File file = new File(certPath);
|
||||
return file.exists() && file.isFile();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("检查证书文件存在性时出错: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信支付证书路径
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 证书路径
|
||||
*/
|
||||
public String getWechatPayCertPath(String fileName) {
|
||||
String certPath = certificateProperties.getWechatPayCertPath(fileName);
|
||||
log.debug("获取微信支付证书路径 - 文件名: {}, 路径: {}", fileName, certPath);
|
||||
|
||||
// 打印完整的绝对路径信息
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("微信支付证书路径模式: CLASSPATH");
|
||||
log.info("微信支付证书相对路径: {}", certPath);
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(certPath);
|
||||
if (resource.exists()) {
|
||||
String absolutePath = resource.getFile().getAbsolutePath();
|
||||
log.info("微信支付证书完整绝对路径: {}", absolutePath);
|
||||
} else {
|
||||
log.warn("微信支付证书文件不存在于classpath: {}", certPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("无法获取微信支付证书绝对路径: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
File file = new File(certPath);
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
log.info("微信支付证书路径模式: FILESYSTEM");
|
||||
log.info("微信支付证书完整绝对路径: {}", absolutePath);
|
||||
log.info("微信支付证书文件是否存在: {}", file.exists());
|
||||
}
|
||||
|
||||
return certPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付宝证书路径
|
||||
*
|
||||
* @param fileName 文件名
|
||||
* @return 证书路径
|
||||
*/
|
||||
public String getAlipayCertPath(String fileName) {
|
||||
String certPath = certificateProperties.getAlipayCertPath(fileName);
|
||||
log.debug("获取支付宝证书路径 - 文件名: {}, 路径: {}", fileName, certPath);
|
||||
|
||||
// 打印完整的绝对路径信息
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("支付宝证书路径模式: CLASSPATH");
|
||||
log.info("支付宝证书相对路径: {}", certPath);
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource(certPath);
|
||||
if (resource.exists()) {
|
||||
String absolutePath = resource.getFile().getAbsolutePath();
|
||||
log.info("支付宝证书完整绝对路径: {}", absolutePath);
|
||||
} else {
|
||||
log.warn("支付宝证书文件不存在于classpath: {}", certPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("无法获取支付宝证书绝对路径: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
File file = new File(certPath);
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
log.info("支付宝证书路径模式: FILESYSTEM");
|
||||
log.info("支付宝证书完整绝对路径: {}", absolutePath);
|
||||
log.info("支付宝证书文件是否存在: {}", file.exists());
|
||||
}
|
||||
|
||||
return certPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证X509证书
|
||||
*
|
||||
* @param certType 证书类型
|
||||
* @param fileName 文件名
|
||||
* @return 证书信息
|
||||
*/
|
||||
public CertificateInfo validateX509Certificate(String certType, String fileName) {
|
||||
try (InputStream inputStream = getCertificateInputStream(certType, fileName)) {
|
||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
|
||||
|
||||
CertificateInfo info = new CertificateInfo();
|
||||
info.setSubject(cert.getSubjectX500Principal().toString());
|
||||
info.setIssuer(cert.getIssuerX500Principal().toString());
|
||||
info.setNotBefore(cert.getNotBefore());
|
||||
info.setNotAfter(cert.getNotAfter());
|
||||
info.setSerialNumber(cert.getSerialNumber().toString());
|
||||
info.setValid(isValidDate(cert.getNotBefore(), cert.getNotAfter()));
|
||||
|
||||
return info;
|
||||
} catch (Exception e) {
|
||||
log.error("验证证书失败: {}/{}, 错误: {}", certType, fileName, e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书目录结构
|
||||
*/
|
||||
private void checkCertificateDirectories() {
|
||||
String[] certTypes = {"wechat", "alipay"};
|
||||
|
||||
for (String certType : certTypes) {
|
||||
if (!certificateProperties.isClasspathMode()) {
|
||||
// 检查文件系统目录
|
||||
String dirPath = certificateProperties.getCertificatePath(certType, "");
|
||||
File dir = new File(dirPath);
|
||||
if (!dir.exists()) {
|
||||
log.warn("证书目录不存在: {}", dirPath);
|
||||
} else {
|
||||
log.info("证书目录存在: {}", dirPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有证书状态
|
||||
*
|
||||
* @return 证书状态映射
|
||||
*/
|
||||
public Map<String, Object> getAllCertificateStatus() {
|
||||
Map<String, Object> status = new HashMap<>();
|
||||
|
||||
// 微信支付证书状态
|
||||
Map<String, Object> wechatStatus = new HashMap<>();
|
||||
CertificateProperties.WechatPayConfig wechatConfig = certificateProperties.getWechatPay();
|
||||
wechatStatus.put("privateKey", getCertStatus("wechat", wechatConfig.getDev().getPrivateKeyFile()));
|
||||
wechatStatus.put("apiclientCert", getCertStatus("wechat", wechatConfig.getDev().getApiclientCertFile()));
|
||||
wechatStatus.put("wechatpayCert", getCertStatus("wechat", wechatConfig.getDev().getWechatpayCertFile()));
|
||||
status.put("wechat", wechatStatus);
|
||||
|
||||
// 支付宝证书状态
|
||||
Map<String, Object> alipayStatus = new HashMap<>();
|
||||
CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();
|
||||
alipayStatus.put("appPrivateKey", getCertStatus("alipay", alipayConfig.getAppPrivateKeyFile()));
|
||||
alipayStatus.put("appCertPublicKey", getCertStatus("alipay", alipayConfig.getAppCertPublicKeyFile()));
|
||||
alipayStatus.put("alipayCertPublicKey", getCertStatus("alipay", alipayConfig.getAlipayCertPublicKeyFile()));
|
||||
alipayStatus.put("alipayRootCert", getCertStatus("alipay", alipayConfig.getAlipayRootCertFile()));
|
||||
status.put("alipay", alipayStatus);
|
||||
|
||||
// 系统信息
|
||||
Map<String, Object> systemInfo = new HashMap<>();
|
||||
systemInfo.put("loadMode", certificateProperties.getLoadMode());
|
||||
systemInfo.put("certRootPath", certificateProperties.getCertRootPath());
|
||||
systemInfo.put("devCertPath", certificateProperties.getDevCertPath());
|
||||
status.put("system", systemInfo);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单个证书状态
|
||||
*/
|
||||
private Map<String, Object> getCertStatus(String certType, String fileName) {
|
||||
Map<String, Object> status = new HashMap<>();
|
||||
status.put("fileName", fileName);
|
||||
status.put("exists", certificateExists(certType, fileName));
|
||||
status.put("path", getCertificateFilePath(certType, fileName));
|
||||
|
||||
// 如果是.crt或.pem文件,尝试验证证书
|
||||
if (fileName.endsWith(".crt") || fileName.endsWith(".pem")) {
|
||||
CertificateInfo certInfo = validateX509Certificate(certType, fileName);
|
||||
status.put("certificateInfo", certInfo);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查日期是否有效
|
||||
*/
|
||||
private boolean isValidDate(Date notBefore, Date notAfter) {
|
||||
Date now = new Date();
|
||||
return now.after(notBefore) && now.before(notAfter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 证书信息类
|
||||
*/
|
||||
public static class CertificateInfo {
|
||||
private String subject;
|
||||
private String issuer;
|
||||
private Date notBefore;
|
||||
private Date notAfter;
|
||||
private String serialNumber;
|
||||
private boolean valid;
|
||||
|
||||
// Getters and Setters
|
||||
public String getSubject() { return subject; }
|
||||
public void setSubject(String subject) { this.subject = subject; }
|
||||
|
||||
public String getIssuer() { return issuer; }
|
||||
public void setIssuer(String issuer) { this.issuer = issuer; }
|
||||
|
||||
public Date getNotBefore() { return notBefore; }
|
||||
public void setNotBefore(Date notBefore) { this.notBefore = notBefore; }
|
||||
|
||||
public Date getNotAfter() { return notAfter; }
|
||||
public void setNotAfter(Date notAfter) { this.notAfter = notAfter; }
|
||||
|
||||
public String getSerialNumber() { return serialNumber; }
|
||||
public void setSerialNumber(String serialNumber) { this.serialNumber = serialNumber; }
|
||||
|
||||
public boolean isValid() { return valid; }
|
||||
public void setValid(boolean valid) { this.valid = valid; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package com.gxwebsoft.common.core.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import com.gxwebsoft.common.system.param.PaymentParam;
|
||||
import com.gxwebsoft.common.system.service.PaymentService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 支付配置缓存服务
|
||||
* 统一管理支付配置的缓存读取,支持 Payment:1* 格式
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-27
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PaymentCacheService {
|
||||
|
||||
@Autowired
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Autowired
|
||||
private PaymentService paymentService;
|
||||
|
||||
/**
|
||||
* 根据支付类型获取支付配置
|
||||
* 优先从 Payment:1{payType} 格式的缓存读取
|
||||
*
|
||||
* @param payType 支付类型 (0=微信支付, 1=支付宝, 2=其他)
|
||||
* @param tenantId 租户ID (用于兜底查询)
|
||||
* @return Payment 支付配置
|
||||
*/
|
||||
public Payment getPaymentConfig(Integer payType, Integer tenantId) {
|
||||
// 1. 优先使用 Payment:1{payType} 格式的缓存键
|
||||
String primaryKey = "Payment:1:" + tenantId;
|
||||
Payment payment = redisUtil.get(primaryKey, Payment.class);
|
||||
|
||||
if (ObjectUtil.isNotEmpty(payment)) {
|
||||
log.debug("从缓存获取支付配置成功: {}", primaryKey);
|
||||
return payment;
|
||||
}
|
||||
|
||||
// 2. 如果 Payment:1* 格式不存在,尝试原有格式
|
||||
String fallbackKey = "Payment:" + payType + ":" + tenantId;
|
||||
payment = redisUtil.get(fallbackKey, Payment.class);
|
||||
|
||||
if (ObjectUtil.isNotEmpty(payment)) {
|
||||
log.debug("从兜底缓存获取支付配置成功: {}", fallbackKey);
|
||||
// 将查询结果缓存到 Payment:1* 格式
|
||||
redisUtil.set(primaryKey, payment);
|
||||
return payment;
|
||||
}
|
||||
|
||||
// 3. 最后从数据库查询
|
||||
log.debug("从数据库查询支付配置, payType: {}, tenantId: {}", payType, tenantId);
|
||||
PaymentParam paymentParam = new PaymentParam();
|
||||
paymentParam.setType(payType);
|
||||
paymentParam.setTenantId(tenantId); // 设置租户ID进行过滤
|
||||
List<Payment> payments = paymentService.listRel(paymentParam);
|
||||
|
||||
if (payments.isEmpty()) {
|
||||
throw new BusinessException("请完成支付配置,支付类型: " + payType);
|
||||
}
|
||||
|
||||
Payment dbPayment = payments.get(0);
|
||||
|
||||
// 清理时间字段,避免序列化问题
|
||||
Payment cachePayment = cleanPaymentForCache(dbPayment);
|
||||
|
||||
// 将查询结果缓存到 Payment:1* 格式
|
||||
redisUtil.set(primaryKey, cachePayment);
|
||||
log.debug("支付配置已缓存到: {}", primaryKey);
|
||||
|
||||
return dbPayment; // 返回原始对象,不影响业务逻辑
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存支付配置
|
||||
* 同时缓存到 Payment:1{payType} 和原有格式
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param tenantId 租户ID
|
||||
*/
|
||||
public void cachePaymentConfig(Payment payment, Integer tenantId) {
|
||||
// 缓存到 Payment:1* 格式
|
||||
String primaryKey = "Payment:1" + payment.getCode();
|
||||
redisUtil.set(primaryKey, payment);
|
||||
log.debug("支付配置已缓存到: {}", primaryKey);
|
||||
|
||||
// 兼容原有格式
|
||||
String legacyKey = "Payment:" + payment.getCode() + ":" + tenantId;
|
||||
redisUtil.set(legacyKey, payment);
|
||||
log.debug("支付配置已缓存到兼容格式: {}", legacyKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除支付配置缓存
|
||||
* 同时删除 Payment:1{payType} 和原有格式
|
||||
*
|
||||
* @param paymentCode 支付代码 (可以是String或Integer)
|
||||
* @param tenantId 租户ID
|
||||
*/
|
||||
public void removePaymentConfig(String paymentCode, Integer tenantId) {
|
||||
// 删除 Payment:1* 格式缓存
|
||||
String primaryKey = "Payment:1" + paymentCode;
|
||||
redisUtil.delete(primaryKey);
|
||||
log.debug("已删除支付配置缓存: {}", primaryKey);
|
||||
|
||||
// 删除原有格式缓存
|
||||
String legacyKey = "Payment:" + paymentCode + ":" + tenantId;
|
||||
redisUtil.delete(legacyKey);
|
||||
log.debug("已删除兼容格式缓存: {}", legacyKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信支付配置 (payType = 0)
|
||||
*/
|
||||
public Payment getWechatPayConfig(Integer tenantId) {
|
||||
return getPaymentConfig(0, tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付宝配置 (payType = 1)
|
||||
*/
|
||||
public Payment getAlipayConfig(Integer tenantId) {
|
||||
return getPaymentConfig(1, tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理Payment对象用于缓存
|
||||
* 移除可能导致序列化问题的时间字段
|
||||
*/
|
||||
private Payment cleanPaymentForCache(Payment original) {
|
||||
if (original == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Payment cleaned = new Payment();
|
||||
// 复制所有业务相关字段
|
||||
cleaned.setId(original.getId());
|
||||
cleaned.setName(original.getName());
|
||||
cleaned.setType(original.getType());
|
||||
cleaned.setCode(original.getCode());
|
||||
cleaned.setImage(original.getImage());
|
||||
cleaned.setWechatType(original.getWechatType());
|
||||
cleaned.setAppId(original.getAppId());
|
||||
cleaned.setMchId(original.getMchId());
|
||||
cleaned.setApiKey(original.getApiKey());
|
||||
cleaned.setApiclientCert(original.getApiclientCert());
|
||||
cleaned.setApiclientKey(original.getApiclientKey());
|
||||
cleaned.setPubKey(original.getPubKey());
|
||||
cleaned.setPubKeyId(original.getPubKeyId());
|
||||
cleaned.setMerchantSerialNumber(original.getMerchantSerialNumber());
|
||||
cleaned.setNotifyUrl(original.getNotifyUrl());
|
||||
cleaned.setComments(original.getComments());
|
||||
cleaned.setSortNumber(original.getSortNumber());
|
||||
cleaned.setStatus(original.getStatus());
|
||||
cleaned.setDeleted(original.getDeleted());
|
||||
cleaned.setTenantId(original.getTenantId());
|
||||
|
||||
// 不设置时间字段,避免序列化问题
|
||||
// cleaned.setCreateTime(null);
|
||||
// cleaned.setUpdateTime(null);
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
}
|
||||
75
src/main/java/com/gxwebsoft/common/core/socketio/cache/ClientCache.java
vendored
Normal file
75
src/main/java/com/gxwebsoft/common/core/socketio/cache/ClientCache.java
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.gxwebsoft.common.core.socketio.cache;
|
||||
|
||||
import com.corundumstudio.socketio.SocketIOClient;
|
||||
import com.corundumstudio.socketio.SocketIOServer;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @Author
|
||||
* @Description 用户信息缓存
|
||||
* @Date 14:00 2022/1/21
|
||||
* @Param
|
||||
* @return
|
||||
**/
|
||||
@Component
|
||||
public class ClientCache {
|
||||
|
||||
private static Map<String, HashMap<UUID, SocketIOClient>> concurrentHashMap = new ConcurrentHashMap<>();
|
||||
|
||||
private static SocketIOServer socketIOServer;
|
||||
|
||||
public static SocketIOServer getSocketIOServer() {
|
||||
return socketIOServer;
|
||||
}
|
||||
|
||||
public static void setSocketIOServer(SocketIOServer instance) {
|
||||
socketIOServer = instance;
|
||||
}
|
||||
public void saveClient(String userId,UUID sessionId,SocketIOClient socketIOClient){
|
||||
HashMap<UUID, SocketIOClient> sessionIdClientCache = concurrentHashMap.get(userId);
|
||||
if(sessionIdClientCache == null){
|
||||
sessionIdClientCache = new HashMap<>();
|
||||
}
|
||||
sessionIdClientCache.put(sessionId,socketIOClient);
|
||||
concurrentHashMap.put(userId,sessionIdClientCache);
|
||||
}
|
||||
|
||||
|
||||
public HashMap<UUID,SocketIOClient> getUserClient(String userId){
|
||||
return concurrentHashMap.get(userId);
|
||||
}
|
||||
|
||||
public void deleteSessionClientByUserId(String userId,UUID sessionId){
|
||||
concurrentHashMap.get(userId).remove(sessionId);
|
||||
}
|
||||
|
||||
|
||||
public void deleteUserCacheByUserId(String userId){
|
||||
concurrentHashMap.remove(userId);
|
||||
}
|
||||
|
||||
public int getOnLineCount(){
|
||||
return concurrentHashMap.size();
|
||||
}
|
||||
|
||||
public void sendUserEvent(String userId,String event, Object message) {
|
||||
// 发送到接收方
|
||||
HashMap<UUID, SocketIOClient> userClient = concurrentHashMap.get(userId);
|
||||
|
||||
// 查看对方是否在线
|
||||
if(!CollectionUtils.isEmpty(userClient)){
|
||||
for (UUID uuid : userClient.keySet()) {
|
||||
SocketIOClient ioClient = userClient.get(uuid);
|
||||
ioClient.sendEvent(event, message);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.gxwebsoft.common.core.socketio.config;
|
||||
|
||||
import com.corundumstudio.socketio.SocketIOServer;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.security.JwtSubject;
|
||||
import com.gxwebsoft.common.core.security.JwtUtil;
|
||||
import com.gxwebsoft.common.core.socketio.cache.ClientCache;
|
||||
import com.gxwebsoft.common.core.socketio.handler.SocketIOHandler;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* socket服务配置
|
||||
* @author machenike
|
||||
*/
|
||||
@Configuration
|
||||
public class SocketIOConfig implements InitializingBean {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(SocketIOConfig.class);
|
||||
@Value("${socketio.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${socketio.port}")
|
||||
private Integer port;
|
||||
|
||||
@Resource
|
||||
private SocketIOHandler socketIOHandler;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
|
||||
//设置host
|
||||
config.setHostname(host);
|
||||
//设置端口
|
||||
config.setPort(port);
|
||||
config.setBossThreads(1);
|
||||
config.setAuthorizationListener(handshakeData -> {
|
||||
String userId =handshakeData.getSingleUrlParam("userId");
|
||||
String token = handshakeData.getSingleUrlParam("token");
|
||||
logger.info("身份验证 token:{}", token);
|
||||
if(!StringUtils.hasText(token) || !StringUtils.hasText(userId)){
|
||||
return false;
|
||||
}
|
||||
// 身份验证
|
||||
Claims claims = JwtUtil.parseToken(token, configProperties.getTokenKey());
|
||||
JwtSubject jwtSubject = JwtUtil.getJwtSubject(claims);
|
||||
User user = userService.getByUsername(jwtSubject.getUsername(), jwtSubject.getTenantId());
|
||||
|
||||
if (user == null || !user.getUserId().equals(Integer.valueOf(userId))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
InputStream resourceAsStream = this.getClass().getResourceAsStream("/jks/love.jks"); // 读取证书文件流
|
||||
config.setKeyStore(resourceAsStream); // 设置证书文件
|
||||
config.setKeyStorePassword("123456"); // 设置证书密码
|
||||
|
||||
// 启动socket服务
|
||||
// SocketIOServer server = new SocketIOServer(config);
|
||||
// server.addListeners(socketIOHandler);
|
||||
// server.start();
|
||||
// ClientCache.setSocketIOServer(server);
|
||||
// logger.debug("Netty SocketIO启动:{}:{}",host,port);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.gxwebsoft.common.core.socketio.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.corundumstudio.socketio.AckRequest;
|
||||
import com.corundumstudio.socketio.SocketIOClient;
|
||||
import com.corundumstudio.socketio.annotation.OnConnect;
|
||||
import com.corundumstudio.socketio.annotation.OnDisconnect;
|
||||
import com.corundumstudio.socketio.annotation.OnEvent;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.socketio.cache.ClientCache;
|
||||
import com.gxwebsoft.common.system.entity.ChatMessage;
|
||||
import com.gxwebsoft.common.system.service.ChatConversationService;
|
||||
import com.gxwebsoft.common.system.service.ChatMessageService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
/**
|
||||
* socket处理拦截器
|
||||
* @author machenike
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class SocketIOHandler {
|
||||
|
||||
|
||||
@Resource
|
||||
private ChatMessageService messageService;
|
||||
|
||||
@Resource
|
||||
private ChatConversationService conversationService;
|
||||
|
||||
@Resource
|
||||
private ClientCache clientCache;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
/**
|
||||
* 日志
|
||||
*/
|
||||
private final static Logger logger = LoggerFactory.getLogger(SocketIOHandler.class);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 客户端连上socket服务器时执行此事件
|
||||
* @param client
|
||||
*/
|
||||
@OnConnect
|
||||
public void onConnect(SocketIOClient client) {
|
||||
String userId = client.getHandshakeData().getSingleUrlParam("userId");
|
||||
logger.debug("socket client auth success [userId="+userId+"]");
|
||||
UUID sessionId = client.getSessionId();
|
||||
|
||||
|
||||
// 管理员
|
||||
String isAdmin = client.getHandshakeData().getSingleUrlParam("isAdmin");
|
||||
if(StringUtils.isNotBlank(isAdmin)){
|
||||
// todo 权限验证
|
||||
clientCache.saveClient("admin",sessionId, client);
|
||||
}else {
|
||||
clientCache.saveClient(userId,sessionId, client);
|
||||
}
|
||||
|
||||
System.out.println("userId: "+userId+"连接建立成功 - "+sessionId);
|
||||
logger.info("当前在线人数:{}",clientCache.getOnLineCount());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 客户端断开socket服务器时执行此事件
|
||||
* @param client
|
||||
*/
|
||||
@OnDisconnect
|
||||
public void onDisconnect(SocketIOClient client) {
|
||||
String userId = client.getHandshakeData().getSingleUrlParam("userId");
|
||||
UUID sessionId = client.getSessionId();
|
||||
clientCache.deleteSessionClientByUserId(userId,sessionId);
|
||||
System.out.println("userId: "+userId+"连接关闭成功 - "+sessionId);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param client
|
||||
*/
|
||||
@OnEvent( value = "message")
|
||||
@Transactional
|
||||
public void onMessage(SocketIOClient client, AckRequest request, ChatMessage message) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
145
src/main/java/com/gxwebsoft/common/core/utils/AliYunSender.java
Normal file
145
src/main/java/com/gxwebsoft/common/core/utils/AliYunSender.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class AliYunSender {
|
||||
/*
|
||||
* 计算MD5+BASE64
|
||||
*/
|
||||
public static String MD5Base64(String s) {
|
||||
if (s == null)
|
||||
return null;
|
||||
String encodeStr = "";
|
||||
byte[] utfBytes = s.getBytes();
|
||||
MessageDigest mdTemp;
|
||||
try {
|
||||
mdTemp = MessageDigest.getInstance("MD5");
|
||||
mdTemp.update(utfBytes);
|
||||
byte[] md5Bytes = mdTemp.digest();
|
||||
encodeStr = Base64.encode(md5Bytes);
|
||||
} catch (Exception e) {
|
||||
throw new Error("Failed to generate MD5 : " + e.getMessage());
|
||||
}
|
||||
return encodeStr;
|
||||
}
|
||||
/*
|
||||
* 计算 HMAC-SHA1
|
||||
*/
|
||||
public static String HMACSha1(String data, String key) {
|
||||
String result;
|
||||
try {
|
||||
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
mac.init(signingKey);
|
||||
byte[] rawHmac = mac.doFinal(data.getBytes());
|
||||
result = Base64.encode(rawHmac);
|
||||
} catch (Exception e) {
|
||||
throw new Error("Failed to generate HMAC : " + e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*
|
||||
* 获取时间
|
||||
*/
|
||||
public static String toGMTString(Date date) {
|
||||
SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
|
||||
df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
|
||||
return df.format(date);
|
||||
}
|
||||
/*
|
||||
* 发送POST请求
|
||||
*/
|
||||
public static String sendPost(String url, String body, String ak_id, String ak_secret) {
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
String result = "";
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
/*
|
||||
* http header 参数
|
||||
*/
|
||||
String method = "POST";
|
||||
String accept = "application/json";
|
||||
String content_type = "application/json;chrset=utf-8";
|
||||
String path = realUrl.getFile();
|
||||
String date = toGMTString(new Date());
|
||||
String host = realUrl.getHost();
|
||||
// 1.对body做MD5+BASE64加密
|
||||
String bodyMd5 = MD5Base64(body);
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
|
||||
+ "x-acs-signature-method:HMAC-SHA1\n"
|
||||
+ "x-acs-signature-nonce:" + uuid + "\n"
|
||||
+ "x-acs-version:2019-01-02\n"
|
||||
+ path;
|
||||
// 2.计算 HMAC-SHA1
|
||||
String signature = HMACSha1(stringToSign, ak_secret);
|
||||
// 3.得到 authorization header
|
||||
String authHeader = "acs " + ak_id + ":" + signature;
|
||||
// 打开和URL之间的连接
|
||||
URLConnection conn = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("Accept", accept);
|
||||
conn.setRequestProperty("Content-Type", content_type);
|
||||
conn.setRequestProperty("Content-MD5", bodyMd5);
|
||||
conn.setRequestProperty("Date", date);
|
||||
conn.setRequestProperty("Host", host);
|
||||
conn.setRequestProperty("Authorization", authHeader);
|
||||
conn.setRequestProperty("x-acs-signature-nonce", uuid);
|
||||
conn.setRequestProperty("x-acs-signature-method", "HMAC-SHA1");
|
||||
conn.setRequestProperty("x-acs-version", "2019-01-02"); // 版本可选
|
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream());
|
||||
// 发送请求参数
|
||||
out.print(body);
|
||||
// flush输出流的缓冲
|
||||
out.flush();
|
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
InputStream is;
|
||||
HttpURLConnection httpconn = (HttpURLConnection) conn;
|
||||
if (httpconn.getResponseCode() == 200) {
|
||||
is = httpconn.getInputStream();
|
||||
} else {
|
||||
is = httpconn.getErrorStream();
|
||||
}
|
||||
in = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("发送 POST 请求出现异常!" + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 使用finally块来关闭输出流、输入流
|
||||
finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayConstants;
|
||||
import com.alipay.api.CertAlipayRequest;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.core.service.CertificateService;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 支付宝工具类
|
||||
* 支持新的证书管理系统
|
||||
* @author leng
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AlipayConfigUtil {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
public Integer tenantId;
|
||||
public String gateway;
|
||||
public JSONObject config;
|
||||
public String appId;
|
||||
public String privateKey;
|
||||
public String appCertPublicKey;
|
||||
public String alipayCertPublicKey;
|
||||
public String alipayRootCert;
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String active;
|
||||
|
||||
@Resource
|
||||
private ConfigProperties pathConfig;
|
||||
@Resource
|
||||
private CertificateService certificateService;
|
||||
@Resource
|
||||
private CertificateProperties certificateProperties;
|
||||
|
||||
public AlipayConfigUtil(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
// 实例化客户端
|
||||
public DefaultAlipayClient alipayClient(Integer tenantId) throws AlipayApiException {
|
||||
this.gateway = "https://openapi.alipay.com/gateway.do";
|
||||
this.tenantId = tenantId;
|
||||
this.payment(tenantId);
|
||||
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
|
||||
certAlipayRequest.setServerUrl(this.gateway);
|
||||
certAlipayRequest.setAppId(this.appId);
|
||||
certAlipayRequest.setPrivateKey(this.privateKey);
|
||||
certAlipayRequest.setFormat(AlipayConstants.FORMAT_JSON);
|
||||
certAlipayRequest.setCharset(AlipayConstants.CHARSET_UTF8);
|
||||
certAlipayRequest.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
|
||||
certAlipayRequest.setCertPath(this.appCertPublicKey);
|
||||
certAlipayRequest.setAlipayPublicCertPath(this.alipayCertPublicKey);
|
||||
certAlipayRequest.setRootCertPath(this.alipayRootCert);
|
||||
// System.out.println("this.appId = " + this.appId);
|
||||
// System.out.println("this.appId = " + this.gateway);
|
||||
// System.out.println("this.appId = " + this.privateKey);
|
||||
// System.out.println("this.appId = " + this.appCertPublicKey);
|
||||
// System.out.println("this.appId = " + this.alipayCertPublicKey);
|
||||
// System.out.println("this.appId = " + this.alipayRootCert);
|
||||
// System.out.println("this.config = " + this.config);
|
||||
return new DefaultAlipayClient(certAlipayRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付宝秘钥
|
||||
*/
|
||||
public JSONObject payment(Integer tenantId) {
|
||||
log.debug("获取支付宝配置,租户ID: {}", tenantId);
|
||||
String key = "cache".concat(tenantId.toString()).concat(":setting:payment");
|
||||
log.debug("Redis缓存key: {}", key);
|
||||
|
||||
// 测试期间注释掉从缓存获取支付配置
|
||||
// String cache = stringRedisTemplate.opsForValue().get(key);
|
||||
// if (cache == null) {
|
||||
// throw new BusinessException("支付方式未配置");
|
||||
// }
|
||||
|
||||
// 测试期间:模拟缓存为空的情况
|
||||
String cache = null;
|
||||
log.debug("测试模式:支付宝配置缓存设为null");
|
||||
if (cache == null) {
|
||||
throw new BusinessException("支付方式未配置(测试模式:缓存已注释)");
|
||||
}
|
||||
|
||||
// 解析json数据
|
||||
JSONObject payment = JSON.parseObject(cache.getBytes());
|
||||
this.config = payment;
|
||||
this.appId = payment.getString("alipayAppId");
|
||||
this.privateKey = payment.getString("privateKey");
|
||||
|
||||
try {
|
||||
if (active.equals("dev")) {
|
||||
// 开发环境:使用证书服务获取证书路径
|
||||
CertificateProperties.AlipayConfig alipayConfig = certificateProperties.getAlipay();
|
||||
this.appCertPublicKey = certificateService.getAlipayCertPath(alipayConfig.getAppCertPublicKeyFile());
|
||||
this.alipayCertPublicKey = certificateService.getAlipayCertPath(alipayConfig.getAlipayCertPublicKeyFile());
|
||||
this.alipayRootCert = certificateService.getAlipayCertPath(alipayConfig.getAlipayRootCertFile());
|
||||
|
||||
log.info("开发环境支付宝证书路径:");
|
||||
log.info("应用证书相对路径: {}", this.appCertPublicKey);
|
||||
log.info("支付宝证书相对路径: {}", this.alipayCertPublicKey);
|
||||
log.info("根证书相对路径: {}", this.alipayRootCert);
|
||||
|
||||
// 打印完整的绝对路径
|
||||
try {
|
||||
if (certificateProperties.isClasspathMode()) {
|
||||
log.info("支付宝证书加载模式: CLASSPATH");
|
||||
org.springframework.core.io.ClassPathResource appCertResource = new org.springframework.core.io.ClassPathResource(this.appCertPublicKey);
|
||||
org.springframework.core.io.ClassPathResource alipayCertResource = new org.springframework.core.io.ClassPathResource(this.alipayCertPublicKey);
|
||||
org.springframework.core.io.ClassPathResource rootCertResource = new org.springframework.core.io.ClassPathResource(this.alipayRootCert);
|
||||
|
||||
if (appCertResource.exists()) {
|
||||
log.info("应用证书完整绝对路径: {}", appCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
if (alipayCertResource.exists()) {
|
||||
log.info("支付宝证书完整绝对路径: {}", alipayCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
if (rootCertResource.exists()) {
|
||||
log.info("根证书完整绝对路径: {}", rootCertResource.getFile().getAbsolutePath());
|
||||
}
|
||||
} else {
|
||||
log.info("支付宝证书加载模式: FILESYSTEM");
|
||||
log.info("应用证书完整绝对路径: {}", new java.io.File(this.appCertPublicKey).getAbsolutePath());
|
||||
log.info("支付宝证书完整绝对路径: {}", new java.io.File(this.alipayCertPublicKey).getAbsolutePath());
|
||||
log.info("根证书完整绝对路径: {}", new java.io.File(this.alipayRootCert).getAbsolutePath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("获取支付宝证书绝对路径失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 检查证书文件是否存在
|
||||
if (!certificateService.certificateExists("alipay", alipayConfig.getAppCertPublicKeyFile())) {
|
||||
throw new RuntimeException("支付宝应用证书文件不存在");
|
||||
}
|
||||
if (!certificateService.certificateExists("alipay", alipayConfig.getAlipayCertPublicKeyFile())) {
|
||||
throw new RuntimeException("支付宝公钥证书文件不存在");
|
||||
}
|
||||
if (!certificateService.certificateExists("alipay", alipayConfig.getAlipayRootCertFile())) {
|
||||
throw new RuntimeException("支付宝根证书文件不存在");
|
||||
}
|
||||
} else {
|
||||
// 生产环境:使用上传的证书文件
|
||||
// 修改路径拼接规则:uploadPath + "file" + 数据库存储的相对路径
|
||||
String appCertPath = payment.getString("appCertPublicKey");
|
||||
String alipayCertPath = payment.getString("alipayCertPublicKey");
|
||||
String rootCertPath = payment.getString("alipayRootCert");
|
||||
|
||||
this.appCertPublicKey = pathConfig.getUploadPath() + "file" + appCertPath;
|
||||
this.alipayCertPublicKey = pathConfig.getUploadPath() + "file" + alipayCertPath;
|
||||
this.alipayRootCert = pathConfig.getUploadPath() + "file" + rootCertPath;
|
||||
|
||||
log.info("生产环境支付宝证书路径构建:");
|
||||
log.info("上传根路径: {}", pathConfig.getUploadPath());
|
||||
log.info("应用证书 - 数据库路径: {}, 完整路径: {}", appCertPath, this.appCertPublicKey);
|
||||
log.info("支付宝证书 - 数据库路径: {}, 完整路径: {}", alipayCertPath, this.alipayCertPublicKey);
|
||||
log.info("根证书 - 数据库路径: {}, 完整路径: {}", rootCertPath, this.alipayRootCert);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("配置支付宝证书路径失败: {}", e.getMessage(), e);
|
||||
throw new RuntimeException("支付宝证书配置失败: " + e.getMessage());
|
||||
}
|
||||
|
||||
return payment;
|
||||
}
|
||||
|
||||
public String appId(){
|
||||
return this.appId;
|
||||
}
|
||||
|
||||
public String privateKey(){
|
||||
return this.privateKey;
|
||||
}
|
||||
|
||||
public String appCertPublicKey(){
|
||||
return this.appCertPublicKey;
|
||||
}
|
||||
|
||||
public String alipayCertPublicKey(){
|
||||
return this.alipayCertPublicKey;
|
||||
}
|
||||
|
||||
public String alipayRootCert(){
|
||||
return this.alipayRootCert;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
264
src/main/java/com/gxwebsoft/common/core/utils/CacheClient.java
Normal file
264
src/main/java/com/gxwebsoft/common/core/utils/CacheClient.java
Normal file
@@ -0,0 +1,264 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.result.RedisResult;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.RedisConstants.CACHE_NULL_TTL;
|
||||
|
||||
@Component
|
||||
public class CacheClient {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
public static Integer tenantId;
|
||||
|
||||
public CacheClient(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param entity 实体类对象
|
||||
*/
|
||||
public <T> void set(String key, T entity){
|
||||
stringRedisTemplate.opsForValue().set(prefix(key), JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param entity 实体类对象
|
||||
* 示例 cacheClient.set("merchant:"+id,merchant,1L,TimeUnit.DAYS)
|
||||
*/
|
||||
public <T> void set(String key, T entity, Long time, TimeUnit unit){
|
||||
stringRedisTemplate.opsForValue().set(prefix(key), JSONUtil.toJSONString(entity),time,unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存
|
||||
* @param key [表名]:id
|
||||
* 示例 cacheClient.get(key)
|
||||
* @return merchant
|
||||
*/
|
||||
public String get(String key) {
|
||||
return stringRedisTemplate.opsForValue().get(prefix(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param clazz Merchant.class
|
||||
* @param <T>
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
* @return merchant
|
||||
*/
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
String json = stringRedisTemplate.opsForValue().get(prefix(key));
|
||||
if(StrUtil.isNotBlank(json)){
|
||||
return JSONUtil.parseObject(json, clazz);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写redis缓存(哈希类型)
|
||||
* @param key [表名]:id
|
||||
* @param field 字段
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
*/
|
||||
public <T> void hPut(String key, String field, T entity) {
|
||||
stringRedisTemplate.opsForHash().put(prefix(key),field,JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写redis缓存(哈希类型)
|
||||
* @param key [表名]:id
|
||||
* @param map 字段
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
*/
|
||||
public void hPutAll(String key, Map<String,String> map) {
|
||||
stringRedisTemplate.opsForHash().putAll(prefix(key),map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存(哈希类型)
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
* @param key [表名]:id
|
||||
* @param field 字段
|
||||
* @return merchant
|
||||
*/
|
||||
public <T> T hGet(String key, String field, Class<T> clazz) {
|
||||
Object obj = stringRedisTemplate.opsForHash().get(prefix(key), field);
|
||||
return JSONUtil.parseObject(JSONUtil.toJSONString(obj),clazz);
|
||||
}
|
||||
|
||||
public List<Object> hValues(String key){
|
||||
return stringRedisTemplate.opsForHash().values(prefix(key));
|
||||
}
|
||||
|
||||
public Long hSize(String key){
|
||||
return stringRedisTemplate.opsForHash().size(prefix(key));
|
||||
}
|
||||
|
||||
// 逻辑过期方式写入redis
|
||||
public <T> void setWithLogicalExpire(String key, T value, Long time, TimeUnit unit){
|
||||
// 设置逻辑过期时间
|
||||
final RedisResult<T> redisResult = new RedisResult<>();
|
||||
redisResult.setData(value);
|
||||
redisResult.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
|
||||
stringRedisTemplate.opsForValue().set(prefix(key),JSONUtil.toJSONString(redisResult));
|
||||
}
|
||||
|
||||
// 读取redis
|
||||
public <R,ID> R query(String keyPrefix, ID id, Class<R> clazz, Function<ID,R> dbFallback, Long time, TimeUnit unit){
|
||||
String key = keyPrefix + id;
|
||||
// 1.从redis查询缓存
|
||||
final String json = stringRedisTemplate.opsForValue().get(prefix(key));
|
||||
// 2.判断是否存在
|
||||
if (StrUtil.isNotBlank(json)) {
|
||||
// 3.存在,直接返回
|
||||
return JSONUtil.parseObject(json,clazz);
|
||||
}
|
||||
// 判断命中的是否为空值
|
||||
if (json != null) {
|
||||
return null;
|
||||
}
|
||||
// 4. 不存在,跟进ID查询数据库
|
||||
R r = dbFallback.apply(id);
|
||||
// 5. 数据库不存在,返回错误
|
||||
if(r == null){
|
||||
// 空值写入数据库
|
||||
this.set(prefix(key),"",CACHE_NULL_TTL,TimeUnit.MINUTES);
|
||||
return null;
|
||||
}
|
||||
// 写入redis
|
||||
this.set(prefix(key),r,time,unit);
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加商户定位点
|
||||
* @param key geo
|
||||
* @param id
|
||||
* 示例 cacheClient.geoAdd("merchant-geo",merchant)
|
||||
*/
|
||||
public <T> void geoAdd(String key, Double x, Double y, String id){
|
||||
stringRedisTemplate.opsForGeo().add(prefix(key),new Point(x,y),id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除定位
|
||||
* @param key geo
|
||||
* @param id
|
||||
* 示例 cacheClient.geoRemove("merchant-geo",id)
|
||||
*/
|
||||
public void geoRemove(String key, Integer id){
|
||||
stringRedisTemplate.opsForGeo().remove(prefix(key),id.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public <T> void sAdd(String key, T entity){
|
||||
stringRedisTemplate.opsForSet().add(prefix(key),JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
public <T> Set<String> sMembers(String key){
|
||||
return stringRedisTemplate.opsForSet().members(prefix(key));
|
||||
}
|
||||
|
||||
// 更新排行榜
|
||||
public void zAdd(String key, Integer userId, Double value) {
|
||||
stringRedisTemplate.opsForZSet().add(prefix(key),userId.toString(),value);
|
||||
}
|
||||
// 增加元素的score值,并返回增加后的值
|
||||
public Double zIncrementScore(String key,Integer userId, Double delta){
|
||||
return stringRedisTemplate.opsForZSet().incrementScore(key, userId.toString(), delta);
|
||||
}
|
||||
// 获取排名榜
|
||||
public Set<String> range(String key, Integer start, Integer end) {
|
||||
return stringRedisTemplate.opsForZSet().range(prefix(key), start, end);
|
||||
}
|
||||
// 获取排名榜
|
||||
public Set<String> reverseRange(String key, Integer start, Integer end){
|
||||
return stringRedisTemplate.opsForZSet().reverseRange(prefix(key), start, end);
|
||||
}
|
||||
// 获取分数
|
||||
public Double score(String key, Object value){
|
||||
return stringRedisTemplate.opsForZSet().score(prefix(key), value);
|
||||
}
|
||||
|
||||
public void delete(String key){
|
||||
stringRedisTemplate.delete(prefix(key));
|
||||
}
|
||||
|
||||
// 存储在list头部
|
||||
public void leftPush(String key, String keyword){
|
||||
stringRedisTemplate.opsForList().leftPush(prefix(key),keyword);
|
||||
}
|
||||
|
||||
// 获取列表指定范围内的元素
|
||||
public List<String> listRange(String key,Long start, Long end){
|
||||
return stringRedisTemplate.opsForList().range(prefix(key), start, end);
|
||||
}
|
||||
|
||||
// 获取列表长度
|
||||
public Long listSize(String key){
|
||||
return stringRedisTemplate.opsForList().size(prefix(key));
|
||||
}
|
||||
|
||||
// 裁剪list
|
||||
public void listTrim(String key){
|
||||
stringRedisTemplate.opsForList().trim(prefix(key), 0L, 100L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取后台系统设置信息
|
||||
* @param keyName 键名wx-word
|
||||
* @param tenantId 租户ID
|
||||
* @return
|
||||
* key示例 cache10048:setting:wx-work
|
||||
*/
|
||||
public JSONObject getSettingInfo(String keyName,Integer tenantId){
|
||||
String key = "cache" + tenantId + ":setting:" + keyName;
|
||||
final String cache = stringRedisTemplate.opsForValue().get(key);
|
||||
assert cache != null;
|
||||
return JSON.parseObject(cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* KEY前缀
|
||||
* cache[tenantId]:[key+id]
|
||||
*/
|
||||
public static String prefix(String key){
|
||||
String prefix = "cache";
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object object = authentication.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
final Integer tenantId = ((User) object).getTenantId();
|
||||
prefix = prefix.concat(tenantId.toString()).concat(":");
|
||||
}
|
||||
}
|
||||
return prefix.concat(key);
|
||||
}
|
||||
|
||||
// 组装key
|
||||
public String key(String name,Integer id){
|
||||
return name.concat(":").concat(id.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* 证书加载工具类
|
||||
* 支持多种证书加载方式,适配Docker容器化部署
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-01-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CertificateLoader {
|
||||
|
||||
private final CertificateProperties certConfig;
|
||||
|
||||
public CertificateLoader(CertificateProperties certConfig) {
|
||||
this.certConfig = certConfig;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("证书加载器初始化,加载模式:{}", certConfig.getLoadMode());
|
||||
if (certConfig.getLoadMode() == CertificateProperties.LoadMode.VOLUME) {
|
||||
log.info("Docker挂载卷证书路径:{}", certConfig.getCertRootPath());
|
||||
validateCertDirectory();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证证书目录是否存在
|
||||
*/
|
||||
private void validateCertDirectory() {
|
||||
File certDir = new File(certConfig.getCertRootPath());
|
||||
if (!certDir.exists()) {
|
||||
log.warn("证书目录不存在:{},将尝试创建", certConfig.getCertRootPath());
|
||||
if (!certDir.mkdirs()) {
|
||||
log.error("无法创建证书目录:{}", certConfig.getCertRootPath());
|
||||
}
|
||||
} else {
|
||||
log.info("证书目录验证成功:{}", certConfig.getCertRootPath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载证书文件路径
|
||||
*
|
||||
* @param certPath 证书路径(可能是相对路径、绝对路径或classpath路径)
|
||||
* @return 实际的证书文件路径
|
||||
*/
|
||||
public String loadCertificatePath(String certPath) {
|
||||
if (!StringUtils.hasText(certPath)) {
|
||||
throw new IllegalArgumentException("证书路径不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
switch (certConfig.getLoadMode()) {
|
||||
case CLASSPATH:
|
||||
return loadFromClasspath(certPath);
|
||||
case VOLUME:
|
||||
return loadFromVolume(certPath);
|
||||
case FILESYSTEM:
|
||||
default:
|
||||
return loadFromFileSystem(certPath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("加载证书失败,路径:{}", certPath, e);
|
||||
throw new RuntimeException("证书加载失败:" + certPath, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从classpath加载证书
|
||||
*/
|
||||
private String loadFromClasspath(String certPath) throws IOException {
|
||||
String resourcePath = certPath.startsWith("classpath:") ?
|
||||
certPath.substring("classpath:".length()) : certPath;
|
||||
|
||||
ClassPathResource resource = new ClassPathResource(resourcePath);
|
||||
if (!resource.exists()) {
|
||||
throw new IOException("Classpath中找不到证书文件:" + resourcePath);
|
||||
}
|
||||
|
||||
// 将classpath中的文件复制到临时目录
|
||||
Path tempFile = Files.createTempFile("cert_", ".pem");
|
||||
try (InputStream inputStream = resource.getInputStream()) {
|
||||
Files.copy(inputStream, tempFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
String tempPath = tempFile.toAbsolutePath().toString();
|
||||
log.debug("从classpath加载证书:{} -> {}", resourcePath, tempPath);
|
||||
return tempPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Docker挂载卷加载证书
|
||||
*/
|
||||
private String loadFromVolume(String certPath) {
|
||||
log.debug("尝试从Docker挂载卷加载证书:{}", certPath);
|
||||
|
||||
// 如果是完整路径,直接使用
|
||||
if (certPath.startsWith("/") || certPath.contains(":")) {
|
||||
File file = new File(certPath);
|
||||
log.debug("检查完整路径文件是否存在:{}", certPath);
|
||||
if (file.exists()) {
|
||||
log.debug("使用完整路径加载证书:{}", certPath);
|
||||
return certPath;
|
||||
} else {
|
||||
log.error("完整路径文件不存在:{}", certPath);
|
||||
}
|
||||
}
|
||||
|
||||
// 否则拼接挂载卷路径
|
||||
String fullPath = Paths.get(certConfig.getCertRootPath(), certPath).toString();
|
||||
File file = new File(fullPath);
|
||||
if (!file.exists()) {
|
||||
throw new RuntimeException("Docker挂载卷中找不到证书文件:" + fullPath);
|
||||
}
|
||||
|
||||
log.debug("从Docker挂载卷加载证书:{}", fullPath);
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件系统加载证书
|
||||
*/
|
||||
private String loadFromFileSystem(String certPath) {
|
||||
File file = new File(certPath);
|
||||
if (!file.exists()) {
|
||||
throw new RuntimeException("文件系统中找不到证书文件:" + certPath);
|
||||
}
|
||||
|
||||
log.debug("从文件系统加载证书:{}", certPath);
|
||||
return certPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书文件是否存在
|
||||
*
|
||||
* @param certPath 证书路径
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean certificateExists(String certPath) {
|
||||
try {
|
||||
switch (certConfig.getLoadMode()) {
|
||||
case CLASSPATH:
|
||||
String resourcePath = certPath.startsWith("classpath:") ?
|
||||
certPath.substring("classpath:".length()) : certPath;
|
||||
ClassPathResource resource = new ClassPathResource(resourcePath);
|
||||
return resource.exists();
|
||||
case VOLUME:
|
||||
String fullPath = certPath.startsWith("/") ? certPath :
|
||||
Paths.get(certConfig.getCertRootPath(), certPath).toString();
|
||||
return new File(fullPath).exists();
|
||||
case FILESYSTEM:
|
||||
default:
|
||||
return new File(certPath).exists();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("检查证书文件存在性时出错:{}", certPath, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取证书文件的输入流
|
||||
*
|
||||
* @param certPath 证书路径
|
||||
* @return 输入流
|
||||
*/
|
||||
public InputStream getCertificateInputStream(String certPath) throws IOException {
|
||||
switch (certConfig.getLoadMode()) {
|
||||
case CLASSPATH:
|
||||
String resourcePath = certPath.startsWith("classpath:") ?
|
||||
certPath.substring("classpath:".length()) : certPath;
|
||||
ClassPathResource resource = new ClassPathResource(resourcePath);
|
||||
return resource.getInputStream();
|
||||
case VOLUME:
|
||||
case FILESYSTEM:
|
||||
default:
|
||||
String actualPath = loadCertificatePath(certPath);
|
||||
return Files.newInputStream(Paths.get(actualPath));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出证书目录中的所有文件
|
||||
*
|
||||
* @return 证书文件列表
|
||||
*/
|
||||
public String[] listCertificateFiles() {
|
||||
try {
|
||||
switch (certConfig.getLoadMode()) {
|
||||
case VOLUME:
|
||||
File certDir = new File(certConfig.getCertRootPath());
|
||||
if (certDir.exists() && certDir.isDirectory()) {
|
||||
return certDir.list();
|
||||
}
|
||||
break;
|
||||
case CLASSPATH:
|
||||
// classpath模式下不支持列出文件
|
||||
log.warn("Classpath模式下不支持列出证书文件");
|
||||
break;
|
||||
case FILESYSTEM:
|
||||
default:
|
||||
// 文件系统模式下证书可能分散在不同目录,不支持统一列出
|
||||
log.warn("文件系统模式下不支持列出证书文件");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("列出证书文件时出错", e);
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
293
src/main/java/com/gxwebsoft/common/core/utils/CommonUtil.java
Normal file
293
src/main/java/com/gxwebsoft/common/core/utils/CommonUtil.java
Normal file
@@ -0,0 +1,293 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.utils.JSONUtil;
|
||||
import com.gxwebsoft.common.system.entity.Role;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 常用工具方法
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:22
|
||||
*/
|
||||
public class CommonUtil {
|
||||
// 生成uuid的字符
|
||||
private static final String[] chars = new String[]{
|
||||
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
||||
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
|
||||
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
|
||||
};
|
||||
|
||||
/**
|
||||
* 生成8位uuid
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String randomUUID8() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
String str = uuid.substring(i * 4, i * 4 + 4);
|
||||
int x = Integer.parseInt(str, 16);
|
||||
sb.append(chars[x % 0x3E]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成16位uuid
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String randomUUID16() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||||
for (int i = 0; i < 16; i++) {
|
||||
String str = uuid.substring(i * 2, i * 2 + 2);
|
||||
int x = Integer.parseInt(str, 16);
|
||||
sb.append(chars[x % 0x3E]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String currentTime() {
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成10位随机用户名
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String randomUsername(String prefix) {
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
|
||||
String currentTime = sdf.format(date);
|
||||
return prefix + currentTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单号
|
||||
* 20233191166110426
|
||||
* 20230419135802391412
|
||||
* @return
|
||||
*/
|
||||
public static String createOrderNo() {
|
||||
String prefix = DateTime.now().toString(DatePattern.PURE_DATETIME_PATTERN);
|
||||
return prefix + RandomUtil.randomNumbers(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单号
|
||||
* @param tenantId
|
||||
* 20233191166110426
|
||||
* 20230419135802391412
|
||||
* @return
|
||||
*/
|
||||
public static String createOrderNo(String tenantId) {
|
||||
String prefix = DateTime.now().toString(DatePattern.PURE_DATETIME_PATTERN);
|
||||
return prefix + tenantId + RandomUtil.randomNumbers(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单水流号
|
||||
* @param tenantId
|
||||
* @return
|
||||
*/
|
||||
public static String serialNo(int tenantId) {
|
||||
String prefix = DateTime.now().toString(DatePattern.PURE_DATETIME_PATTERN);
|
||||
return prefix + tenantId + RandomUtil.randomNumbers(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查List是否有重复元素
|
||||
*
|
||||
* @param list List
|
||||
* @param mapper 获取需要检查的字段的Function
|
||||
* @param <T> 数据的类型
|
||||
* @param <R> 需要检查的字段的类型
|
||||
* @return boolean
|
||||
*/
|
||||
public static <T, R> boolean checkRepeat(List<T> list, Function<? super T, ? extends R> mapper) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if (i != j && mapper.apply(list.get(i)).equals(mapper.apply(list.get(j)))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* List转为树形结构
|
||||
*
|
||||
* @param data List
|
||||
* @param parentId 顶级的parentId
|
||||
* @param parentIdMapper 获取parentId的Function
|
||||
* @param idMapper 获取id的Function
|
||||
* @param consumer 赋值children的Consumer
|
||||
* @param <T> 数据的类型
|
||||
* @param <R> parentId的类型
|
||||
* @return List<T>
|
||||
*/
|
||||
public static <T, R> List<T> toTreeData(List<T> data, R parentId,
|
||||
Function<? super T, ? extends R> parentIdMapper,
|
||||
Function<? super T, ? extends R> idMapper,
|
||||
BiConsumer<T, List<T>> consumer) {
|
||||
List<T> result = new ArrayList<>();
|
||||
for (T d : data) {
|
||||
R dParentId = parentIdMapper.apply(d);
|
||||
if (ObjectUtil.equals(parentId, dParentId)) {
|
||||
R dId = idMapper.apply(d);
|
||||
List<T> children = toTreeData(data, dId, parentIdMapper, idMapper, consumer);
|
||||
consumer.accept(d, children);
|
||||
result.add(d);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历树形结构数据
|
||||
*
|
||||
* @param data List
|
||||
* @param consumer 回调
|
||||
* @param mapper 获取children的Function
|
||||
* @param <T> 数据的类型
|
||||
*/
|
||||
public static <T> void eachTreeData(List<T> data, Consumer<T> consumer, Function<T, List<T>> mapper) {
|
||||
for (T d : data) {
|
||||
consumer.accept(d);
|
||||
List<T> children = mapper.apply(d);
|
||||
if (children != null && children.size() > 0) {
|
||||
eachTreeData(children, consumer, mapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取集合中的第一条数据
|
||||
*
|
||||
* @param records 集合
|
||||
* @return 第一条数据
|
||||
*/
|
||||
public static <T> T listGetOne(List<T> records) {
|
||||
return records == null || records.size() == 0 ? null : records.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持跨域
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
*/
|
||||
public static void addCrossHeaders(HttpServletResponse response) {
|
||||
response.setHeader("Access-Control-Max-Age", "3600");
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
response.setHeader("Access-Control-Allow-Methods", "*");
|
||||
response.setHeader("Access-Control-Allow-Headers", "*");
|
||||
response.setHeader("Access-Control-Expose-Headers", Constants.TOKEN_HEADER_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出错误信息
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param code 错误码
|
||||
* @param message 提示信息
|
||||
* @param error 错误信息
|
||||
*/
|
||||
public static void responseError(HttpServletResponse response, Integer code, String message, String error) {
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
try {
|
||||
PrintWriter out = response.getWriter();
|
||||
out.write(JSONUtil.toJSONString(new ApiResult<>(code, message, null, error)));
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean hasRole(List<Role> array, String value){
|
||||
System.out.println("value = " + value);
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
if (array == null) {
|
||||
return false;
|
||||
}
|
||||
if (!array.isEmpty()) {
|
||||
final List<String> collect = array.stream().map(Role::getRoleCode)
|
||||
.collect(Collectors.toList());
|
||||
final boolean contains = collect.contains(value);
|
||||
if (contains) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean hasRole(List<Role> array,List<String> value){
|
||||
System.out.println("value = " + value);
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
if (array == null) {
|
||||
return false;
|
||||
}
|
||||
if (!array.isEmpty()) {
|
||||
final List<String> collect = array.stream().map(Role::getRoleCode)
|
||||
.collect(Collectors.toList());
|
||||
final boolean disjoint = Collections.disjoint(collect, value);
|
||||
if (!disjoint) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证给定的字符串是否为有效的中国大陆手机号码。
|
||||
*
|
||||
* @param phoneNumber 要验证的电话号码字符串
|
||||
* @return 如果字符串是有效的手机号码,则返回true;否则返回false
|
||||
*/
|
||||
public static boolean isValidPhoneNumber(String phoneNumber) {
|
||||
// 定义手机号码的正则表达式
|
||||
String regex = "^1[3-9]\\d{9}$";
|
||||
|
||||
// 创建Pattern对象
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
|
||||
// 使用matcher方法创建Matcher对象并进行匹配
|
||||
return pattern.matcher(phoneNumber).matches();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.DomainConstants.*;
|
||||
|
||||
public class DomainUtil {
|
||||
|
||||
/**
|
||||
* 根域名
|
||||
* @return domain.com
|
||||
*/
|
||||
public static String getRootDomain() {
|
||||
return ROOT_DOMAIN;
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理后台地址
|
||||
* @return https://{tenantId}.websoft.top
|
||||
*/
|
||||
public static String getAdminUrl(String tenantId) {
|
||||
return PREFIX.concat(tenantId).concat(ADMIN_SUFFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用网址
|
||||
* @param tenantId
|
||||
* @return https://{tenantId}.wsdns.cn
|
||||
*/
|
||||
public static String getSiteUrl(String tenantId){
|
||||
return PREFIX.concat(tenantId).concat(WEB_SUFFIX);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,401 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.img.ImgUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.apache.tika.Tika;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 文件上传下载工具类
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-12-14 08:38:53
|
||||
*/
|
||||
public class FileServerUtil {
|
||||
// 除 text/* 外也需要设置输出编码的 content-type
|
||||
private final static List<String> SET_CHARSET_CONTENT_TYPES = Arrays.asList(
|
||||
"application/json",
|
||||
"application/javascript"
|
||||
);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file MultipartFile
|
||||
* @param directory 文件保存的目录
|
||||
* @param uuidName 是否用uuid命名
|
||||
* @return File
|
||||
*/
|
||||
public static File upload(MultipartFile file, String directory, boolean uuidName)
|
||||
throws IOException, IllegalStateException {
|
||||
File outFile = getUploadFile(file.getOriginalFilename(), directory, uuidName);
|
||||
if (!outFile.getParentFile().exists()) {
|
||||
if (!outFile.getParentFile().mkdirs()) {
|
||||
throw new RuntimeException("make directory fail");
|
||||
}
|
||||
}
|
||||
file.transferTo(outFile);
|
||||
return outFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传base64格式文件
|
||||
*
|
||||
* @param base64 base64编码字符
|
||||
* @param fileName 文件名称, 为空使用uuid命名
|
||||
* @param directory 文件保存的目录
|
||||
* @return File
|
||||
*/
|
||||
public static File upload(String base64, String fileName, String directory)
|
||||
throws FileNotFoundException, IORuntimeException {
|
||||
if (StrUtil.isBlank(base64) || !base64.startsWith("data:image/") || !base64.contains(";base64,")) {
|
||||
throw new RuntimeException("base64 data error");
|
||||
}
|
||||
String suffix = "." + base64.substring(11, base64.indexOf(";")); // 获取文件后缀
|
||||
boolean uuidName = StrUtil.isBlank(fileName);
|
||||
File outFile = getUploadFile(uuidName ? suffix : fileName, directory, uuidName);
|
||||
byte[] bytes = Base64.getDecoder().decode(base64.substring(base64.indexOf(";") + 8).getBytes());
|
||||
IoUtil.write(new FileOutputStream(outFile), true, bytes);
|
||||
return outFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传文件位置
|
||||
*
|
||||
* @param name 文件名称
|
||||
* @param directory 上传目录
|
||||
* @param uuidName 是否使用uuid命名
|
||||
* @return File
|
||||
*/
|
||||
public static File getUploadFile(String name, String directory, boolean uuidName) {
|
||||
// 当前日期作为上传子目录
|
||||
String dir = new SimpleDateFormat("yyyyMMdd/").format(new Date());
|
||||
// 获取文件后缀
|
||||
String suffix = (name == null || !name.contains(".")) ? "" : name.substring(name.lastIndexOf("."));
|
||||
// 使用uuid命名
|
||||
if (uuidName || name == null) {
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
return new File(directory, dir + uuid + suffix);
|
||||
}
|
||||
// 使用原名称, 存在相同则加(1)
|
||||
File file = new File(directory, dir + name);
|
||||
String prefix = StrUtil.removeSuffix(name, suffix);
|
||||
int sameSize = 2;
|
||||
while (file.exists()) {
|
||||
file = new File(directory, dir + prefix + "(" + sameSize + ")" + suffix);
|
||||
sameSize++;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看文件, 支持断点续传
|
||||
*
|
||||
* @param file 文件
|
||||
* @param pdfDir office转pdf输出目录
|
||||
* @param officeHome openOffice安装目录
|
||||
* @param response HttpServletResponse
|
||||
* @param request HttpServletRequest
|
||||
*/
|
||||
public static void preview(File file, String pdfDir, String officeHome,
|
||||
HttpServletResponse response, HttpServletRequest request) {
|
||||
preview(file, false, null, pdfDir, officeHome, response, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看文件, 支持断点续传
|
||||
*
|
||||
* @param file 文件
|
||||
* @param forceDownload 是否强制下载
|
||||
* @param fileName 强制下载的文件名称
|
||||
* @param pdfDir office转pdf输出目录
|
||||
* @param officeHome openOffice安装目录
|
||||
* @param response HttpServletResponse
|
||||
* @param request HttpServletRequest
|
||||
*/
|
||||
public static void preview(File file, boolean forceDownload, String fileName, String pdfDir, String officeHome,
|
||||
HttpServletResponse response, HttpServletRequest request) {
|
||||
CommonUtil.addCrossHeaders(response);
|
||||
if (file == null || !file.exists()) {
|
||||
outNotFund(response);
|
||||
return;
|
||||
}
|
||||
if (forceDownload) {
|
||||
setDownloadHeader(response, StrUtil.isBlank(fileName) ? file.getName() : fileName);
|
||||
} else {
|
||||
// office转pdf预览
|
||||
if (OpenOfficeUtil.canConverter(file.getName())) {
|
||||
File pdfFile = OpenOfficeUtil.converterToPDF(file.getAbsolutePath(), pdfDir, officeHome);
|
||||
if (pdfFile != null) {
|
||||
file = pdfFile;
|
||||
}
|
||||
}
|
||||
// 获取文件类型
|
||||
String contentType = getContentType(file);
|
||||
if (contentType != null) {
|
||||
response.setContentType(contentType);
|
||||
// 设置编码
|
||||
if (contentType.startsWith("text/") || SET_CHARSET_CONTENT_TYPES.contains(contentType)) {
|
||||
try {
|
||||
String charset = JChardetFacadeUtil.detectCodepage(file.toURI().toURL());
|
||||
if (charset != null) {
|
||||
response.setCharacterEncoding(charset);
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
setDownloadHeader(response, file.getName());
|
||||
}
|
||||
}
|
||||
response.setHeader("Cache-Control", "public");
|
||||
output(file, response, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看缩略图
|
||||
*
|
||||
* @param file 原文件
|
||||
* @param thumbnail 缩略图文件
|
||||
* @param size 缩略图文件的最大值(kb)
|
||||
* @param response HttpServletResponse
|
||||
* @param request HttpServletRequest
|
||||
*/
|
||||
public static void previewThumbnail(File file, File thumbnail, Integer size,
|
||||
HttpServletResponse response, HttpServletRequest request) {
|
||||
// 如果是图片并且缩略图不存在则生成
|
||||
if (!thumbnail.exists() && isImage(file)) {
|
||||
long fileSize = file.length();
|
||||
if ((fileSize / 1024) > size) {
|
||||
try {
|
||||
if (thumbnail.getParentFile().mkdirs()) {
|
||||
System.out.println("生成缩略图1>>>>>>>>>>>>>>>> = " + thumbnail);
|
||||
ImgUtil.scale(file, thumbnail, size / (fileSize / 1024f));
|
||||
if (thumbnail.exists() && thumbnail.length() > file.length()) {
|
||||
FileUtil.copy(file, thumbnail, true);
|
||||
}
|
||||
}else{
|
||||
System.out.println("生成缩略图2>>>>>>>>>>>>>>>> = " + thumbnail);
|
||||
ImgUtil.scale(file, thumbnail, size / (fileSize / 1024f));
|
||||
if (thumbnail.exists() && thumbnail.length() > file.length()) {
|
||||
FileUtil.copy(file, thumbnail, true);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
preview(file, null, null, response, request);
|
||||
return;
|
||||
}
|
||||
}
|
||||
preview(thumbnail.exists() ? thumbnail : file, null, null, response, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出文件流, 支持断点续传
|
||||
*
|
||||
* @param file 文件
|
||||
* @param response HttpServletResponse
|
||||
* @param request HttpServletRequest
|
||||
*/
|
||||
public static void output(File file, HttpServletResponse response, HttpServletRequest request) {
|
||||
long length = file.length(); // 文件总大小
|
||||
long start = 0, to = length - 1; // 开始读取位置, 结束读取位置
|
||||
long lastModified = file.lastModified(); // 文件修改时间
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setHeader("ETag", "\"" + length + "-" + lastModified + "\"");
|
||||
response.setHeader("Last-Modified", new Date(lastModified).toString());
|
||||
String range = request.getHeader("Range");
|
||||
if (range != null) {
|
||||
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
String[] ranges = range.replace("bytes=", "").split("-");
|
||||
start = Long.parseLong(ranges[0].trim());
|
||||
if (ranges.length > 1) {
|
||||
to = Long.parseLong(ranges[1].trim());
|
||||
}
|
||||
response.setHeader("Content-Range", "bytes " + start + "-" + to + "/" + length);
|
||||
}
|
||||
response.setHeader("Content-Length", String.valueOf(to - start + 1));
|
||||
try {
|
||||
output(file, response.getOutputStream(), 2048, start, to);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出文件流
|
||||
*
|
||||
* @param file 文件
|
||||
* @param os 输出流
|
||||
*/
|
||||
public static void output(File file, OutputStream os) {
|
||||
output(file, os, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出文件流
|
||||
*
|
||||
* @param file 文件
|
||||
* @param os 输出流
|
||||
* @param size 读取缓冲区大小
|
||||
*/
|
||||
public static void output(File file, OutputStream os, Integer size) {
|
||||
output(file, os, size, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出文件流, 支持分片
|
||||
*
|
||||
* @param file 文件
|
||||
* @param os 输出流
|
||||
* @param size 读取缓冲区大小
|
||||
* @param start 开始位置
|
||||
* @param to 结束位置
|
||||
*/
|
||||
public static void output(File file, OutputStream os, Integer size, Long start, Long to) {
|
||||
BufferedInputStream is = null;
|
||||
try {
|
||||
is = new BufferedInputStream(new FileInputStream(file));
|
||||
if (start != null) {
|
||||
long skip = is.skip(start);
|
||||
if (skip < start) {
|
||||
System.out.println("ERROR: skip fail[ skipped=" + skip + ", start= " + start + " ]");
|
||||
}
|
||||
to = to - start + 1;
|
||||
}
|
||||
byte[] bytes = new byte[size == null ? 2048 : size];
|
||||
int len;
|
||||
if (to == null) {
|
||||
while ((len = is.read(bytes)) != -1) {
|
||||
os.write(bytes, 0, len);
|
||||
}
|
||||
} else {
|
||||
while (to > 0 && (len = is.read(bytes)) != -1) {
|
||||
os.write(bytes, 0, to < len ? (int) ((long) to) : len);
|
||||
to -= len;
|
||||
}
|
||||
}
|
||||
os.flush();
|
||||
} catch (IOException ignored) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (os != null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件类型
|
||||
*
|
||||
* @param file 文件
|
||||
* @return String
|
||||
*/
|
||||
public static String getContentType(File file) {
|
||||
String contentType = null;
|
||||
if (file.exists()) {
|
||||
try {
|
||||
contentType = new Tika().detect(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否是图片类型
|
||||
*
|
||||
* @param file 文件
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isImage(File file) {
|
||||
return isImage(getContentType(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否是图片类型
|
||||
*
|
||||
* @param contentType 文件类型
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isImage(String contentType) {
|
||||
return contentType != null && contentType.startsWith("image/");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置下载文件的header
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param fileName 文件名称
|
||||
*/
|
||||
public static void setDownloadHeader(HttpServletResponse response, String fileName) {
|
||||
response.setContentType("application/force-download");
|
||||
try {
|
||||
fileName = URLEncoder.encode(fileName, "utf-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出404错误页面
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
*/
|
||||
public static void outNotFund(HttpServletResponse response) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
outMessage("404 Not Found", null, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出错误页面
|
||||
*
|
||||
* @param title 标题
|
||||
* @param message 内容
|
||||
* @param response HttpServletResponse
|
||||
*/
|
||||
public static void outMessage(String title, String message, HttpServletResponse response) {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
try {
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("<!doctype html>");
|
||||
writer.write("<title>" + title + "</title>");
|
||||
writer.write("<h1 style=\"text-align: center\">" + title + "</h1>");
|
||||
if (message != null) {
|
||||
writer.write(message);
|
||||
}
|
||||
writer.write("<hr/><p style=\"text-align: center\">WebSoft File Server</p>");
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
311
src/main/java/com/gxwebsoft/common/core/utils/HttpUtils.java
Normal file
311
src/main/java/com/gxwebsoft/common/core/utils/HttpUtils.java
Normal file
@@ -0,0 +1,311 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doGet(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post form
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param bodys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
Map<String, String> bodys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (bodys != null) {
|
||||
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||
|
||||
for (String key : bodys.keySet()) {
|
||||
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||
}
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
request.setEntity(formEntity);
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post String
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post stream
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put String
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put stream
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doDelete(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||
StringBuilder sbUrl = new StringBuilder();
|
||||
sbUrl.append(host);
|
||||
if (!StringUtils.isBlank(path)) {
|
||||
sbUrl.append(path);
|
||||
}
|
||||
if (null != querys) {
|
||||
StringBuilder sbQuery = new StringBuilder();
|
||||
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||
if (0 < sbQuery.length()) {
|
||||
sbQuery.append("&");
|
||||
}
|
||||
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append(query.getValue());
|
||||
}
|
||||
if (!StringUtils.isBlank(query.getKey())) {
|
||||
sbQuery.append(query.getKey());
|
||||
if (!StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append("=");
|
||||
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 < sbQuery.length()) {
|
||||
sbUrl.append("?").append(sbQuery);
|
||||
}
|
||||
}
|
||||
|
||||
return sbUrl.toString();
|
||||
}
|
||||
|
||||
private static HttpClient wrapClient(String host) {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
if (host.startsWith("https://")) {
|
||||
sslClient(httpClient);
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private static void sslClient(HttpClient httpClient) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, ssf));
|
||||
} catch (KeyManagementException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
62
src/main/java/com/gxwebsoft/common/core/utils/ImageUtil.java
Normal file
62
src/main/java/com/gxwebsoft/common/core/utils/ImageUtil.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.codec.Base64Encoder;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class ImageUtil {
|
||||
public static String ImageBase64(String imgUrl) {
|
||||
URL url = null;
|
||||
InputStream is = null;
|
||||
ByteArrayOutputStream outStream = null;
|
||||
HttpURLConnection httpUrl = null;
|
||||
try{
|
||||
url = new URL(imgUrl);
|
||||
httpUrl = (HttpURLConnection) url.openConnection();
|
||||
httpUrl.connect();
|
||||
httpUrl.getInputStream();
|
||||
is = httpUrl.getInputStream();
|
||||
|
||||
outStream = new ByteArrayOutputStream();
|
||||
//创建一个Buffer字符串
|
||||
byte[] buffer = new byte[1024];
|
||||
//每次读取的字符串长度,如果为-1,代表全部读取完毕
|
||||
int len = 0;
|
||||
//使用一个输入流从buffer里把数据读取出来
|
||||
while( (len=is.read(buffer)) != -1 ){
|
||||
//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
|
||||
outStream.write(buffer, 0, len);
|
||||
}
|
||||
// 对字节数组Base64编码
|
||||
return new Base64Encoder().encode(outStream.toByteArray());
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally{
|
||||
if(is != null)
|
||||
{
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if(outStream != null)
|
||||
{
|
||||
try {
|
||||
outStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if(httpUrl != null)
|
||||
{
|
||||
httpUrl.disconnect();
|
||||
}
|
||||
}
|
||||
return imgUrl;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
69
src/main/java/com/gxwebsoft/common/core/utils/JSONUtil.java
Normal file
69
src/main/java/com/gxwebsoft/common/core/utils/JSONUtil.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
|
||||
/**
|
||||
* JSON解析工具类
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:39
|
||||
*/
|
||||
public class JSONUtil {
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
|
||||
|
||||
/**
|
||||
* 对象转json字符串
|
||||
*
|
||||
* @param value 对象
|
||||
* @return String
|
||||
*/
|
||||
public static String toJSONString(Object value) {
|
||||
return toJSONString(value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转json字符串
|
||||
*
|
||||
* @param value 对象
|
||||
* @param pretty 是否格式化输出
|
||||
* @return String
|
||||
*/
|
||||
public static String toJSONString(Object value, boolean pretty) {
|
||||
if (value != null) {
|
||||
if (value instanceof String) {
|
||||
return (String) value;
|
||||
}
|
||||
try {
|
||||
if (pretty) {
|
||||
return objectWriter.writeValueAsString(value);
|
||||
}
|
||||
return objectMapper.writeValueAsString(value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转对象
|
||||
*
|
||||
* @param json String
|
||||
* @param clazz Class
|
||||
* @return T
|
||||
*/
|
||||
public static <T> T parseObject(String json, Class<T> clazz) {
|
||||
if (StrUtil.isNotBlank(json) && clazz != null) {
|
||||
try {
|
||||
return objectMapper.readValue(json, clazz);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 日志分析工具类
|
||||
* 用于收集和分析请求相关信息
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2025-01-20
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LogAnalysisUtil {
|
||||
|
||||
/**
|
||||
* 记录请求详细信息
|
||||
*/
|
||||
public static void logRequestDetails(HttpServletRequest request, String operation) {
|
||||
try {
|
||||
Map<String, Object> requestInfo = new HashMap<>();
|
||||
|
||||
// 基本请求信息
|
||||
requestInfo.put("operation", operation);
|
||||
requestInfo.put("method", request.getMethod());
|
||||
requestInfo.put("requestURL", request.getRequestURL().toString());
|
||||
requestInfo.put("requestURI", request.getRequestURI());
|
||||
requestInfo.put("queryString", request.getQueryString());
|
||||
requestInfo.put("remoteAddr", request.getRemoteAddr());
|
||||
requestInfo.put("userAgent", request.getHeader("User-Agent"));
|
||||
|
||||
// 请求头信息
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String headerName = headerNames.nextElement();
|
||||
// 过滤敏感信息
|
||||
if (!isSensitiveHeader(headerName)) {
|
||||
headers.put(headerName, request.getHeader(headerName));
|
||||
}
|
||||
}
|
||||
requestInfo.put("headers", headers);
|
||||
|
||||
// 参数信息
|
||||
Map<String, String[]> parameters = request.getParameterMap();
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
parameters.forEach((key, values) -> {
|
||||
if (!isSensitiveParameter(key)) {
|
||||
params.put(key, values.length == 1 ? values[0] : values);
|
||||
}
|
||||
});
|
||||
requestInfo.put("parameters", params);
|
||||
|
||||
log.info("请求详情: {}", requestInfo);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录请求详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录异常详细信息
|
||||
*/
|
||||
public static void logExceptionDetails(Exception exception, String context) {
|
||||
try {
|
||||
Map<String, Object> exceptionInfo = new HashMap<>();
|
||||
|
||||
exceptionInfo.put("context", context);
|
||||
exceptionInfo.put("exceptionType", exception.getClass().getSimpleName());
|
||||
exceptionInfo.put("message", exception.getMessage());
|
||||
exceptionInfo.put("timestamp", System.currentTimeMillis());
|
||||
|
||||
// 堆栈跟踪
|
||||
StackTraceElement[] stackTrace = exception.getStackTrace();
|
||||
if (stackTrace.length > 0) {
|
||||
StackTraceElement firstElement = stackTrace[0];
|
||||
exceptionInfo.put("errorLocation",
|
||||
firstElement.getClassName() + "." + firstElement.getMethodName() +
|
||||
"(" + firstElement.getFileName() + ":" + firstElement.getLineNumber() + ")");
|
||||
}
|
||||
|
||||
// 根异常
|
||||
Throwable rootCause = getRootCause(exception);
|
||||
if (rootCause != exception) {
|
||||
exceptionInfo.put("rootCause", rootCause.getClass().getSimpleName());
|
||||
exceptionInfo.put("rootCauseMessage", rootCause.getMessage());
|
||||
}
|
||||
|
||||
log.error("异常详情: {}", exceptionInfo, exception);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录异常详情失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录性能信息
|
||||
*/
|
||||
public static void logPerformanceInfo(String operation, long startTime, long endTime) {
|
||||
try {
|
||||
long duration = endTime - startTime;
|
||||
Map<String, Object> performanceInfo = new HashMap<>();
|
||||
|
||||
performanceInfo.put("operation", operation);
|
||||
performanceInfo.put("startTime", startTime);
|
||||
performanceInfo.put("endTime", endTime);
|
||||
performanceInfo.put("duration", duration + "ms");
|
||||
|
||||
// 性能级别判断
|
||||
String level = "INFO";
|
||||
if (duration > 5000) {
|
||||
level = "WARN";
|
||||
} else if (duration > 10000) {
|
||||
level = "ERROR";
|
||||
}
|
||||
performanceInfo.put("performanceLevel", level);
|
||||
|
||||
if ("ERROR".equals(level)) {
|
||||
log.error("性能异常: {}", performanceInfo);
|
||||
} else if ("WARN".equals(level)) {
|
||||
log.warn("性能告警: {}", performanceInfo);
|
||||
} else {
|
||||
log.info("性能信息: {}", performanceInfo);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录性能信息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为敏感请求头
|
||||
*/
|
||||
private static boolean isSensitiveHeader(String headerName) {
|
||||
String lowerName = headerName.toLowerCase();
|
||||
return lowerName.contains("password") ||
|
||||
lowerName.contains("token") ||
|
||||
lowerName.contains("authorization") ||
|
||||
lowerName.contains("cookie");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否为敏感参数
|
||||
*/
|
||||
private static boolean isSensitiveParameter(String paramName) {
|
||||
String lowerName = paramName.toLowerCase();
|
||||
return lowerName.contains("password") ||
|
||||
lowerName.contains("token") ||
|
||||
lowerName.contains("secret") ||
|
||||
lowerName.contains("key");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取根异常
|
||||
*/
|
||||
private static Throwable getRootCause(Throwable throwable) {
|
||||
Throwable rootCause = throwable;
|
||||
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
|
||||
rootCause = rootCause.getCause();
|
||||
}
|
||||
return rootCause;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录安全相关日志
|
||||
*/
|
||||
public static void logSecurityEvent(String event, String username, String details, HttpServletRequest request) {
|
||||
try {
|
||||
Map<String, Object> securityInfo = new HashMap<>();
|
||||
|
||||
securityInfo.put("event", event);
|
||||
securityInfo.put("username", username);
|
||||
securityInfo.put("details", details);
|
||||
securityInfo.put("timestamp", System.currentTimeMillis());
|
||||
securityInfo.put("remoteAddr", request != null ? request.getRemoteAddr() : "unknown");
|
||||
securityInfo.put("userAgent", request != null ? request.getHeader("User-Agent") : "unknown");
|
||||
|
||||
log.warn("安全事件: {}", securityInfo);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("记录安全事件失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.extra.qrcode.QrCodeUtil;
|
||||
import cn.hutool.extra.qrcode.QrConfig;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.QRCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 常用工具方法
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:22
|
||||
*/
|
||||
public class MyQrCodeUtil {
|
||||
|
||||
private static final String logoUrl = "https://file.wsdns.cn/20230430/6fa31aca3b0d47af98a149cf2dd26a4f.jpeg";
|
||||
|
||||
/**
|
||||
* 生成用户二维码
|
||||
*/
|
||||
public static String getUserCode(Integer userId, String content) throws IOException {
|
||||
return createQrCode(USER_QRCODE,userId,content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成工单二维码
|
||||
*/
|
||||
public static String getTaskCode(Integer taskId, String content) throws IOException {
|
||||
return createQrCode(TASK_QRCODE,taskId,content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成商品二维码
|
||||
*/
|
||||
public static String getGoodsCode(Integer goodsId, String content) throws IOException {
|
||||
return createQrCode(GOODS_QRCODE,goodsId,content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成自定义二维码
|
||||
*/
|
||||
public static String getCodeMap(HashMap<String, String> map) throws IOException {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成带水印的二维码
|
||||
* @param type 类型
|
||||
* @param id 实体ID
|
||||
* @param content 二维码内容
|
||||
* @return 二维码图片地址
|
||||
*/
|
||||
public static String createQrCode(String type,Integer id, String content) throws IOException {
|
||||
String filePath = "/www/wwwroot/file.ws/qrcode/".concat(type).concat("/");
|
||||
String qrcodeUrl = "https://file.websoft.top/qrcode/".concat(type).concat("/");
|
||||
// 将URL转为BufferedImage
|
||||
BufferedImage bufferedImage = ImageIO.read(new URL(logoUrl));
|
||||
// 生成二维码
|
||||
QrConfig config = new QrConfig(300, 300);
|
||||
// 设置边距,既二维码和背景之间的边距
|
||||
config.setMargin(1);
|
||||
// 附带小logo
|
||||
config.setImg(bufferedImage);
|
||||
// 保存路径
|
||||
filePath = filePath.concat(id + ".jpg");
|
||||
qrcodeUrl = qrcodeUrl.concat(id + ".jpg") + "?v=" + DateUtil.current();
|
||||
|
||||
// 生成二维码
|
||||
QrCodeUtil.generate(content, config, FileUtil.file(filePath));
|
||||
return qrcodeUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.artofsolving.jodconverter.OfficeDocumentConverter;
|
||||
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
|
||||
import org.artofsolving.jodconverter.office.OfficeManager;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* OpenOfficeUtil
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2018-12-14 08:38:19
|
||||
*/
|
||||
public class OpenOfficeUtil {
|
||||
// 支持转换pdf的文件后缀列表
|
||||
private static final String[] CAN_CONVERTER_FILES = new String[]{
|
||||
"doc", "docx", "xls", "xlsx", "ppt", "pptx"
|
||||
};
|
||||
|
||||
/**
|
||||
* 文件转pdf
|
||||
*
|
||||
* @param filePath 源文件路径
|
||||
* @param outDir 输出目录
|
||||
* @param officeHome OpenOffice安装路径
|
||||
* @return File
|
||||
*/
|
||||
public static File converterToPDF(String filePath, String outDir, String officeHome) {
|
||||
return converterToPDF(filePath, outDir, officeHome, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件转pdf
|
||||
*
|
||||
* @param filePath 源文件路径
|
||||
* @param outDir 输出目录
|
||||
* @param officeHome OpenOffice安装路径
|
||||
* @param cache 是否使用上次转换过的文件
|
||||
* @return File
|
||||
*/
|
||||
public static File converterToPDF(String filePath, String outDir, String officeHome, boolean cache) {
|
||||
if (StrUtil.isBlank(filePath)) {
|
||||
return null;
|
||||
}
|
||||
File srcFile = new File(filePath);
|
||||
if (!srcFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
// 是否转换过
|
||||
String outPath = Base64.getEncoder().encodeToString(filePath.getBytes())
|
||||
.replace("/", "-").replace("+", "-");
|
||||
File outFile = new File(outDir, outPath + ".pdf");
|
||||
if (cache && outFile.exists()) {
|
||||
return outFile;
|
||||
}
|
||||
// 转换
|
||||
OfficeManager officeManager = null;
|
||||
try {
|
||||
officeManager = getOfficeManager(officeHome);
|
||||
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
|
||||
return converterFile(srcFile, outFile, converter);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (officeManager != null) {
|
||||
officeManager.stop();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换文件
|
||||
*
|
||||
* @param inFile 源文件
|
||||
* @param outFile 输出文件
|
||||
* @param converter OfficeDocumentConverter
|
||||
* @return File
|
||||
*/
|
||||
public static File converterFile(File inFile, File outFile, OfficeDocumentConverter converter) {
|
||||
if (!outFile.getParentFile().exists()) {
|
||||
if (!outFile.getParentFile().mkdirs()) {
|
||||
return outFile;
|
||||
}
|
||||
}
|
||||
converter.convert(inFile, outFile);
|
||||
return outFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件后缀是否可以转换pdf
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean canConverter(String path) {
|
||||
try {
|
||||
String suffix = path.substring(path.lastIndexOf(".") + 1);
|
||||
return Arrays.asList(CAN_CONVERTER_FILES).contains(suffix);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接并启动OpenOffice
|
||||
*
|
||||
* @param officeHome OpenOffice安装路径
|
||||
* @return OfficeManager
|
||||
*/
|
||||
public static OfficeManager getOfficeManager(String officeHome) {
|
||||
if (officeHome == null || officeHome.trim().isEmpty()) return null;
|
||||
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
|
||||
config.setOfficeHome(officeHome); // 设置OpenOffice安装目录
|
||||
OfficeManager officeManager = config.buildOfficeManager();
|
||||
officeManager.start(); // 启动OpenOffice服务
|
||||
return officeManager;
|
||||
}
|
||||
|
||||
}
|
||||
96
src/main/java/com/gxwebsoft/common/core/utils/PushUtil.java
Normal file
96
src/main/java/com/gxwebsoft/common/core/utils/PushUtil.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.common.system.vo.PushMessageVO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 个推推送消息工具类
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:39
|
||||
*/
|
||||
@Component
|
||||
public class PushUtil {
|
||||
@Resource
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
private static final String url = "https://fc-mp-ba98f1e0-713d-457b-a0a4-27a7939371e6.next.bspapp.com/unipush";
|
||||
|
||||
/**
|
||||
* 获取鉴权token
|
||||
* @return
|
||||
*/
|
||||
public String getToken() {
|
||||
String key = "token:oOVaDtYDYQ8q3lNjhLh401";
|
||||
|
||||
if(redisUtil.get(key) != null){
|
||||
return redisUtil.get(key);
|
||||
}
|
||||
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
long timeMillis = System.currentTimeMillis();
|
||||
String sign = SecureUtil.sha256("AC6IghgsUx7Mwjb7G5eqv" + timeMillis + "JVRkOCXXzA6EyE2Fi5sPr9");
|
||||
map.put("sign",sign);
|
||||
map.put("timestamp",timeMillis);
|
||||
map.put("appkey","AC6IghgsUx7Mwjb7G5eqv");
|
||||
final String body = HttpRequest.post(url.concat("/auth")).body(JSONUtil.toJSONString(map)).execute().body();
|
||||
final JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
final String data = jsonObject.getString("data");
|
||||
final JSONObject jsonData = JSONObject.parseObject(data);
|
||||
|
||||
final String token = jsonData.getString("token");
|
||||
final Long expireTime = Long.valueOf(jsonData.getString("expire_time"));
|
||||
// 保存token
|
||||
|
||||
redisUtil.set(key,token, expireTime-System.currentTimeMillis(), TimeUnit.MILLISECONDS);
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行cid单推
|
||||
* cid数组,只能填一个cid
|
||||
*
|
||||
*/
|
||||
public static boolean toSingle(PushMessageVO pushMessageVO){
|
||||
final String body = HttpRequest.post(url).body(JSONUtil.toJSONString(pushMessageVO)).execute().body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
if("success".equals(jsonObject.get("errMsg"))){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean toSingle(Integer userId,String title, String content,String type, Object obj){
|
||||
PushMessageVO.Payload payload = new PushMessageVO.Payload();
|
||||
payload.setType(type);
|
||||
payload.setData(obj);
|
||||
|
||||
String clientId = userService.getById(userId).getClientId();
|
||||
HashSet<String> clientIds = new HashSet<>();
|
||||
clientIds.add(clientId);
|
||||
|
||||
PushMessageVO messageVO = PushMessageVO.builder().title(title).content(content).payload(payload).push_clientid(clientIds).build();
|
||||
|
||||
final String body = HttpRequest.post(url).body(JSONUtil.toJSONString(messageVO)).execute().body();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(body);
|
||||
if("success".equals(jsonObject.get("errMsg"))){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
282
src/main/java/com/gxwebsoft/common/core/utils/RedisUtil.java
Normal file
282
src/main/java/com/gxwebsoft/common/core/utils/RedisUtil.java
Normal file
@@ -0,0 +1,282 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.result.RedisResult;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.RedisConstants.CACHE_NULL_TTL;
|
||||
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
public static Integer tenantId;
|
||||
|
||||
public RedisUtil(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param entity 实体类对象
|
||||
* 示例 cacheClient.set("merchant:"+id,merchant)
|
||||
*/
|
||||
public <T> void set(String key, T entity){
|
||||
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param entity 实体类对象
|
||||
* 示例 cacheClient.set("merchant:"+id,merchant,1L,TimeUnit.DAYS)
|
||||
*/
|
||||
public <T> void set(String key, T entity, Long time, TimeUnit unit){
|
||||
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJSONString(entity),time,unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存
|
||||
* @param key [表名]:id
|
||||
* 示例 cacheClient.get(key)
|
||||
* @return merchant
|
||||
*/
|
||||
public String get(String key) {
|
||||
return stringRedisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存
|
||||
* @param key [表名]:id
|
||||
* @param clazz Merchant.class
|
||||
* @param <T>
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
* @return merchant
|
||||
*/
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
String json = stringRedisTemplate.opsForValue().get(key);
|
||||
if(StrUtil.isNotBlank(json)){
|
||||
return JSONUtil.parseObject(json, clazz);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写redis缓存(哈希类型)
|
||||
* @param key [表名]:id
|
||||
* @param field 字段
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
*/
|
||||
public <T> void hPut(String key, String field, T entity) {
|
||||
stringRedisTemplate.opsForHash().put(key,field,JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 写redis缓存(哈希类型)
|
||||
* @param key [表名]:id
|
||||
* @param map 字段
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
*/
|
||||
public void hPutAll(String key, Map<String,String> map) {
|
||||
stringRedisTemplate.opsForHash().putAll(key,map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取redis缓存(哈希类型)
|
||||
* 示例 cacheClient.get("merchant:"+id,Merchant.class)
|
||||
* @param key [表名]:id
|
||||
* @param field 字段
|
||||
* @return merchant
|
||||
*/
|
||||
public <T> T hGet(String key, String field, Class<T> clazz) {
|
||||
Object obj = stringRedisTemplate.opsForHash().get(key, field);
|
||||
return JSONUtil.parseObject(JSONUtil.toJSONString(obj),clazz);
|
||||
}
|
||||
|
||||
public List<Object> hValues(String key){
|
||||
return stringRedisTemplate.opsForHash().values(key);
|
||||
}
|
||||
|
||||
public Long hSize(String key){
|
||||
return stringRedisTemplate.opsForHash().size(key);
|
||||
}
|
||||
|
||||
// 逻辑过期方式写入redis
|
||||
public <T> void setWithLogicalExpire(String key, T value, Long time, TimeUnit unit){
|
||||
// 设置逻辑过期时间
|
||||
final RedisResult<T> redisResult = new RedisResult<>();
|
||||
redisResult.setData(value);
|
||||
redisResult.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
|
||||
stringRedisTemplate.opsForValue().set(key,JSONUtil.toJSONString(redisResult));
|
||||
}
|
||||
|
||||
// 读取redis
|
||||
public <R,ID> R query(String keyPrefix, ID id, Class<R> clazz, Function<ID,R> dbFallback, Long time, TimeUnit unit){
|
||||
String key = keyPrefix + id;
|
||||
// 1.从redis查询缓存
|
||||
final String json = stringRedisTemplate.opsForValue().get(key);
|
||||
// 2.判断是否存在
|
||||
if (StrUtil.isNotBlank(json)) {
|
||||
// 3.存在,直接返回
|
||||
return JSONUtil.parseObject(json,clazz);
|
||||
}
|
||||
// 判断命中的是否为空值
|
||||
if (json != null) {
|
||||
return null;
|
||||
}
|
||||
// 4. 不存在,跟进ID查询数据库
|
||||
R r = dbFallback.apply(id);
|
||||
// 5. 数据库不存在,返回错误
|
||||
if(r == null){
|
||||
// 空值写入数据库
|
||||
this.set(key,"",CACHE_NULL_TTL,TimeUnit.MINUTES);
|
||||
return null;
|
||||
}
|
||||
// 写入redis
|
||||
this.set(key,r,time,unit);
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加商户定位点
|
||||
* @param key geo
|
||||
* @param id
|
||||
* 示例 cacheClient.geoAdd("merchant-geo",merchant)
|
||||
*/
|
||||
public <T> void geoAdd(String key, Double x, Double y, String id){
|
||||
stringRedisTemplate.opsForGeo().add(key,new Point(x,y),id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除定位
|
||||
* @param key geo
|
||||
* @param id
|
||||
* 示例 cacheClient.geoRemove("merchant-geo",id)
|
||||
*/
|
||||
public void geoRemove(String key, Integer id){
|
||||
stringRedisTemplate.opsForGeo().remove(key,id.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public <T> void sAdd(String key, T entity){
|
||||
stringRedisTemplate.opsForSet().add(key,JSONUtil.toJSONString(entity));
|
||||
}
|
||||
|
||||
public <T> Set<String> sMembers(String key){
|
||||
return stringRedisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
// 更新排行榜
|
||||
public void zAdd(String key, Integer userId, Double value) {
|
||||
stringRedisTemplate.opsForZSet().add(key,userId.toString(),value);
|
||||
}
|
||||
// 增加元素的score值,并返回增加后的值
|
||||
public Double zIncrementScore(String key,Integer userId, Double delta){
|
||||
return stringRedisTemplate.opsForZSet().incrementScore(key, userId.toString(), delta);
|
||||
}
|
||||
// 获取排名榜
|
||||
public Set<String> range(String key, Integer start, Integer end) {
|
||||
return stringRedisTemplate.opsForZSet().range(key, start, end);
|
||||
}
|
||||
// 获取排名榜
|
||||
public Set<String> reverseRange(String key, Integer start, Integer end){
|
||||
return stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
|
||||
}
|
||||
// 获取分数
|
||||
public Double score(String key, Object value){
|
||||
return stringRedisTemplate.opsForZSet().score(key, value);
|
||||
}
|
||||
|
||||
public void delete(String key){
|
||||
stringRedisTemplate.delete(key);
|
||||
}
|
||||
|
||||
// 存储在list头部
|
||||
public void leftPush(String key, String keyword){
|
||||
stringRedisTemplate.opsForList().leftPush(key,keyword);
|
||||
}
|
||||
|
||||
// 获取列表指定范围内的元素
|
||||
public List<String> listRange(String key,Long start, Long end){
|
||||
return stringRedisTemplate.opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
// 获取列表长度
|
||||
public Long listSize(String key){
|
||||
return stringRedisTemplate.opsForList().size(key);
|
||||
}
|
||||
|
||||
// 裁剪list
|
||||
public void listTrim(String key){
|
||||
stringRedisTemplate.opsForList().trim(key, 0L, 100L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取后台系统设置信息
|
||||
* @param keyName 键名wx-word
|
||||
* @param tenantId 租户ID
|
||||
* @return
|
||||
* key示例 cache10048:setting:wx-work
|
||||
*/
|
||||
public JSONObject getSettingInfo(String keyName,Integer tenantId){
|
||||
String key = "cache" + tenantId + ":setting:" + keyName;
|
||||
final String cache = stringRedisTemplate.opsForValue().get(key);
|
||||
assert cache != null;
|
||||
return JSON.parseObject(cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* KEY前缀
|
||||
* cache[tenantId]:[key+id]
|
||||
*/
|
||||
public static String prefix(String key){
|
||||
String prefix = "cache";
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object object = authentication.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
final Integer tenantId = ((User) object).getTenantId();
|
||||
prefix = prefix.concat(tenantId.toString()).concat(":");
|
||||
}
|
||||
}
|
||||
return prefix.concat(key);
|
||||
}
|
||||
|
||||
// 组装key
|
||||
public String key(String name,Integer id){
|
||||
return name.concat(":").concat(id.toString());
|
||||
}
|
||||
|
||||
// 获取上传配置
|
||||
public HashMap<String, String> getUploadConfig(Integer tenantId){
|
||||
String key = "setting:upload:" + tenantId;
|
||||
final String s = get(key);
|
||||
final JSONObject jsonObject = JSONObject.parseObject(s);
|
||||
final String uploadMethod = jsonObject.getString("uploadMethod");
|
||||
final String bucketDomain = jsonObject.getString("bucketDomain");
|
||||
|
||||
final HashMap<String, String> map = new HashMap<>();
|
||||
map.put("uploadMethod",uploadMethod);
|
||||
map.put("bucketDomain",bucketDomain);
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
155
src/main/java/com/gxwebsoft/common/core/utils/RequestUtil.java
Normal file
155
src/main/java/com/gxwebsoft/common/core/utils/RequestUtil.java
Normal file
@@ -0,0 +1,155 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.system.entity.*;
|
||||
import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Component
|
||||
public class RequestUtil {
|
||||
private static final String SERVER_HOST = "https://server.websoft.top/api";
|
||||
private static final String MODULES_HOST = "https://modules.gxwebsoft.com/api";
|
||||
private static String ACCESS_TOKEN;
|
||||
private static String TENANT_ID;
|
||||
|
||||
public void setTenantId(String tenantId) {
|
||||
TENANT_ID = tenantId;
|
||||
}
|
||||
|
||||
public void setAccessToken(String token) {
|
||||
ACCESS_TOKEN = token;
|
||||
}
|
||||
|
||||
|
||||
// 余额支付通知
|
||||
public void pushBalancePayNotify(Transaction transaction, Payment payment) {
|
||||
System.out.println("payment = " + payment);
|
||||
System.out.println("transaction = " + transaction);
|
||||
// 设置租户ID
|
||||
setTenantId(payment.getTenantId().toString());
|
||||
// 推送支付通知地址
|
||||
String path = payment.getNotifyUrl();
|
||||
try {
|
||||
// 链式构建请求
|
||||
HttpRequest.post(path)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.body(JSONUtil.toJSONString(transaction))//表单内容
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 微信支付通知
|
||||
public void pushWxPayNotify(Transaction transaction, Payment payment) {
|
||||
// 设置租户ID
|
||||
setTenantId(payment.getTenantId().toString());
|
||||
// 推送支付通知地址
|
||||
String path = payment.getNotifyUrl();
|
||||
try {
|
||||
// 链式构建请求
|
||||
HttpRequest.post(path)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.body(JSONUtil.toJSONString(transaction))//表单内容
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public User getMerchantAccountByPhone(String phone) {
|
||||
String path = "/shop/merchant-account/getMerchantAccountByPhone/" + phone;
|
||||
try {
|
||||
// 链式构建请求
|
||||
String result = HttpRequest.get(MODULES_HOST.concat(path))
|
||||
.header("Authorization", ACCESS_TOKEN)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
final String data = jsonObject.getString("data");
|
||||
return JSONObject.parseObject(data, User.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public User getByUserId(Integer userId) {
|
||||
String path = "/system/user/" + userId;
|
||||
try {
|
||||
// 链式构建请求
|
||||
String result = HttpRequest.get(MODULES_HOST.concat(path))
|
||||
.header("Authorization", ACCESS_TOKEN)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
final String data = jsonObject.getString("data");
|
||||
return JSONObject.parseObject(data, User.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public User getUserByPhone(String phone) {
|
||||
String path = "/system/user/getByPhone/" + phone;
|
||||
try {
|
||||
// 链式构建请求
|
||||
String result = HttpRequest.get(SERVER_HOST.concat(path))
|
||||
.header("Authorization", ACCESS_TOKEN)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(result);
|
||||
final String data = jsonObject.getString("data");
|
||||
return JSONObject.parseObject(data, User.class);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 新增用户
|
||||
public boolean saveUserByPhone(MerchantAccount merchantAccount) {
|
||||
String path = "/system/user/";
|
||||
try {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("nickname", merchantAccount.getRealName());
|
||||
map.put("username", merchantAccount.getPhone());
|
||||
map.put("realName", merchantAccount.getRealName());
|
||||
map.put("phone", merchantAccount.getPhone());
|
||||
map.put("password", merchantAccount.getPassword());
|
||||
final ArrayList<Object> roles = new ArrayList<>();
|
||||
final UserRole userRole = new UserRole();
|
||||
userRole.setUserId(merchantAccount.getUserId());
|
||||
userRole.setRoleId(merchantAccount.getRoleId());
|
||||
userRole.setTenantId(merchantAccount.getTenantId());
|
||||
roles.add(userRole);
|
||||
map.put("roles", roles);
|
||||
map.put("tenantId", TENANT_ID);
|
||||
// 链式构建请求
|
||||
String result = HttpRequest.post(SERVER_HOST.concat(path))
|
||||
.header("Authorization", ACCESS_TOKEN)
|
||||
.header("Tenantid", TENANT_ID)
|
||||
.body(JSONUtil.toJSONString(map))//表单内容
|
||||
.timeout(20000)//超时,毫秒
|
||||
.execute().body();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
196
src/main/java/com/gxwebsoft/common/core/utils/SignCheckUtil.java
Normal file
196
src/main/java/com/gxwebsoft/common/core/utils/SignCheckUtil.java
Normal file
@@ -0,0 +1,196 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.system.entity.KVEntity;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 签名检查和获取签名
|
||||
* https://blog.csdn.net/u011628753/article/details/110251445
|
||||
* @author leng
|
||||
*
|
||||
*/
|
||||
public class SignCheckUtil {
|
||||
// 签名字段
|
||||
public final static String SIGN = "sign";
|
||||
|
||||
/**
|
||||
* 签名检查,签名参数中,sign是用于校验的加密值,其他参数按照字母顺序排序,加密,并将其内容链接起来
|
||||
*
|
||||
* @param params
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static boolean signCheck(JSONObject params, String key) {
|
||||
if (null != params) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
params.forEach((k, v) -> {
|
||||
map.put(k, v.toString());
|
||||
});
|
||||
return signCheck(map, key);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 签名检查,签名参数中,sign是用于校验的加密值,其他参数按照字母顺序排序,加密,并将其内容链接起来
|
||||
*
|
||||
* @param params
|
||||
* @param key
|
||||
* 签名key不允许为空
|
||||
* @return
|
||||
*/
|
||||
public static boolean signCheck(Map<String, String> params, String key) {
|
||||
String sign = params.get(SIGN);// 签名
|
||||
if (null == sign) {
|
||||
return false;
|
||||
}
|
||||
String signTemp = getSignString(params,key);
|
||||
if (null == signTemp) {
|
||||
return false;
|
||||
}
|
||||
return signTemp.equals(sign);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名的字符串
|
||||
*
|
||||
* @param params
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String getSignString(JSONObject params, String key) {
|
||||
if (null != params) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
|
||||
params.forEach((k, v) -> {
|
||||
map.put(k, v.toString());
|
||||
});
|
||||
return getSignString(map, key);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名的字符串
|
||||
*
|
||||
* @param params
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String getSignString(Map<String, String> params, String key) {
|
||||
// 签名
|
||||
if (null == params || params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
key = (null == key) ? "" : key;
|
||||
List<KVEntity<String, String>> list = new ArrayList<>(params.size() - 1);
|
||||
|
||||
params.forEach((k, v) -> {
|
||||
if (!SIGN.equals(k)) {
|
||||
list.add(KVEntity.build(k, v));
|
||||
}
|
||||
});
|
||||
|
||||
Collections.sort(list, (obj1, obj2) -> {
|
||||
return obj1.getK().compareTo(obj2.getK());
|
||||
});
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (KVEntity<String, String> kv : list) {
|
||||
String value = kv.getV();
|
||||
if (!StringUtils.isEmpty(value)) {
|
||||
sb.append(kv.getV()).append("-");
|
||||
}
|
||||
}
|
||||
sb.append(key);
|
||||
System.out.println("md5加密前的字符串 = " + sb + key);
|
||||
String signTemp = SecureUtil.md5(sb.toString()).toLowerCase();
|
||||
return signTemp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信签名的字符串
|
||||
*
|
||||
* 注意签名(sign)的生成方式,具体见官方文档(传参都要参与生成签名,且参数名按照字典序排序,最后接上APP_KEY,转化成大写)
|
||||
*
|
||||
* @param params
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String getWXSignString(Map<String, String> params, String key) {
|
||||
// 签名
|
||||
if (null == params || params.size() == 0 || StringUtils.isEmpty(key)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<KVEntity<String, String>> list = new ArrayList<>(params.size() - 1);
|
||||
|
||||
params.forEach((k, v) -> {
|
||||
if (!SIGN.equals(k)) {
|
||||
list.add(KVEntity.build(k, v));
|
||||
}
|
||||
});
|
||||
|
||||
Collections.sort(list, (obj1, obj2) -> {
|
||||
return obj1.getK().compareTo(obj2.getK());
|
||||
});
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (KVEntity<String, String> kv : list) {
|
||||
String value = kv.getV();
|
||||
if (!StringUtils.isEmpty(value)) {
|
||||
sb.append(kv.getK() + "=" + value + "&");
|
||||
}
|
||||
}
|
||||
|
||||
sb.append("key=" + key);
|
||||
String signTemp = SecureUtil.md5(sb.toString()).toLowerCase();
|
||||
return signTemp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信签名验证
|
||||
* @param params
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static boolean WXsignCheck(Map<String, String> params, String key) {
|
||||
String sign = params.get(SIGN);
|
||||
if (StringUtils.isEmpty(sign)) {
|
||||
return false;
|
||||
}
|
||||
return sign.equals(getWXSignString(params, key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 白名单校验
|
||||
* @param domainName abc.com
|
||||
* @return true
|
||||
*/
|
||||
public boolean checkWhiteDomains(List<String> whiteDomains, String domainName) {
|
||||
if(whiteDomains == null){
|
||||
return true;
|
||||
}
|
||||
if (whiteDomains.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
// 服务器域名白名单列表
|
||||
whiteDomains.add("server.gxwebsoft.com");
|
||||
for(String item: whiteDomains){
|
||||
// System.out.println(">>> domainName = " + domainName);
|
||||
if(Objects.equals(item, domainName)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.wechat.pay.java.core.Config;
|
||||
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 微信支付证书自动配置工具类
|
||||
* 使用RSAAutoCertificateConfig实现证书自动管理
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2024-07-26
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatCertAutoConfig {
|
||||
|
||||
/**
|
||||
* 创建微信支付自动证书配置
|
||||
*
|
||||
* @param merchantId 商户号
|
||||
* @param privateKeyPath 私钥文件路径
|
||||
* @param merchantSerialNumber 商户证书序列号
|
||||
* @param apiV3Key APIv3密钥
|
||||
* @return 微信支付配置对象
|
||||
*/
|
||||
public Config createAutoConfig(String merchantId, String privateKeyPath,
|
||||
String merchantSerialNumber, String apiV3Key) {
|
||||
try {
|
||||
log.info("创建微信支付自动证书配置...");
|
||||
log.info("商户号: {}", merchantId);
|
||||
log.info("私钥路径: {}", privateKeyPath);
|
||||
log.info("证书序列号: {}", merchantSerialNumber);
|
||||
|
||||
Config config = new RSAAutoCertificateConfig.Builder()
|
||||
.merchantId(merchantId)
|
||||
.privateKeyFromPath(privateKeyPath)
|
||||
.merchantSerialNumber(merchantSerialNumber)
|
||||
.apiV3Key(apiV3Key)
|
||||
.build();
|
||||
|
||||
log.info("✅ 微信支付自动证书配置创建成功");
|
||||
log.info("🔄 系统将自动管理平台证书的下载和更新");
|
||||
|
||||
return config;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 创建微信支付自动证书配置失败: {}", e.getMessage(), e);
|
||||
|
||||
// 提供详细的错误诊断信息
|
||||
log.error("🔍 错误诊断:");
|
||||
log.error("1. 请检查商户平台是否已开启API安全功能");
|
||||
log.error("2. 请确认已申请使用微信支付公钥");
|
||||
log.error("3. 请验证APIv3密钥和证书序列号是否正确");
|
||||
log.error("4. 请检查网络连接是否正常");
|
||||
log.error("5. 请确认私钥文件路径是否正确: {}", privateKeyPath);
|
||||
|
||||
throw new RuntimeException("微信支付自动证书配置失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用默认开发环境配置创建自动证书配置
|
||||
*
|
||||
* @return 微信支付配置对象
|
||||
*/
|
||||
public Config createDefaultDevConfig() {
|
||||
String merchantId = "1723321338";
|
||||
String privateKeyPath = "src/main/resources/certs/dev/wechat/apiclient_key.pem";
|
||||
String merchantSerialNumber = "2B933F7C35014A1C363642623E4A62364B34C4EB";
|
||||
String apiV3Key = "0kF5OlPr482EZwtn9zGufUcqa7ovgxRL";
|
||||
|
||||
return createAutoConfig(merchantId, privateKeyPath, merchantSerialNumber, apiV3Key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试证书配置是否正常
|
||||
*
|
||||
* @param config 微信支付配置
|
||||
* @return 是否配置成功
|
||||
*/
|
||||
public boolean testConfig(Config config) {
|
||||
try {
|
||||
// 这里可以添加一些基本的配置验证逻辑
|
||||
log.info("🧪 测试微信支付证书配置...");
|
||||
|
||||
if (config == null) {
|
||||
log.error("配置对象为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
log.info("✅ 证书配置测试通过");
|
||||
return true;
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ 证书配置测试失败: {}", e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置使用说明
|
||||
*
|
||||
* @return 使用说明
|
||||
*/
|
||||
public String getUsageInstructions() {
|
||||
return """
|
||||
🚀 微信支付自动证书配置使用说明
|
||||
================================
|
||||
|
||||
✅ 优势:
|
||||
1. 自动下载微信支付平台证书
|
||||
2. 证书过期时自动更新
|
||||
3. 无需手动管理 wechatpay_cert.pem 文件
|
||||
4. 符合微信支付官方最佳实践
|
||||
|
||||
📝 使用方法:
|
||||
|
||||
// 方法1: 使用默认开发环境配置
|
||||
Config config = wechatCertAutoConfig.createDefaultDevConfig();
|
||||
|
||||
// 方法2: 自定义配置
|
||||
Config config = wechatCertAutoConfig.createAutoConfig(
|
||||
"商户号",
|
||||
"私钥路径",
|
||||
"证书序列号",
|
||||
"APIv3密钥"
|
||||
);
|
||||
|
||||
🔧 前置条件:
|
||||
1. 微信商户平台已开启API安全功能
|
||||
2. 已申请使用微信支付公钥
|
||||
3. 私钥文件存在且路径正确
|
||||
4. 网络连接正常
|
||||
|
||||
📚 更多信息:
|
||||
https://pay.weixin.qq.com/doc/v3/merchant/4012153196
|
||||
""";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付证书诊断工具
|
||||
* 专门用于诊断和解决证书相关问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-29
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayCertificateDiagnostic {
|
||||
|
||||
private final CertificateProperties certConfig;
|
||||
private final CertificateLoader certificateLoader;
|
||||
|
||||
public WechatPayCertificateDiagnostic(CertificateProperties certConfig, CertificateLoader certificateLoader) {
|
||||
this.certConfig = certConfig;
|
||||
this.certificateLoader = certificateLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全面诊断微信支付证书配置
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param tenantId 租户ID
|
||||
* @param environment 环境(dev/prod)
|
||||
* @return 诊断结果
|
||||
*/
|
||||
public DiagnosticResult diagnoseCertificateConfig(Payment payment, Integer tenantId, String environment) {
|
||||
DiagnosticResult result = new DiagnosticResult();
|
||||
|
||||
log.info("=== 开始微信支付证书诊断 ===");
|
||||
log.info("租户ID: {}, 环境: {}", tenantId, environment);
|
||||
|
||||
try {
|
||||
// 1. 检查基本配置
|
||||
checkBasicConfig(payment, result);
|
||||
|
||||
// 2. 检查证书文件
|
||||
checkCertificateFiles(payment, tenantId, environment, result);
|
||||
|
||||
// 3. 检查证书内容
|
||||
validateCertificateContent(payment, tenantId, environment, result);
|
||||
|
||||
// 4. 生成建议
|
||||
generateRecommendations(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
result.addError("诊断过程中发生异常: " + e.getMessage());
|
||||
log.error("证书诊断异常", e);
|
||||
}
|
||||
|
||||
log.info("=== 证书诊断完成 ===");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查基本配置
|
||||
*/
|
||||
private void checkBasicConfig(Payment payment, DiagnosticResult result) {
|
||||
if (payment == null) {
|
||||
result.addError("支付配置为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (payment.getMchId() == null || payment.getMchId().trim().isEmpty()) {
|
||||
result.addError("商户号未配置");
|
||||
} else {
|
||||
result.addInfo("商户号: " + payment.getMchId());
|
||||
}
|
||||
|
||||
if (payment.getAppId() == null || payment.getAppId().trim().isEmpty()) {
|
||||
result.addError("应用ID未配置");
|
||||
} else {
|
||||
result.addInfo("应用ID: " + payment.getAppId());
|
||||
}
|
||||
|
||||
if (payment.getMerchantSerialNumber() == null || payment.getMerchantSerialNumber().trim().isEmpty()) {
|
||||
result.addError("商户证书序列号未配置");
|
||||
} else {
|
||||
result.addInfo("商户证书序列号: " + payment.getMerchantSerialNumber());
|
||||
}
|
||||
|
||||
if (payment.getApiKey() == null || payment.getApiKey().trim().isEmpty()) {
|
||||
result.addWarning("数据库中APIv3密钥未配置,将使用配置文件默认值");
|
||||
} else {
|
||||
result.addInfo("APIv3密钥: 已配置(" + payment.getApiKey().length() + "位)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书文件
|
||||
*/
|
||||
private void checkCertificateFiles(Payment payment, Integer tenantId, String environment, DiagnosticResult result) {
|
||||
if ("dev".equals(environment)) {
|
||||
// 开发环境证书检查
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
// 检查私钥文件
|
||||
if (certificateLoader.certificateExists(privateKeyPath)) {
|
||||
result.addInfo("✅ 私钥文件存在: " + privateKeyPath);
|
||||
try {
|
||||
String privateKeyFile = certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
result.addInfo("私钥文件路径: " + privateKeyFile);
|
||||
} catch (Exception e) {
|
||||
result.addError("私钥文件加载失败: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
result.addError("❌ 私钥文件不存在: " + privateKeyPath);
|
||||
}
|
||||
|
||||
// 检查商户证书文件
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
result.addInfo("✅ 商户证书文件存在: " + apiclientCertPath);
|
||||
} else {
|
||||
result.addWarning("⚠️ 商户证书文件不存在: " + apiclientCertPath + " (自动证书配置不需要此文件)");
|
||||
}
|
||||
|
||||
} else {
|
||||
// 生产环境证书检查
|
||||
if (payment.getApiclientKey() != null) {
|
||||
result.addInfo("私钥文件配置: " + payment.getApiclientKey());
|
||||
} else {
|
||||
result.addError("生产环境私钥文件路径未配置");
|
||||
}
|
||||
|
||||
if (payment.getApiclientCert() != null) {
|
||||
result.addInfo("商户证书文件配置: " + payment.getApiclientCert());
|
||||
} else {
|
||||
result.addWarning("生产环境商户证书文件路径未配置 (自动证书配置不需要此文件)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证证书内容
|
||||
*/
|
||||
private void validateCertificateContent(Payment payment, Integer tenantId, String environment, DiagnosticResult result) {
|
||||
try {
|
||||
if ("dev".equals(environment)) {
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String apiclientCertPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getApiclientCertFile();
|
||||
|
||||
if (certificateLoader.certificateExists(apiclientCertPath)) {
|
||||
validateX509Certificate(apiclientCertPath, payment.getMerchantSerialNumber(), result);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.addWarning("证书内容验证失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证X509证书
|
||||
*/
|
||||
private void validateX509Certificate(String certPath, String expectedSerialNumber, DiagnosticResult result) {
|
||||
try {
|
||||
String actualCertPath = certificateLoader.loadCertificatePath(certPath);
|
||||
|
||||
try (InputStream inputStream = new FileInputStream(new File(actualCertPath))) {
|
||||
CertificateFactory cf = CertificateFactory.getInstance("X.509");
|
||||
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
|
||||
|
||||
if (cert != null) {
|
||||
String actualSerialNumber = cert.getSerialNumber().toString(16).toUpperCase();
|
||||
result.addInfo("证书序列号: " + actualSerialNumber);
|
||||
result.addInfo("证书有效期: " + cert.getNotBefore() + " 至 " + cert.getNotAfter());
|
||||
result.addInfo("证书主体: " + cert.getSubjectX500Principal().toString());
|
||||
|
||||
// 检查序列号是否匹配
|
||||
if (expectedSerialNumber != null && !expectedSerialNumber.equalsIgnoreCase(actualSerialNumber)) {
|
||||
result.addError("证书序列号不匹配! 配置: " + expectedSerialNumber + ", 实际: " + actualSerialNumber);
|
||||
} else {
|
||||
result.addInfo("✅ 证书序列号匹配");
|
||||
}
|
||||
|
||||
// 检查证书是否过期
|
||||
long now = System.currentTimeMillis();
|
||||
if (now < cert.getNotBefore().getTime()) {
|
||||
result.addError("证书尚未生效");
|
||||
} else if (now > cert.getNotAfter().getTime()) {
|
||||
result.addError("证书已过期");
|
||||
} else {
|
||||
result.addInfo("✅ 证书在有效期内");
|
||||
}
|
||||
} else {
|
||||
result.addError("无法解析证书文件");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.addError("证书验证失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成建议
|
||||
*/
|
||||
private void generateRecommendations(DiagnosticResult result) {
|
||||
if (result.hasErrors()) {
|
||||
result.addRecommendation("🔧 修复建议:");
|
||||
|
||||
String errorText = result.getErrors();
|
||||
if (errorText.contains("商户号")) {
|
||||
result.addRecommendation("1. 请在支付配置中设置正确的商户号");
|
||||
}
|
||||
|
||||
if (errorText.contains("序列号")) {
|
||||
result.addRecommendation("2. 请检查商户证书序列号是否正确,可在微信商户平台查看");
|
||||
}
|
||||
|
||||
if (errorText.contains("证书文件")) {
|
||||
result.addRecommendation("3. 请确保证书文件已正确放置在指定目录");
|
||||
}
|
||||
|
||||
if (errorText.contains("过期")) {
|
||||
result.addRecommendation("4. 请更新过期的证书文件");
|
||||
}
|
||||
|
||||
result.addRecommendation("5. 建议使用RSAAutoCertificateConfig自动证书配置,可避免手动管理证书");
|
||||
result.addRecommendation("6. 确保在微信商户平台开启API安全功能并申请使用微信支付公钥");
|
||||
} else {
|
||||
result.addRecommendation("✅ 证书配置正常,建议使用自动证书配置以获得最佳体验");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 诊断结果类
|
||||
*/
|
||||
public static class DiagnosticResult {
|
||||
private final StringBuilder errors = new StringBuilder();
|
||||
private final StringBuilder warnings = new StringBuilder();
|
||||
private final StringBuilder info = new StringBuilder();
|
||||
private final StringBuilder recommendations = new StringBuilder();
|
||||
|
||||
public void addError(String error) {
|
||||
if (errors.length() > 0) errors.append("\n");
|
||||
errors.append(error);
|
||||
}
|
||||
|
||||
public void addWarning(String warning) {
|
||||
if (warnings.length() > 0) warnings.append("\n");
|
||||
warnings.append(warning);
|
||||
}
|
||||
|
||||
public void addInfo(String information) {
|
||||
if (info.length() > 0) info.append("\n");
|
||||
info.append(information);
|
||||
}
|
||||
|
||||
public void addRecommendation(String recommendation) {
|
||||
if (recommendations.length() > 0) recommendations.append("\n");
|
||||
recommendations.append(recommendation);
|
||||
}
|
||||
|
||||
public boolean hasErrors() {
|
||||
return errors.length() > 0;
|
||||
}
|
||||
|
||||
public String getErrors() {
|
||||
return errors.toString();
|
||||
}
|
||||
|
||||
public String getWarnings() {
|
||||
return warnings.toString();
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return info.toString();
|
||||
}
|
||||
|
||||
public String getRecommendations() {
|
||||
return recommendations.toString();
|
||||
}
|
||||
|
||||
public String getFullReport() {
|
||||
StringBuilder report = new StringBuilder();
|
||||
report.append("=== 微信支付证书诊断报告 ===\n\n");
|
||||
|
||||
if (info.length() > 0) {
|
||||
report.append("📋 基本信息:\n").append(info).append("\n\n");
|
||||
}
|
||||
|
||||
if (warnings.length() > 0) {
|
||||
report.append("⚠️ 警告:\n").append(warnings).append("\n\n");
|
||||
}
|
||||
|
||||
if (errors.length() > 0) {
|
||||
report.append("❌ 错误:\n").append(errors).append("\n\n");
|
||||
}
|
||||
|
||||
if (recommendations.length() > 0) {
|
||||
report.append("💡 建议:\n").append(recommendations).append("\n\n");
|
||||
}
|
||||
|
||||
report.append("=== 诊断报告结束 ===");
|
||||
return report.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.core.config.CertificateProperties;
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* 微信支付配置验证工具
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-27
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayConfigValidator {
|
||||
|
||||
private final CertificateProperties certConfig;
|
||||
private final CertificateLoader certificateLoader;
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String activeProfile;
|
||||
|
||||
public WechatPayConfigValidator(CertificateProperties certConfig, CertificateLoader certificateLoader) {
|
||||
this.certConfig = certConfig;
|
||||
this.certificateLoader = certificateLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证微信支付配置
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param tenantId 租户ID
|
||||
* @return 验证结果
|
||||
*/
|
||||
public ValidationResult validateWechatPayConfig(Payment payment, Integer tenantId) {
|
||||
ValidationResult result = new ValidationResult();
|
||||
|
||||
log.info("开始验证微信支付配置 - 租户ID: {}", tenantId);
|
||||
|
||||
// 1. 验证基本配置
|
||||
if (payment == null) {
|
||||
result.addError("支付配置为空");
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(payment.getMchId())) {
|
||||
result.addError("商户号未配置");
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(payment.getAppId())) {
|
||||
result.addError("应用ID未配置");
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(payment.getMerchantSerialNumber())) {
|
||||
result.addError("商户证书序列号未配置");
|
||||
}
|
||||
|
||||
// 2. 验证 APIv3 密钥
|
||||
String apiV3Key = getValidApiV3Key(payment);
|
||||
if (!StringUtils.hasText(apiV3Key)) {
|
||||
result.addError("APIv3密钥未配置");
|
||||
} else {
|
||||
validateApiV3Key(apiV3Key, result);
|
||||
}
|
||||
|
||||
// 3. 验证证书文件
|
||||
validateCertificateFiles(tenantId, result);
|
||||
|
||||
// 4. 记录验证结果
|
||||
if (result.isValid()) {
|
||||
log.info("✅ 微信支付配置验证通过 - 租户ID: {}", tenantId);
|
||||
} else {
|
||||
log.error("❌ 微信支付配置验证失败 - 租户ID: {}, 错误: {}", tenantId, result.getErrors());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取有效的 APIv3 密钥
|
||||
* 优先使用数据库配置,如果为空则使用配置文件默认值
|
||||
*/
|
||||
public String getValidApiV3Key(Payment payment) {
|
||||
String apiV3Key = payment.getApiKey();
|
||||
|
||||
if (!StringUtils.hasText(apiV3Key)) {
|
||||
apiV3Key = certConfig.getWechatPay().getDev().getApiV3Key();
|
||||
log.warn("数据库中APIv3密钥为空,使用配置文件默认值");
|
||||
}
|
||||
|
||||
return apiV3Key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证 APIv3 密钥格式
|
||||
*/
|
||||
private void validateApiV3Key(String apiV3Key, ValidationResult result) {
|
||||
if (apiV3Key.length() != 32) {
|
||||
result.addError("APIv3密钥长度错误,应为32位,实际为: " + apiV3Key.length());
|
||||
}
|
||||
|
||||
if (!apiV3Key.matches("^[a-zA-Z0-9]+$")) {
|
||||
result.addError("APIv3密钥格式错误,应仅包含字母和数字");
|
||||
}
|
||||
|
||||
log.info("APIv3密钥验证 - 长度: {}, 格式: {}",
|
||||
apiV3Key.length(),
|
||||
apiV3Key.matches("^[a-zA-Z0-9]+$") ? "正确" : "错误");
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证证书文件
|
||||
*/
|
||||
private void validateCertificateFiles(Integer tenantId, ValidationResult result) {
|
||||
if ("dev".equals(activeProfile)) {
|
||||
// 开发环境证书验证
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
|
||||
if (!certificateLoader.certificateExists(privateKeyPath)) {
|
||||
result.addError("证书文件不存在: " + privateKeyPath);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
certificateLoader.loadCertificatePath(privateKeyPath);
|
||||
log.info("✅ 开发环境证书文件验证通过: {}", privateKeyPath);
|
||||
} catch (Exception e) {
|
||||
result.addError("证书文件加载失败: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
// 生产环境证书验证 - 跳过文件存在性检查,因为证书路径来自数据库
|
||||
log.info("✅ 生产环境跳过证书文件存在性验证,使用数据库配置的证书路径");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证结果类
|
||||
*/
|
||||
public static class ValidationResult {
|
||||
private boolean valid = true;
|
||||
private StringBuilder errors = new StringBuilder();
|
||||
|
||||
public void addError(String error) {
|
||||
this.valid = false;
|
||||
if (errors.length() > 0) {
|
||||
errors.append("; ");
|
||||
}
|
||||
errors.append(error);
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public String getErrors() {
|
||||
return errors.toString();
|
||||
}
|
||||
|
||||
public void logErrors() {
|
||||
if (!valid) {
|
||||
log.error("配置验证失败: {}", errors.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成配置诊断报告
|
||||
*/
|
||||
public String generateDiagnosticReport(Payment payment, Integer tenantId) {
|
||||
StringBuilder report = new StringBuilder();
|
||||
report.append("=== 微信支付配置诊断报告 ===\n");
|
||||
report.append("租户ID: ").append(tenantId).append("\n");
|
||||
|
||||
if (payment != null) {
|
||||
report.append("商户号: ").append(payment.getMchId()).append("\n");
|
||||
report.append("应用ID: ").append(payment.getAppId()).append("\n");
|
||||
report.append("商户证书序列号: ").append(payment.getMerchantSerialNumber()).append("\n");
|
||||
|
||||
String dbApiKey = payment.getApiKey();
|
||||
String configApiKey = certConfig.getWechatPay().getDev().getApiV3Key();
|
||||
|
||||
report.append("数据库APIv3密钥: ").append(dbApiKey != null ? "已配置(" + dbApiKey.length() + "位)" : "未配置").append("\n");
|
||||
report.append("配置文件APIv3密钥: ").append(configApiKey != null ? "已配置(" + configApiKey.length() + "位)" : "未配置").append("\n");
|
||||
|
||||
String finalApiKey = getValidApiV3Key(payment);
|
||||
report.append("最终使用APIv3密钥: ").append(finalApiKey != null ? "已配置(" + finalApiKey.length() + "位)" : "未配置").append("\n");
|
||||
|
||||
} else {
|
||||
report.append("❌ 支付配置为空\n");
|
||||
}
|
||||
|
||||
// 证书文件检查
|
||||
report.append("当前环境: ").append(activeProfile).append("\n");
|
||||
if ("dev".equals(activeProfile)) {
|
||||
String tenantCertPath = "dev/wechat/" + tenantId;
|
||||
String privateKeyPath = tenantCertPath + "/" + certConfig.getWechatPay().getDev().getPrivateKeyFile();
|
||||
boolean certExists = certificateLoader.certificateExists(privateKeyPath);
|
||||
|
||||
report.append("开发环境证书文件路径: ").append(privateKeyPath).append("\n");
|
||||
report.append("证书文件存在: ").append(certExists ? "是" : "否").append("\n");
|
||||
} else {
|
||||
report.append("生产环境证书路径: 从数据库配置获取\n");
|
||||
if (payment != null) {
|
||||
report.append("私钥文件: ").append(payment.getApiclientKey()).append("\n");
|
||||
report.append("证书文件: ").append(payment.getApiclientCert()).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
ValidationResult validation = validateWechatPayConfig(payment, tenantId);
|
||||
report.append("配置验证结果: ").append(validation.isValid() ? "通过" : "失败").append("\n");
|
||||
if (!validation.isValid()) {
|
||||
report.append("验证错误: ").append(validation.getErrors()).append("\n");
|
||||
}
|
||||
|
||||
report.append("=== 诊断报告结束 ===");
|
||||
|
||||
return report.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.gxwebsoft.common.system.entity.Payment;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* 微信支付配置诊断工具
|
||||
* 用于排查微信支付签名验证失败等问题
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-07-27
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatPayDiagnostic {
|
||||
|
||||
/**
|
||||
* 诊断微信支付配置
|
||||
*
|
||||
* @param payment 支付配置
|
||||
* @param privateKeyPath 私钥路径
|
||||
* @param environment 环境标识
|
||||
*/
|
||||
public void diagnosePaymentConfig(Payment payment, String privateKeyPath, String environment) {
|
||||
log.info("=== 微信支付配置诊断开始 ===");
|
||||
log.info("环境: {}", environment);
|
||||
|
||||
// 1. 检查支付配置基本信息
|
||||
checkBasicConfig(payment);
|
||||
|
||||
// 2. 检查证书文件
|
||||
checkCertificateFiles(payment, privateKeyPath, environment);
|
||||
|
||||
// 3. 检查配置完整性
|
||||
checkConfigCompleteness(payment);
|
||||
|
||||
log.info("=== 微信支付配置诊断结束 ===");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查基本配置信息
|
||||
*/
|
||||
private void checkBasicConfig(Payment payment) {
|
||||
log.info("--- 基本配置检查 ---");
|
||||
|
||||
if (payment == null) {
|
||||
log.error("❌ 支付配置为空");
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("支付配置ID: {}", payment.getId());
|
||||
log.info("支付方式名称: {}", payment.getName());
|
||||
log.info("支付类型: {}", payment.getType());
|
||||
log.info("支付代码: {}", payment.getCode());
|
||||
log.info("状态: {}", payment.getStatus());
|
||||
|
||||
// 检查关键字段
|
||||
checkField("应用ID", payment.getAppId());
|
||||
checkField("商户号", payment.getMchId());
|
||||
checkField("商户证书序列号", payment.getMerchantSerialNumber());
|
||||
checkField("API密钥", payment.getApiKey(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查证书文件
|
||||
*/
|
||||
private void checkCertificateFiles(Payment payment, String privateKeyPath, String environment) {
|
||||
log.info("--- 证书文件检查 ---");
|
||||
|
||||
// 检查私钥文件
|
||||
if (privateKeyPath != null) {
|
||||
checkFileExists("私钥文件", privateKeyPath);
|
||||
}
|
||||
|
||||
// 生产环境检查证书文件
|
||||
if (!"dev".equals(environment)) {
|
||||
if (payment.getApiclientCert() != null) {
|
||||
log.info("商户证书文件配置: {}", payment.getApiclientCert());
|
||||
}
|
||||
|
||||
if (payment.getPubKey() != null) {
|
||||
log.info("公钥文件配置: {}", payment.getPubKey());
|
||||
log.info("公钥ID: {}", payment.getPubKeyId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查配置完整性
|
||||
*/
|
||||
private void checkConfigCompleteness(Payment payment) {
|
||||
log.info("--- 配置完整性检查 ---");
|
||||
|
||||
boolean isComplete = true;
|
||||
|
||||
if (isEmpty(payment.getMchId())) {
|
||||
log.error("❌ 商户号未配置");
|
||||
isComplete = false;
|
||||
}
|
||||
|
||||
if (isEmpty(payment.getMerchantSerialNumber())) {
|
||||
log.error("❌ 商户证书序列号未配置");
|
||||
isComplete = false;
|
||||
}
|
||||
|
||||
if (isEmpty(payment.getApiKey())) {
|
||||
log.error("❌ API密钥未配置");
|
||||
isComplete = false;
|
||||
}
|
||||
|
||||
if (isEmpty(payment.getAppId())) {
|
||||
log.error("❌ 应用ID未配置");
|
||||
isComplete = false;
|
||||
}
|
||||
|
||||
if (isComplete) {
|
||||
log.info("✅ 配置完整性检查通过");
|
||||
} else {
|
||||
log.error("❌ 配置不完整,请补充缺失的配置项");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段是否为空
|
||||
*/
|
||||
private void checkField(String fieldName, String value) {
|
||||
checkField(fieldName, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字段是否为空
|
||||
*/
|
||||
private void checkField(String fieldName, String value, boolean isSensitive) {
|
||||
if (isEmpty(value)) {
|
||||
log.warn("⚠️ {}: 未配置", fieldName);
|
||||
} else {
|
||||
if (isSensitive) {
|
||||
log.info("✅ {}: 已配置(长度:{})", fieldName, value.length());
|
||||
} else {
|
||||
log.info("✅ {}: {}", fieldName, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文件是否存在
|
||||
*/
|
||||
private void checkFileExists(String fileName, String filePath) {
|
||||
try {
|
||||
File file = new File(filePath);
|
||||
if (file.exists() && file.isFile()) {
|
||||
log.info("✅ {}: 文件存在 - {}", fileName, filePath);
|
||||
log.info(" 文件大小: {} bytes", file.length());
|
||||
|
||||
// 检查文件内容格式
|
||||
if (filePath.endsWith(".pem")) {
|
||||
checkPemFileFormat(fileName, filePath);
|
||||
}
|
||||
} else {
|
||||
log.error("❌ {}: 文件不存在 - {}", fileName, filePath);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("❌ {}: 检查文件时出错 - {} ({})", fileName, filePath, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查PEM文件格式
|
||||
*/
|
||||
private void checkPemFileFormat(String fileName, String filePath) {
|
||||
try {
|
||||
String content = Files.readString(Paths.get(filePath));
|
||||
if (content.contains("-----BEGIN") && content.contains("-----END")) {
|
||||
log.info("✅ {}: PEM格式正确", fileName);
|
||||
} else {
|
||||
log.warn("⚠️ {}: PEM格式可能有问题", fileName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("⚠️ {}: 无法读取文件内容进行格式检查 ({})", fileName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查字符串是否为空
|
||||
*/
|
||||
private boolean isEmpty(String str) {
|
||||
return str == null || str.trim().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成诊断报告
|
||||
*/
|
||||
public String generateDiagnosticReport(Payment payment, String environment) {
|
||||
StringBuilder report = new StringBuilder();
|
||||
report.append("🔍 微信支付配置诊断报告\n");
|
||||
report.append("========================\n\n");
|
||||
|
||||
report.append("环境: ").append(environment).append("\n");
|
||||
report.append("租户ID: ").append(payment != null ? payment.getTenantId() : "未知").append("\n");
|
||||
report.append("商户号: ").append(payment != null ? payment.getMchId() : "未配置").append("\n");
|
||||
report.append("应用ID: ").append(payment != null ? payment.getAppId() : "未配置").append("\n\n");
|
||||
|
||||
report.append("🚨 常见问题排查:\n");
|
||||
report.append("1. 商户证书序列号是否正确\n");
|
||||
report.append("2. APIv3密钥是否正确\n");
|
||||
report.append("3. 私钥文件是否正确\n");
|
||||
report.append("4. 微信支付平台证书是否过期\n");
|
||||
report.append("5. 网络连接是否正常\n\n");
|
||||
|
||||
report.append("💡 建议解决方案:\n");
|
||||
report.append("1. 使用自动证书配置(RSAAutoCertificateConfig)\n");
|
||||
report.append("2. 在微信商户平台重新下载证书\n");
|
||||
report.append("3. 检查商户平台API安全设置\n");
|
||||
|
||||
return report.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 微信支付工具类
|
||||
* 处理微信支付API的字段限制和格式要求
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2025-01-11
|
||||
*/
|
||||
public class WechatPayUtils {
|
||||
|
||||
/**
|
||||
* 微信支付description字段的最大字节数限制
|
||||
*/
|
||||
public static final int DESCRIPTION_MAX_BYTES = 127;
|
||||
|
||||
/**
|
||||
* 微信支付attach字段的最大字节数限制
|
||||
*/
|
||||
public static final int ATTACH_MAX_BYTES = 127;
|
||||
|
||||
/**
|
||||
* 截断字符串以确保字节数不超过指定限制
|
||||
* 主要用于微信支付API的字段限制处理
|
||||
*
|
||||
* @param text 原始文本
|
||||
* @param maxBytes 最大字节数
|
||||
* @return 截断后的文本,确保UTF-8字符完整性
|
||||
*/
|
||||
public static String truncateToByteLimit(String text, int maxBytes) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
|
||||
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
|
||||
if (bytes.length <= maxBytes) {
|
||||
return text;
|
||||
}
|
||||
|
||||
// 截断字节数组,但要确保不会截断UTF-8字符的中间
|
||||
int truncateLength = maxBytes;
|
||||
while (truncateLength > 0) {
|
||||
byte[] truncated = new byte[truncateLength];
|
||||
System.arraycopy(bytes, 0, truncated, 0, truncateLength);
|
||||
|
||||
try {
|
||||
String result = new String(truncated, StandardCharsets.UTF_8);
|
||||
// 检查是否有无效字符(被截断的UTF-8字符)
|
||||
if (!result.contains("\uFFFD")) {
|
||||
return result;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 继续尝试更短的长度
|
||||
}
|
||||
truncateLength--;
|
||||
}
|
||||
|
||||
return ""; // 如果无法安全截断,返回空字符串
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信支付商品描述字段
|
||||
* 确保字节数不超过127字节
|
||||
*
|
||||
* @param description 商品描述
|
||||
* @return 处理后的描述,符合微信支付要求
|
||||
*/
|
||||
public static String processDescription(String description) {
|
||||
return truncateToByteLimit(description, DESCRIPTION_MAX_BYTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理微信支付附加数据字段
|
||||
* 确保字节数不超过127字节
|
||||
*
|
||||
* @param attach 附加数据
|
||||
* @return 处理后的附加数据,符合微信支付要求
|
||||
*/
|
||||
public static String processAttach(String attach) {
|
||||
return truncateToByteLimit(attach, ATTACH_MAX_BYTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证字符串是否符合微信支付字段的字节限制
|
||||
*
|
||||
* @param text 待验证的文本
|
||||
* @param maxBytes 最大字节数限制
|
||||
* @return true如果符合限制,false如果超出限制
|
||||
*/
|
||||
public static boolean isWithinByteLimit(String text, int maxBytes) {
|
||||
if (text == null) {
|
||||
return true;
|
||||
}
|
||||
return text.getBytes(StandardCharsets.UTF_8).length <= maxBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字符串的UTF-8字节数
|
||||
*
|
||||
* @param text 文本
|
||||
* @return 字节数
|
||||
*/
|
||||
public static int getByteLength(String text) {
|
||||
if (text == null) {
|
||||
return 0;
|
||||
}
|
||||
return text.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
/**
|
||||
* 微信小程序数据解密工具类
|
||||
* 用于解密微信小程序的encryptedData数据
|
||||
*
|
||||
* @author WebSoft
|
||||
*/
|
||||
public class WxMiniProgramDecryptUtil {
|
||||
|
||||
/**
|
||||
* 解密微信小程序数据
|
||||
*
|
||||
* @param encryptedData 加密的数据
|
||||
* @param sessionKey 会话密钥
|
||||
* @param iv 初始向量
|
||||
* @return 解密后的JSON字符串
|
||||
* @throws Exception 解密失败时抛出异常
|
||||
*/
|
||||
public static String decrypt(String encryptedData, String sessionKey, String iv) throws Exception {
|
||||
// Base64解码
|
||||
byte[] dataByte = Base64.decodeBase64(encryptedData);
|
||||
byte[] keyByte = Base64.decodeBase64(sessionKey);
|
||||
byte[] ivByte = Base64.decodeBase64(iv);
|
||||
|
||||
try {
|
||||
// 如果密钥长度不够,则补齐到32位
|
||||
if (keyByte.length % 16 != 0) {
|
||||
int groups = keyByte.length / 16 + (keyByte.length % 16 != 0 ? 1 : 0);
|
||||
byte[] temp = new byte[groups * 16];
|
||||
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
|
||||
keyByte = temp;
|
||||
}
|
||||
|
||||
// 初始化
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
|
||||
AlgorithmParameterSpec paramSpec = new IvParameterSpec(ivByte);
|
||||
cipher.init(Cipher.DECRYPT_MODE, spec, paramSpec);
|
||||
|
||||
// 解密
|
||||
byte[] resultByte = cipher.doFinal(dataByte);
|
||||
if (null != resultByte && resultByte.length > 0) {
|
||||
String result = new String(resultByte, StandardCharsets.UTF_8);
|
||||
return result;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new Exception("微信小程序数据解密失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密手机号信息
|
||||
*
|
||||
* @param encryptedData 加密的数据
|
||||
* @param sessionKey 会话密钥
|
||||
* @param iv 初始向量
|
||||
* @return 手机号码,解密失败返回null
|
||||
*/
|
||||
public static String decryptPhoneNumber(String encryptedData, String sessionKey, String iv) {
|
||||
try {
|
||||
String decryptedData = decrypt(encryptedData, sessionKey, iv);
|
||||
if (decryptedData != null) {
|
||||
JSONObject jsonObject = JSON.parseObject(decryptedData);
|
||||
return jsonObject.getString("phoneNumber");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("解密手机号失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密用户信息
|
||||
*
|
||||
* @param encryptedData 加密的数据
|
||||
* @param sessionKey 会话密钥
|
||||
* @param iv 初始向量
|
||||
* @return 解密后的用户信息JSON对象,解密失败返回null
|
||||
*/
|
||||
public static JSONObject decryptUserInfo(String encryptedData, String sessionKey, String iv) {
|
||||
try {
|
||||
String decryptedData = decrypt(encryptedData, sessionKey, iv);
|
||||
if (decryptedData != null) {
|
||||
return JSON.parseObject(decryptedData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("解密用户信息失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import com.wechat.pay.java.core.Config;
|
||||
|
||||
|
||||
public class WxNativeUtil {
|
||||
|
||||
private static final Map<Integer, Config> tenantConfigs = new HashMap<>();
|
||||
|
||||
public static void addConfig(Integer tenantId, Config config) {
|
||||
tenantConfigs.put(tenantId, config);
|
||||
}
|
||||
|
||||
public static Config getConfig(Integer tenantId) {
|
||||
return tenantConfigs.get(tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import com.gxwebsoft.common.system.service.SettingService;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* 微信公众号工具类
|
||||
* @author 科技小王子
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class WxOfficialUtil {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
private Integer tenantId;
|
||||
public String appId;
|
||||
public String appSecret;
|
||||
public String openid;
|
||||
public String unionid;
|
||||
public String access_token;
|
||||
public String expires_in;
|
||||
public String nickname;
|
||||
|
||||
|
||||
@Resource
|
||||
private SettingService settingService;
|
||||
@Resource
|
||||
private ConfigProperties pathConfig;
|
||||
@Resource
|
||||
private CacheClient cacheClient;
|
||||
|
||||
public WxOfficialUtil(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
// 实例化客户端
|
||||
public WxOfficialUtil client(Integer tenantId) {
|
||||
if(tenantId > 0){
|
||||
throw new BusinessException(tenantId + "123123");
|
||||
}
|
||||
this.tenantId = tenantId;
|
||||
this.config();
|
||||
System.out.println("this.tenantId = " + this.tenantId);
|
||||
return this;
|
||||
}
|
||||
|
||||
// 开发者ID和秘钥
|
||||
private void config() {
|
||||
String key = "cache"+ this.tenantId +":setting:wx-official";
|
||||
String wxOfficial = stringRedisTemplate.opsForValue().get(key);
|
||||
JSONObject data = JSONObject.parseObject(wxOfficial);
|
||||
if(data != null){
|
||||
this.appId = data.getString("appId");
|
||||
this.appSecret = data.getString("appSecret");
|
||||
}
|
||||
System.out.println("this.appId = " + this.appId);
|
||||
System.out.println("this.appSecret = " + this.appSecret);
|
||||
}
|
||||
|
||||
// 获取appId
|
||||
public String getAppSecret(){
|
||||
return this.appSecret;
|
||||
}
|
||||
|
||||
public String getCodeUrl() throws UnsupportedEncodingException {
|
||||
String encodedReturnUrl = URLEncoder.encode("https://server.websoft.top/api/open/wx-official/accessToken","UTF-8");
|
||||
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ this.appId +"&redirect_uri=" + encodedReturnUrl + "&response_type=code&scope=snsapi_userinfo&state="+ this.tenantId +"#wechat_redirect";
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
public String getAccessToken(String code) {
|
||||
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ this.appId +"&secret="+ this.appSecret +"&code="+ code +"&grant_type=authorization_code";
|
||||
System.out.println("url = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
final JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
access_token = jsonObject.getString("access_token");
|
||||
if(access_token == null){
|
||||
throw new BusinessException("获取access_token失败");
|
||||
}
|
||||
this.openid = jsonObject.getString("openid");
|
||||
this.unionid = jsonObject.getString("unionid");
|
||||
this.expires_in = jsonObject.getString("expires_in");
|
||||
return access_token;
|
||||
}
|
||||
|
||||
// 获取userinfo
|
||||
public JSONObject getUserInfo(String access_token) {
|
||||
String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+ access_token +"&openid="+ this.openid +"&lang=zh_CN";
|
||||
System.out.println("url2 = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response = " + response);
|
||||
if(response == null){
|
||||
throw new BusinessException("获取userinfo失败");
|
||||
}
|
||||
return JSONObject.parseObject(response);
|
||||
}
|
||||
}
|
||||
134
src/main/java/com/gxwebsoft/common/core/utils/WxUtil.java
Normal file
134
src/main/java/com/gxwebsoft/common/core/utils/WxUtil.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 微信小程序工具类
|
||||
* @author 科技小王子
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class WxUtil {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
private Integer tenantId;
|
||||
public String appId;
|
||||
public String appSecret;
|
||||
public String access_token;
|
||||
public String expires_in;
|
||||
public String nickname;
|
||||
public String userid;
|
||||
public String user_ticket;
|
||||
public String openid;
|
||||
public String external_userid;
|
||||
public String name;
|
||||
public String position;
|
||||
public String mobile;
|
||||
public String gender;
|
||||
public String email;
|
||||
public String avatar;
|
||||
public String thumb_avatar;
|
||||
public String telephone;
|
||||
public String address;
|
||||
public String alias;
|
||||
public String qr_code;
|
||||
public String open_userid;
|
||||
|
||||
@Resource
|
||||
private CacheClient cacheClient;
|
||||
|
||||
|
||||
public WxUtil(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
|
||||
// 实例化客户端
|
||||
public WxUtil client(Integer tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
this.config();
|
||||
return this;
|
||||
}
|
||||
|
||||
// 开发者ID和秘钥
|
||||
private void config() {
|
||||
JSONObject settingInfo = cacheClient.getSettingInfo("wx-work", this.tenantId);
|
||||
if(settingInfo == null){
|
||||
throw new BusinessException("微信小程序未配置");
|
||||
}
|
||||
this.appId = settingInfo.getString("corpId");
|
||||
this.appSecret = settingInfo.getString("secret");
|
||||
System.out.println("this.appId = " + this.appId);
|
||||
System.out.println("this.appSecret = " + this.appSecret);
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
public void getAccessToken(String code) {
|
||||
String key = "cache"+ this.tenantId +":ww:access_token";
|
||||
final String access_token = stringRedisTemplate.opsForValue().get(key);
|
||||
if(access_token != null){
|
||||
this.getUserInfo(code,access_token);
|
||||
}else {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +this.appId+ "&corpsecret="+ this.appSecret;
|
||||
System.out.println("url = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response = " + response);
|
||||
final JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
// 获取成功
|
||||
if(jsonObject.getString("access_token") != null){
|
||||
this.access_token = jsonObject.getString("access_token");
|
||||
this.expires_in = jsonObject.getString("expires_in");
|
||||
stringRedisTemplate.opsForValue().set(key,this.access_token,7000, TimeUnit.SECONDS);
|
||||
System.out.println("获取access_token成功 = " + this.access_token);
|
||||
this.getUserInfo(code,this.access_token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取userinfo
|
||||
public void getUserInfo(String code, String access_token) {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=" +access_token+ "&code=" + code;
|
||||
System.out.println("url2 = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response = " + response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
final String errcode = jsonObject.getString("errcode");
|
||||
final String errmsg = jsonObject.getString("errmsg");
|
||||
if(!StrUtil.equals(errcode,"0")){
|
||||
throw new BusinessException(errmsg);
|
||||
}
|
||||
this.userid = jsonObject.getString("userid");
|
||||
this.user_ticket = jsonObject.getString("user_ticket");
|
||||
this.openid = jsonObject.getString("openid");
|
||||
this.external_userid = jsonObject.getString("external_userid");
|
||||
System.out.println("获取用户信息成功 = " + jsonObject);
|
||||
}
|
||||
|
||||
public void getUserProfile(String userid, String access_token) {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+ access_token +"&userid=" + userid;
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response3 = " + response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
System.out.println("读取用户详细信息 = " + jsonObject);
|
||||
|
||||
this.name = jsonObject.getString("name");
|
||||
this.position = jsonObject.getString("position");
|
||||
this.gender = jsonObject.getString("gender");
|
||||
this.email = jsonObject.getString("email");
|
||||
this.avatar = jsonObject.getString("avatar");
|
||||
this.thumb_avatar = jsonObject.getString("thumb_avatar");
|
||||
this.telephone = jsonObject.getString("telephone");
|
||||
this.address = jsonObject.getString("address");
|
||||
this.alias = jsonObject.getString("alias");
|
||||
this.qr_code = jsonObject.getString("qr_code");
|
||||
this.open_userid = jsonObject.getString("open_userid");
|
||||
}
|
||||
}
|
||||
134
src/main/java/com/gxwebsoft/common/core/utils/WxWorkUtil.java
Normal file
134
src/main/java/com/gxwebsoft/common/core/utils/WxWorkUtil.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package com.gxwebsoft.common.core.utils;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 企业微信工具类
|
||||
* @author 科技小王子
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class WxWorkUtil {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
private Integer tenantId;
|
||||
public String appId;
|
||||
public String appSecret;
|
||||
public String access_token;
|
||||
public String expires_in;
|
||||
public String nickname;
|
||||
public String userid;
|
||||
public String user_ticket;
|
||||
public String openid;
|
||||
public String external_userid;
|
||||
public String name;
|
||||
public String position;
|
||||
public String mobile;
|
||||
public String gender;
|
||||
public String email;
|
||||
public String avatar;
|
||||
public String thumb_avatar;
|
||||
public String telephone;
|
||||
public String address;
|
||||
public String alias;
|
||||
public String qr_code;
|
||||
public String open_userid;
|
||||
|
||||
@Resource
|
||||
private CacheClient cacheClient;
|
||||
|
||||
|
||||
public WxWorkUtil(StringRedisTemplate stringRedisTemplate){
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
|
||||
// 实例化客户端
|
||||
public WxWorkUtil client(Integer tenantId) {
|
||||
this.tenantId = tenantId;
|
||||
this.config();
|
||||
return this;
|
||||
}
|
||||
|
||||
// 开发者ID和秘钥
|
||||
private void config() {
|
||||
JSONObject settingInfo = cacheClient.getSettingInfo("wx-work", this.tenantId);
|
||||
if(settingInfo == null){
|
||||
throw new BusinessException("企业微信未配置");
|
||||
}
|
||||
this.appId = settingInfo.getString("corpId");
|
||||
this.appSecret = settingInfo.getString("secret");
|
||||
System.out.println("this.appId = " + this.appId);
|
||||
System.out.println("this.appSecret = " + this.appSecret);
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
public void getAccessToken(String code) {
|
||||
String key = "cache"+ this.tenantId +":ww:access_token";
|
||||
final String access_token = stringRedisTemplate.opsForValue().get(key);
|
||||
if(access_token != null){
|
||||
this.getUserInfo(code,access_token);
|
||||
}else {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +this.appId+ "&corpsecret="+ this.appSecret;
|
||||
System.out.println("url = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response = " + response);
|
||||
final JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
// 获取成功
|
||||
if(jsonObject.getString("access_token") != null){
|
||||
this.access_token = jsonObject.getString("access_token");
|
||||
this.expires_in = jsonObject.getString("expires_in");
|
||||
stringRedisTemplate.opsForValue().set(key,this.access_token,7000, TimeUnit.SECONDS);
|
||||
System.out.println("获取access_token成功 = " + this.access_token);
|
||||
this.getUserInfo(code,this.access_token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取userinfo
|
||||
public void getUserInfo(String code, String access_token) {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=" +access_token+ "&code=" + code;
|
||||
System.out.println("url2 = " + url);
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response = " + response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
final String errcode = jsonObject.getString("errcode");
|
||||
final String errmsg = jsonObject.getString("errmsg");
|
||||
if(!StrUtil.equals(errcode,"0")){
|
||||
throw new BusinessException(errmsg);
|
||||
}
|
||||
this.userid = jsonObject.getString("userid");
|
||||
this.user_ticket = jsonObject.getString("user_ticket");
|
||||
this.openid = jsonObject.getString("openid");
|
||||
this.external_userid = jsonObject.getString("external_userid");
|
||||
System.out.println("获取用户信息成功 = " + jsonObject);
|
||||
}
|
||||
|
||||
public void getUserProfile(String userid, String access_token) {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token="+ access_token +"&userid=" + userid;
|
||||
String response = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
System.out.println("response3 = " + response);
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
System.out.println("读取用户详细信息 = " + jsonObject);
|
||||
|
||||
this.name = jsonObject.getString("name");
|
||||
this.position = jsonObject.getString("position");
|
||||
this.gender = jsonObject.getString("gender");
|
||||
this.email = jsonObject.getString("email");
|
||||
this.avatar = jsonObject.getString("avatar");
|
||||
this.thumb_avatar = jsonObject.getString("thumb_avatar");
|
||||
this.telephone = jsonObject.getString("telephone");
|
||||
this.address = jsonObject.getString("address");
|
||||
this.alias = jsonObject.getString("alias");
|
||||
this.qr_code = jsonObject.getString("qr_code");
|
||||
this.open_userid = jsonObject.getString("open_userid");
|
||||
}
|
||||
}
|
||||
87
src/main/java/com/gxwebsoft/common/core/web/ApiResult.java
Normal file
87
src/main/java/com/gxwebsoft/common/core/web/ApiResult.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 返回结果
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:50
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class ApiResult<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "状态码")
|
||||
private Integer code;
|
||||
|
||||
@Schema(description = "状态信息")
|
||||
private String message;
|
||||
|
||||
@Schema(description = "返回数据")
|
||||
private T data;
|
||||
|
||||
@Schema(description = "错误信息")
|
||||
private String error;
|
||||
|
||||
public ApiResult() {}
|
||||
|
||||
public ApiResult(Integer code) {
|
||||
this(code, null);
|
||||
}
|
||||
|
||||
public ApiResult(Integer code, String message) {
|
||||
this(code, message, null);
|
||||
}
|
||||
|
||||
public ApiResult(Integer code, String message, T data) {
|
||||
this(code, message, data, null);
|
||||
}
|
||||
|
||||
public ApiResult(Integer code, String message, T data, String error) {
|
||||
setCode(code);
|
||||
setMessage(message);
|
||||
setData(data);
|
||||
setError(error);
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public ApiResult<T> setCode(Integer code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public ApiResult<T> setMessage(String message) {
|
||||
this.message = message;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
public ApiResult<T> setData(T data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return this.error;
|
||||
}
|
||||
|
||||
public ApiResult<T> setError(String error) {
|
||||
this.error = error;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
262
src/main/java/com/gxwebsoft/common/core/web/BaseController.java
Normal file
262
src/main/java/com/gxwebsoft/common/core/web/BaseController.java
Normal file
@@ -0,0 +1,262 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.exception.BusinessException;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.system.entity.Company;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.CompanyService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.InitBinder;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Controller基类
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:19
|
||||
*/
|
||||
public class BaseController {
|
||||
@Resource
|
||||
private HttpServletRequest request;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private CompanyService companyService;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
/**
|
||||
* 获取当前登录的user
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public User getLoginUser() {
|
||||
try {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null) {
|
||||
Object object = authentication.getPrincipal();
|
||||
if (object instanceof User) {
|
||||
return (User) object;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的userId
|
||||
*
|
||||
* @return userId
|
||||
*/
|
||||
public Integer getLoginUserId() {
|
||||
User loginUser = getLoginUser();
|
||||
return loginUser == null ? null : loginUser.getUserId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的tenantId
|
||||
*
|
||||
* @return tenantId
|
||||
*/
|
||||
public Integer getTenantId() {
|
||||
// 1 从登录用户拿tenantId
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
return loginUser.getTenantId();
|
||||
}
|
||||
// 2 从请求头拿ID
|
||||
String tenantId = request.getHeader("tenantId");
|
||||
if(StrUtil.isNotBlank(tenantId)){
|
||||
return Integer.valueOf(tenantId);
|
||||
}
|
||||
// 3 从域名拿ID
|
||||
String Domain = request.getHeader("Domain");
|
||||
if (StrUtil.isNotBlank(Domain)) {
|
||||
String key = "Domain:" + Domain;
|
||||
tenantId = redisUtil.get(key);
|
||||
if(tenantId != null){
|
||||
System.out.println("从域名拿ID = " + tenantId);
|
||||
return Integer.valueOf(tenantId);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登录的企业信息
|
||||
*
|
||||
* @return Company
|
||||
*/
|
||||
public Company getCompany() {
|
||||
List<Company> list = companyService.list(new LambdaQueryWrapper<Company>().eq(Company::getAuthoritative, 1));
|
||||
if (!CollectionUtils.isEmpty(list)) {
|
||||
final Company company = list.get(0);
|
||||
return company;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getCompanyId() {
|
||||
Company company = getCompany();
|
||||
return company.getCompanyId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回成功
|
||||
*
|
||||
* @return ApiResult
|
||||
*/
|
||||
public ApiResult<?> success() {
|
||||
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回成功
|
||||
*
|
||||
* @param message 状态信息
|
||||
* @return ApiResult
|
||||
*/
|
||||
public ApiResult<?> success(String message) {
|
||||
return success().setMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回成功
|
||||
*
|
||||
* @param data 返回数据
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<T> success(T data) {
|
||||
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回成功
|
||||
*
|
||||
* @param message 状态信息
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<T> success(String message, T data) {
|
||||
return success(data).setMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回分页查询数据
|
||||
*
|
||||
* @param list 当前页数据
|
||||
* @param count 总数量
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) {
|
||||
return success(new PageResult<>(list, count));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回分页查询数据
|
||||
*
|
||||
* @param iPage IPage
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<PageResult<T>> success(IPage<T> iPage) {
|
||||
return success(iPage.getRecords(), iPage.getTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败
|
||||
*
|
||||
* @return ApiResult
|
||||
*/
|
||||
public ApiResult<?> fail() {
|
||||
return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败
|
||||
*
|
||||
* @param message 状态信息
|
||||
* @return ApiResult
|
||||
*/
|
||||
public ApiResult<?> fail(String message) {
|
||||
return fail().setMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败
|
||||
*
|
||||
* @param data 返回数据
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<T> fail(T data) {
|
||||
return fail(Constants.RESULT_ERROR_MSG, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回失败
|
||||
*
|
||||
* @param message 状态信息
|
||||
* @param data 返回数据
|
||||
* @return ApiResult
|
||||
*/
|
||||
public <T> ApiResult<T> fail(String message, T data) {
|
||||
return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求参数的空字符串转为null
|
||||
*/
|
||||
@InitBinder
|
||||
public void initBinder(WebDataBinder binder) {
|
||||
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
|
||||
}
|
||||
|
||||
// 自定义函数
|
||||
public String getAuthorization() {
|
||||
return request.getHeader("Authorization");
|
||||
}
|
||||
|
||||
public String getSign() {
|
||||
return request.getParameter("sign");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 根据账号|手机号码|邮箱查找用户ID
|
||||
*
|
||||
* @return userId
|
||||
*/
|
||||
public Integer getUserIdByUsername(String username, Integer tenantId) {
|
||||
// 按账号搜素
|
||||
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).eq(User::getTenantId, tenantId));
|
||||
if (user != null && user.getUserId() > 0) {
|
||||
return user.getUserId();
|
||||
}
|
||||
// 按手机号码搜索
|
||||
User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId));
|
||||
if (userByPhone != null && userByPhone.getUserId() > 0) {
|
||||
return userByPhone.getUserId();
|
||||
}
|
||||
// 按邮箱搜索
|
||||
User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, username).eq(User::getTenantId, tenantId));
|
||||
if (userByEmail != null && userByEmail.getUserId() > 0) {
|
||||
return userByEmail.getUserId();
|
||||
}
|
||||
throw new BusinessException("找不到该用户");
|
||||
}
|
||||
|
||||
}
|
||||
98
src/main/java/com/gxwebsoft/common/core/web/BaseParam.java
Normal file
98
src/main/java/com/gxwebsoft/common/core/web/BaseParam.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.gxwebsoft.common.core.annotation.QueryField;
|
||||
import com.gxwebsoft.common.core.annotation.QueryType;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 查询参数基本字段
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-08-26 22:14:43
|
||||
*/
|
||||
@Data
|
||||
public class BaseParam implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "分页查询页码")
|
||||
private Long page;
|
||||
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "分页查询每页数量")
|
||||
private Long limit;
|
||||
|
||||
@Schema(description = "国际化语言")
|
||||
@TableField(exist = false)
|
||||
private String lang;
|
||||
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "排序字段或sql, 如果是sql则order字段无用, 如: `id asc, name desc`")
|
||||
private String sort;
|
||||
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "sort是字段名称时对应的排序方式, asc升序, desc降序")
|
||||
private String order;
|
||||
|
||||
@QueryField(value = "create_time", type = QueryType.GE)
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "创建时间起始值")
|
||||
private String createTimeStart;
|
||||
|
||||
@QueryField(value = "create_time", type = QueryType.LE)
|
||||
@TableField(exist = false)
|
||||
@Schema(description = "创建时间结束值")
|
||||
private String createTimeEnd;
|
||||
|
||||
@QueryField(value = "create_time", type = QueryType.GE)
|
||||
@Schema(description = "搜索场景")
|
||||
@TableField(exist = false)
|
||||
private String sceneType;
|
||||
|
||||
@Schema(description = "商户ID")
|
||||
@TableField(exist = false)
|
||||
private Long merchantId;
|
||||
|
||||
@Schema(description = "租户ID")
|
||||
@TableField(exist = false)
|
||||
private Integer tenantId;
|
||||
|
||||
@Schema(description = "模糊搜素")
|
||||
@TableField(exist = false)
|
||||
private String keywords;
|
||||
|
||||
@Schema(description = "token")
|
||||
@TableField(exist = false)
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 获取集合中的第一条数据
|
||||
*
|
||||
* @param records 集合
|
||||
* @return 第一条数据
|
||||
*/
|
||||
public <T> T getOne(List<T> records) {
|
||||
return CommonUtil.listGetOne(records);
|
||||
}
|
||||
|
||||
/**
|
||||
* 国际化参数
|
||||
*/
|
||||
public String getLang(){
|
||||
if(StrUtil.isBlank(this.lang)){
|
||||
return null;
|
||||
}
|
||||
if(this.lang.equals("zh")){
|
||||
return "zh_CN";
|
||||
}
|
||||
return this.lang;
|
||||
}
|
||||
|
||||
}
|
||||
57
src/main/java/com/gxwebsoft/common/core/web/BatchParam.java
Normal file
57
src/main/java/com/gxwebsoft/common/core/web/BatchParam.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 批量修改通用参数
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2020-03-13 00:11:06
|
||||
*/
|
||||
@Data
|
||||
public class BatchParam<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "需要修改的数据id集合")
|
||||
private List<Serializable> ids;
|
||||
|
||||
@Schema(description = "需要修改的字段和值")
|
||||
private T data;
|
||||
|
||||
/**
|
||||
* 通用批量修改方法
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField id字段名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean update(IService<T> service, String idField) {
|
||||
if (this.data == null) {
|
||||
return false;
|
||||
}
|
||||
return service.update(this.data, new UpdateWrapper<T>().in(idField, this.ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用批量修改方法
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField id字段名称
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean update(IService<T> service, SFunction<T, ?> idField) {
|
||||
if (this.data == null) {
|
||||
return false;
|
||||
}
|
||||
return service.update(this.data, new LambdaUpdateWrapper<T>().in(idField, this.ids));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 检查是否存在通用参数
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2021-09-07 22:24:39
|
||||
*/
|
||||
@Data
|
||||
public class ExistenceParam<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "检查的字段")
|
||||
private String field;
|
||||
|
||||
@Schema(description = "字段的值")
|
||||
private String value;
|
||||
|
||||
@Schema(description = "修改时的主键")
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 检查是否存在
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField 修改时的主键字段
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExistence(IService<T> service, String idField) {
|
||||
return isExistence(service, idField, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField 修改时的主键字段
|
||||
* @param isToUnderlineCase 是否需要把field转为下划线格式
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExistence(IService<T> service, String idField, boolean isToUnderlineCase) {
|
||||
if (StrUtil.hasBlank(this.field, this.value)) {
|
||||
return false;
|
||||
}
|
||||
String fieldName = isToUnderlineCase ? StrUtil.toUnderlineCase(field) : field;
|
||||
QueryWrapper<T> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq(fieldName, value);
|
||||
if (id != null) {
|
||||
wrapper.ne(idField, id);
|
||||
}
|
||||
return service.count(wrapper) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField 修改时的主键字段
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExistence(IService<T> service, SFunction<T, ?> idField) {
|
||||
return isExistence(service, idField, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在
|
||||
*
|
||||
* @param service IService
|
||||
* @param idField 修改时的主键字段
|
||||
* @param isToUnderlineCase 是否需要把field转为下划线格式
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExistence(IService<T> service, SFunction<T, ?> idField, boolean isToUnderlineCase) {
|
||||
if (StrUtil.hasBlank(this.field, this.value)) {
|
||||
return false;
|
||||
}
|
||||
String fieldName = isToUnderlineCase ? StrUtil.toUnderlineCase(field) : field;
|
||||
LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.apply(fieldName + " = {0}", value);
|
||||
if (id != null) {
|
||||
wrapper.ne(idField, id);
|
||||
}
|
||||
return service.count(wrapper) > 0;
|
||||
}
|
||||
|
||||
}
|
||||
343
src/main/java/com/gxwebsoft/common/core/web/PageParam.java
Normal file
343
src/main/java/com/gxwebsoft/common/core/web/PageParam.java
Normal file
@@ -0,0 +1,343 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.annotation.QueryField;
|
||||
import com.gxwebsoft.common.core.annotation.QueryType;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 分页、排序、搜索参数封装
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2019-04-26 10:34:35
|
||||
*/
|
||||
public class PageParam<T, U extends BaseParam> extends Page<T> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 租户id字段名称
|
||||
*/
|
||||
private static final String TENANT_ID_FIELD = "tenantId";
|
||||
|
||||
/**
|
||||
* 查询条件
|
||||
*/
|
||||
private final U where;
|
||||
|
||||
/**
|
||||
* 是否把字段名称驼峰转下划线
|
||||
*/
|
||||
private final boolean isToUnderlineCase;
|
||||
|
||||
public PageParam() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public PageParam(U where) {
|
||||
this(where, true);
|
||||
}
|
||||
|
||||
public PageParam(U where, boolean isToUnderlineCase) {
|
||||
super();
|
||||
this.where = where;
|
||||
this.isToUnderlineCase = isToUnderlineCase;
|
||||
if (where != null) {
|
||||
// 获取分页页码
|
||||
if (where.getPage() != null) {
|
||||
setCurrent(where.getPage());
|
||||
}
|
||||
// 获取分页每页数量
|
||||
if (where.getLimit() != null) {
|
||||
setSize(where.getLimit());
|
||||
}
|
||||
// 获取排序方式
|
||||
if (where.getSort() != null) {
|
||||
if (sortIsSQL(where.getSort())) {
|
||||
setOrders(parseOrderSQL(where.getSort()));
|
||||
} else {
|
||||
List<OrderItem> orderItems = new ArrayList<>();
|
||||
String column = this.isToUnderlineCase ? StrUtil.toUnderlineCase(where.getSort()) : where.getSort();
|
||||
boolean asc = !Constants.ORDER_DESC_VALUE.equals(where.getOrder());
|
||||
orderItems.add(new OrderItem(column, asc));
|
||||
setOrders(orderItems);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 排序字段是否是sql
|
||||
*/
|
||||
private boolean sortIsSQL(String sort) {
|
||||
return sort != null && (sort.contains(",") || sort.trim().contains(" "));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析排序sql
|
||||
*/
|
||||
private List<OrderItem> parseOrderSQL(String orderSQL) {
|
||||
List<OrderItem> orders = new ArrayList<>();
|
||||
if (StrUtil.isNotBlank(orderSQL)) {
|
||||
for (String item : orderSQL.split(",")) {
|
||||
String[] temp = item.trim().split(" ");
|
||||
if (!temp[0].isEmpty()) {
|
||||
String column = this.isToUnderlineCase ? StrUtil.toUnderlineCase(temp[0]) : temp[0];
|
||||
boolean asc = temp.length == 1 || !temp[temp.length - 1].equals(Constants.ORDER_DESC_VALUE);
|
||||
orders.add(new OrderItem(column, asc));
|
||||
}
|
||||
}
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认排序方式
|
||||
*
|
||||
* @param orderItems 排序方式
|
||||
* @return PageParam
|
||||
*/
|
||||
public PageParam<T, U> setDefaultOrder(List<OrderItem> orderItems) {
|
||||
if (orders() == null || orders().size() == 0) {
|
||||
setOrders(orderItems);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认排序方式
|
||||
*
|
||||
* @param orderSQL 排序方式
|
||||
* @return PageParam
|
||||
*/
|
||||
public PageParam<T, U> setDefaultOrder(String orderSQL) {
|
||||
setDefaultOrder(parseOrderSQL(orderSQL));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*
|
||||
* @param excludes 不包含的字段
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
public QueryWrapper<T> getWrapper(String... excludes) {
|
||||
return buildWrapper(null, Arrays.asList(excludes));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*
|
||||
* @param columns 只包含的字段
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
public QueryWrapper<T> getWrapperWith(String... columns) {
|
||||
return buildWrapper(Arrays.asList(columns), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建QueryWrapper
|
||||
*
|
||||
* @param columns 包含的字段
|
||||
* @param excludes 排除的字段
|
||||
* @return QueryWrapper
|
||||
*/
|
||||
private QueryWrapper<T> buildWrapper(List<String> columns, List<String> excludes) {
|
||||
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
||||
Map<String, Object> map = BeanUtil.beanToMap(where, false, true);
|
||||
for (String fieldName : map.keySet()) {
|
||||
Object fieldValue = map.get(fieldName);
|
||||
Field field = ReflectUtil.getField(where.getClass(), fieldName);
|
||||
|
||||
// 过滤不包含的字段
|
||||
if (columns != null && !columns.contains(fieldName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 过滤排除的字段
|
||||
if (excludes != null && excludes.contains(fieldName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 过滤逻辑删除字段
|
||||
if (field.getAnnotation(TableLogic.class) != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 过滤租户id字段
|
||||
if (fieldName.equals(TENANT_ID_FIELD)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取注解指定的查询字段及查询方式
|
||||
QueryType queryType = QueryType.LIKE;
|
||||
QueryField queryField = field.getAnnotation(QueryField.class);
|
||||
if (queryField != null) {
|
||||
if (StrUtil.isNotEmpty(queryField.value())) {
|
||||
fieldName = queryField.value();
|
||||
}
|
||||
if (queryField.type() != null) {
|
||||
queryType = queryField.type();
|
||||
}
|
||||
} else {
|
||||
// 过滤非本表的字段
|
||||
TableField tableField = field.getAnnotation(TableField.class);
|
||||
if (tableField != null && !tableField.exist()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 字段名驼峰转下划线
|
||||
if (this.isToUnderlineCase) {
|
||||
fieldName = StrUtil.toUnderlineCase(fieldName);
|
||||
}
|
||||
|
||||
//
|
||||
switch (queryType) {
|
||||
case EQ:
|
||||
queryWrapper.eq(fieldName, fieldValue);
|
||||
break;
|
||||
case NE:
|
||||
queryWrapper.ne(fieldName, fieldValue);
|
||||
break;
|
||||
case GT:
|
||||
queryWrapper.gt(fieldName, fieldValue);
|
||||
break;
|
||||
case GE:
|
||||
queryWrapper.ge(fieldName, fieldValue);
|
||||
break;
|
||||
case LT:
|
||||
queryWrapper.lt(fieldName, fieldValue);
|
||||
break;
|
||||
case LE:
|
||||
queryWrapper.le(fieldName, fieldValue);
|
||||
break;
|
||||
case LIKE:
|
||||
queryWrapper.like(fieldName, fieldValue);
|
||||
break;
|
||||
case NOT_LIKE:
|
||||
queryWrapper.notLike(fieldName, fieldValue);
|
||||
break;
|
||||
case LIKE_LEFT:
|
||||
queryWrapper.likeLeft(fieldName, fieldValue);
|
||||
break;
|
||||
case LIKE_RIGHT:
|
||||
queryWrapper.likeRight(fieldName, fieldValue);
|
||||
break;
|
||||
case IS_NULL:
|
||||
queryWrapper.isNull(fieldName);
|
||||
break;
|
||||
case IS_NOT_NULL:
|
||||
queryWrapper.isNotNull(fieldName);
|
||||
break;
|
||||
case IN:
|
||||
queryWrapper.in(fieldName, fieldValue);
|
||||
break;
|
||||
case NOT_IN:
|
||||
queryWrapper.notIn(fieldName, fieldValue);
|
||||
break;
|
||||
case IN_STR:
|
||||
if (fieldValue instanceof String) {
|
||||
queryWrapper.in(fieldName, Arrays.asList(((String) fieldValue).split(",")));
|
||||
}
|
||||
break;
|
||||
case NOT_IN_STR:
|
||||
if (fieldValue instanceof String) {
|
||||
queryWrapper.notIn(fieldName, Arrays.asList(((String) fieldValue).split(",")));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取包含排序的查询条件
|
||||
*
|
||||
* @return 包含排序的QueryWrapper
|
||||
*/
|
||||
public QueryWrapper<T> getOrderWrapper() {
|
||||
return getOrderWrapper(getWrapper());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取包含排序的查询条件
|
||||
*
|
||||
* @param queryWrapper 不含排序的QueryWrapper
|
||||
* @return 包含排序的QueryWrapper
|
||||
*/
|
||||
public QueryWrapper<T> getOrderWrapper(QueryWrapper<T> queryWrapper) {
|
||||
if (queryWrapper == null) {
|
||||
queryWrapper = new QueryWrapper<>();
|
||||
}
|
||||
for (OrderItem orderItem : orders()) {
|
||||
if (orderItem.isAsc()) {
|
||||
queryWrapper.orderByAsc(orderItem.getColumn());
|
||||
} else {
|
||||
queryWrapper.orderByDesc(orderItem.getColumn());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取集合中的第一条数据
|
||||
*
|
||||
* @param records 集合
|
||||
* @return 第一条数据
|
||||
*/
|
||||
public T getOne(List<T> records) {
|
||||
return CommonUtil.listGetOne(records);
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码排序集合
|
||||
*
|
||||
* @param records 集合
|
||||
* @return 排序后的集合
|
||||
*/
|
||||
public List<T> sortRecords(List<T> records) {
|
||||
List<OrderItem> orderItems = orders();
|
||||
if (records == null || records.size() < 2 || orderItems == null || orderItems.size() == 0) {
|
||||
return records;
|
||||
}
|
||||
Comparator<T> comparator = null;
|
||||
for (OrderItem item : orderItems) {
|
||||
if (item.getColumn() == null) {
|
||||
continue;
|
||||
}
|
||||
String field = this.isToUnderlineCase ? StrUtil.toCamelCase(item.getColumn()) : item.getColumn();
|
||||
Function keyExtractor = t -> ReflectUtil.getFieldValue(t, field);
|
||||
if (comparator == null) {
|
||||
if (item.isAsc()) {
|
||||
comparator = Comparator.comparing(keyExtractor);
|
||||
} else {
|
||||
comparator = Comparator.comparing(keyExtractor, Comparator.reverseOrder());
|
||||
}
|
||||
} else {
|
||||
if (item.isAsc()) {
|
||||
comparator.thenComparing(keyExtractor);
|
||||
} else {
|
||||
comparator.thenComparing(keyExtractor, Comparator.reverseOrder());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (comparator != null) {
|
||||
return records.stream().sorted(comparator).collect(Collectors.toList());
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
}
|
||||
51
src/main/java/com/gxwebsoft/common/core/web/PageResult.java
Normal file
51
src/main/java/com/gxwebsoft/common/core/web/PageResult.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.gxwebsoft.common.core.web;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页查询返回结果
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2017-06-10 10:10:02
|
||||
*/
|
||||
public class PageResult<T> implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "当前页数据")
|
||||
private List<T> list;
|
||||
|
||||
@Schema(description = "总数量")
|
||||
private Long count;
|
||||
|
||||
public PageResult() {
|
||||
}
|
||||
|
||||
public PageResult(List<T> list) {
|
||||
this(list, null);
|
||||
}
|
||||
|
||||
public PageResult(List<T> list, Long count) {
|
||||
setList(list);
|
||||
setCount(count);
|
||||
}
|
||||
|
||||
public List<T> getList() {
|
||||
return this.list;
|
||||
}
|
||||
|
||||
public void setList(List<T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Long getCount() {
|
||||
return this.count;
|
||||
}
|
||||
|
||||
public void setCount(Long count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
}
|
||||
145
src/main/java/com/gxwebsoft/common/mq/config/RabbitMQConfig.java
Normal file
145
src/main/java/com/gxwebsoft/common/mq/config/RabbitMQConfig.java
Normal file
@@ -0,0 +1,145 @@
|
||||
package com.gxwebsoft.common.mq.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.amqp.core.DirectExchange;
|
||||
import org.springframework.amqp.core.TopicExchange;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.core.QueueBuilder;
|
||||
import org.springframework.amqp.core.Binding;
|
||||
import org.springframework.amqp.core.BindingBuilder;
|
||||
import org.springframework.amqp.core.AcknowledgeMode;
|
||||
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* RabbitMQ 配置类
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "sync.mq.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class RabbitMQConfig {
|
||||
|
||||
// ==================== 常量定义 ====================
|
||||
public static final String SYNC_EXCHANGE = "sync.topic.exchange";
|
||||
public static final String SYNC_QUEUE = "sync.queue";
|
||||
public static final String SYNC_ROUTING_KEY = "sync.message";
|
||||
|
||||
// 死信队列
|
||||
public static final String DLX_EXCHANGE = "sync.dlx.exchange";
|
||||
public static final String DLQ_QUEUE = "sync.dlq";
|
||||
public static final String DLQ_ROUTING_KEY = "sync.dlq";
|
||||
|
||||
@Value("${spring.rabbitmq.host:localhost}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.rabbitmq.port:5672}")
|
||||
private int port;
|
||||
|
||||
@Value("${spring.rabbitmq.username:guest}")
|
||||
private String username;
|
||||
|
||||
@Value("${spring.rabbitmq.password:guest}")
|
||||
private String password;
|
||||
|
||||
@Value("${spring.rabbitmq.virtual-host:/}")
|
||||
private String virtualHost;
|
||||
|
||||
// ==================== Connection Factory ====================
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
|
||||
connectionFactory.setHost(host);
|
||||
connectionFactory.setPort(port);
|
||||
connectionFactory.setUsername(username);
|
||||
connectionFactory.setPassword(password);
|
||||
connectionFactory.setVirtualHost(virtualHost);
|
||||
// 开启publisher-confirm确认模式
|
||||
connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
|
||||
// 开启publisher-return确认模式
|
||||
connectionFactory.setPublisherReturns(true);
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
// ==================== Message Converter ====================
|
||||
|
||||
@Bean
|
||||
public MessageConverter messageConverter(ObjectMapper objectMapper) {
|
||||
return new Jackson2JsonMessageConverter(objectMapper);
|
||||
}
|
||||
|
||||
// ==================== RabbitTemplate ====================
|
||||
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
|
||||
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
|
||||
rabbitTemplate.setMessageConverter(messageConverter);
|
||||
// 设置Mandatory为true,才能触发ReturnCallback
|
||||
rabbitTemplate.setMandatory(true);
|
||||
return rabbitTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
|
||||
ConnectionFactory connectionFactory, MessageConverter messageConverter) {
|
||||
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory);
|
||||
factory.setMessageConverter(messageConverter);
|
||||
// 设置并发数
|
||||
factory.setConcurrentConsumers(1);
|
||||
factory.setMaxConcurrentConsumers(5);
|
||||
// 设置手动ack
|
||||
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
|
||||
// 预取数量
|
||||
factory.setPrefetchCount(10);
|
||||
return factory;
|
||||
}
|
||||
|
||||
// ==================== 交换机 ====================
|
||||
|
||||
/**
|
||||
* 用户同步 Topic Exchange
|
||||
* 使用 Topic 类型,支持按 targetSystem 路由到不同队列
|
||||
* routing key 格式: user.sync.{targetSystem}
|
||||
* 各子系统可以绑定自己的队列来消费消息
|
||||
*/
|
||||
@Bean
|
||||
public TopicExchange syncExchange() {
|
||||
return new TopicExchange(SYNC_EXCHANGE, true, false);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DirectExchange deadLetterExchange() {
|
||||
return new DirectExchange(DLX_EXCHANGE, true, false);
|
||||
}
|
||||
|
||||
// ==================== 队列 ====================
|
||||
|
||||
/**
|
||||
* 注意:core 系统只负责发送消息,不消费消息
|
||||
* 各子系统(websopy等)需要在自己的系统中配置消费者和队列
|
||||
*
|
||||
* 如果 core 系统也需要消费某些消息,可以在这里添加对应的队列
|
||||
*/
|
||||
|
||||
@Bean
|
||||
public Queue deadLetterQueue() {
|
||||
return QueueBuilder.durable(DLQ_QUEUE).build();
|
||||
}
|
||||
|
||||
// ==================== 绑定 ====================
|
||||
|
||||
@Bean
|
||||
public Binding dlqBinding() {
|
||||
return BindingBuilder.bind(deadLetterQueue())
|
||||
.to(deadLetterExchange())
|
||||
.with(DLQ_ROUTING_KEY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.gxwebsoft.common.mq.message;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 统一消息实体 - 用于各模块间的数据同步
|
||||
*/
|
||||
@Data
|
||||
public class SyncMessage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 消息唯一ID
|
||||
*/
|
||||
private String messageId;
|
||||
|
||||
/**
|
||||
* 消息类型:USER_SYNC, TENANT_SYNC, etc.
|
||||
*/
|
||||
private String messageType;
|
||||
|
||||
/**
|
||||
* 事件类型:CREATE, UPDATE, DELETE
|
||||
*/
|
||||
private String eventType;
|
||||
|
||||
/**
|
||||
* 目标系统标识
|
||||
*/
|
||||
private String targetSystem;
|
||||
|
||||
/**
|
||||
* 业务数据(Map格式,便于序列化)
|
||||
*/
|
||||
private Map<String, Object> data;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 消息重试次数
|
||||
*/
|
||||
private Integer retryCount;
|
||||
|
||||
public SyncMessage() {
|
||||
this.createTime = LocalDateTime.now();
|
||||
this.retryCount = 0;
|
||||
}
|
||||
|
||||
public SyncMessage(String messageType, String eventType, String targetSystem, Map<String, Object> data) {
|
||||
this();
|
||||
this.messageId = java.util.UUID.randomUUID().toString().replace("-", "");
|
||||
this.messageType = messageType;
|
||||
this.eventType = eventType;
|
||||
this.targetSystem = targetSystem;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户同步消息
|
||||
*/
|
||||
public static SyncMessage userCreate(String targetSystem, Map<String, Object> userData) {
|
||||
return new SyncMessage("USER_SYNC", "CREATE", targetSystem, userData);
|
||||
}
|
||||
|
||||
public static SyncMessage userUpdate(String targetSystem, Map<String, Object> userData) {
|
||||
return new SyncMessage("USER_SYNC", "UPDATE", targetSystem, userData);
|
||||
}
|
||||
|
||||
public static SyncMessage userDelete(String targetSystem, Map<String, Object> userData) {
|
||||
return new SyncMessage("USER_SYNC", "DELETE", targetSystem, userData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.gxwebsoft.common.mq.producer;
|
||||
|
||||
import com.gxwebsoft.common.mq.message.SyncMessage;
|
||||
|
||||
/**
|
||||
* 消息生产者接口 - 预留抽象层,便于将来切换MQ实现(如从RabbitMQ迁移到RocketMQ)
|
||||
*/
|
||||
public interface SyncMessageProducer {
|
||||
|
||||
/**
|
||||
* 发送同步消息
|
||||
*
|
||||
* @param message 消息体
|
||||
*/
|
||||
void sendSyncMessage(SyncMessage message);
|
||||
|
||||
/**
|
||||
* 发送同步消息(带回调)
|
||||
*
|
||||
* @param message 消息体
|
||||
* @param callback 发送回调
|
||||
*/
|
||||
void sendSyncMessage(SyncMessage message, SendCallback callback);
|
||||
|
||||
/**
|
||||
* 发送用户同步消息
|
||||
*
|
||||
* @param targetSystem 目标系统
|
||||
* @param eventType 事件类型:CREATE, UPDATE, DELETE
|
||||
* @param userData 用户数据
|
||||
*/
|
||||
void sendUserSyncMessage(String targetSystem, String eventType, Object userData);
|
||||
|
||||
/**
|
||||
* 发送回调接口
|
||||
*/
|
||||
interface SendCallback {
|
||||
void onSuccess(String messageId);
|
||||
void onFailure(String messageId, Throwable throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.gxwebsoft.common.mq.producer.impl;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.gxwebsoft.common.mq.config.RabbitMQConfig;
|
||||
import com.gxwebsoft.common.mq.message.SyncMessage;
|
||||
import com.gxwebsoft.common.mq.producer.SyncMessageProducer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* RabbitMQ 消息生产者实现
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(name = "sync.mq.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class RabbitMQSyncProducer implements SyncMessageProducer, RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
|
||||
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
private final MessageConverter messageConverter;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public RabbitMQSyncProducer(RabbitTemplate rabbitTemplate, MessageConverter messageConverter, ObjectMapper objectMapper) {
|
||||
this.rabbitTemplate = rabbitTemplate;
|
||||
this.messageConverter = messageConverter;
|
||||
this.objectMapper = objectMapper;
|
||||
// 设置确认回调
|
||||
this.rabbitTemplate.setConfirmCallback(this);
|
||||
this.rabbitTemplate.setReturnCallback(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSyncMessage(SyncMessage message) {
|
||||
sendSyncMessage(message, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSyncMessage(SyncMessage message, SendCallback callback) {
|
||||
try {
|
||||
log.info("发送MQ消息: messageId={}, type={}, event={}, target={}",
|
||||
message.getMessageId(), message.getMessageType(),
|
||||
message.getEventType(), message.getTargetSystem());
|
||||
|
||||
CorrelationData correlationData = new CorrelationData(message.getMessageId());
|
||||
|
||||
if (callback != null) {
|
||||
correlationData.getFuture().addCallback(
|
||||
result -> {
|
||||
if (result.isAck()) {
|
||||
callback.onSuccess(message.getMessageId());
|
||||
} else {
|
||||
callback.onFailure(message.getMessageId(),
|
||||
new RuntimeException("消息发送未被确认"));
|
||||
}
|
||||
},
|
||||
ex -> callback.onFailure(message.getMessageId(), ex)
|
||||
);
|
||||
}
|
||||
|
||||
// 使用 targetSystem 作为 routing key
|
||||
// 格式: user.sync.{targetSystem}
|
||||
// 各子系统绑定队列时使用 pattern: user.sync.{systemName}
|
||||
String routingKey = buildRoutingKey(message.getTargetSystem());
|
||||
|
||||
rabbitTemplate.convertAndSend(
|
||||
RabbitMQConfig.SYNC_EXCHANGE,
|
||||
routingKey,
|
||||
message,
|
||||
correlationData
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("发送MQ消息失败: messageId={}, error={}", message.getMessageId(), e.getMessage(), e);
|
||||
if (callback != null) {
|
||||
callback.onFailure(message.getMessageId(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 routing key
|
||||
* 格式: user.sync.{targetSystem}
|
||||
*/
|
||||
private String buildRoutingKey(String targetSystem) {
|
||||
if (targetSystem == null || targetSystem.isEmpty()) {
|
||||
return "user.sync.all";
|
||||
}
|
||||
return "user.sync." + targetSystem.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendUserSyncMessage(String targetSystem, String eventType, Object userData) {
|
||||
try {
|
||||
Map<String, Object> dataMap;
|
||||
if (userData instanceof Map) {
|
||||
dataMap = (Map<String, Object>) userData;
|
||||
} else {
|
||||
// 转换为Map
|
||||
dataMap = objectMapper.convertValue(userData, Map.class);
|
||||
}
|
||||
|
||||
SyncMessage message = new SyncMessage("USER_SYNC", eventType, targetSystem, dataMap);
|
||||
sendSyncMessage(message);
|
||||
} catch (Exception e) {
|
||||
log.error("发送用户同步消息失败: targetSystem={}, eventType={}, error={}",
|
||||
targetSystem, eventType, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认回调 - Broker确认收到消息
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
String messageId = correlationData.getId();
|
||||
if (ack) {
|
||||
log.debug("消息确认成功: messageId={}", messageId);
|
||||
} else {
|
||||
log.warn("消息确认失败: messageId={}, cause={}", messageId, cause);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return回调 - 消息无法路由时回调
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(Message message, int replyCode, String replyText,
|
||||
String exchange, String routingKey) {
|
||||
log.error("消息无法路由: exchange={}, routingKey={}, replyCode={}, replyText={}",
|
||||
exchange, routingKey, replyCode, replyText);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package com.gxwebsoft.common.system.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.gxwebsoft.common.core.annotation.OperationLog;
|
||||
import com.gxwebsoft.common.core.utils.CacheClient;
|
||||
import com.gxwebsoft.common.core.utils.CommonUtil;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.core.web.*;
|
||||
import com.gxwebsoft.common.system.entity.AccessKey;
|
||||
import com.gxwebsoft.common.system.param.AccessKeyParam;
|
||||
import com.gxwebsoft.common.system.service.AccessKeyService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static com.gxwebsoft.common.core.constants.WebsiteConstants.CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS;
|
||||
|
||||
/**
|
||||
* 访问凭证管理控制器
|
||||
*
|
||||
* @author 科技小王子
|
||||
* @since 2023-05-16 19:19:55
|
||||
*/
|
||||
@Tag(name = "安全访问凭证")
|
||||
@RestController
|
||||
@RequestMapping("/api/system/access-key")
|
||||
public class AccessKeyController extends BaseController {
|
||||
@Resource
|
||||
private AccessKeyService accessKeyService;
|
||||
@Resource
|
||||
private CacheClient cacheClient;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:list')")
|
||||
@OperationLog
|
||||
@Operation(summary = "分页查询访问凭证")
|
||||
@GetMapping("/page")
|
||||
public ApiResult<PageResult<AccessKey>> page(AccessKeyParam param) {
|
||||
// 使用关联查询
|
||||
final PageResult<AccessKey> accessKeyPageResult = accessKeyService.pageRel(param);
|
||||
if (param.getCode() != null) {
|
||||
// 短信验证码校验
|
||||
final String code = param.getCode();
|
||||
// 验证码校验
|
||||
String key = "code:" + param.getPhone();
|
||||
if (!code.equals(redisUtil.get(key)) && !redisUtil.get(CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS).equals(code)) {
|
||||
String message = "验证码不正确";
|
||||
return fail(message, null);
|
||||
}
|
||||
return success(accessKeyPageResult);
|
||||
}
|
||||
// 默认不给查看AccessSecret
|
||||
accessKeyPageResult.getList().forEach(d -> {
|
||||
d.setAccessSecret(null);
|
||||
});
|
||||
return success(accessKeyPageResult);
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:list')")
|
||||
@OperationLog
|
||||
@Operation(summary = "查询全部访问凭证管理")
|
||||
@GetMapping()
|
||||
public ApiResult<List<AccessKey>> list(AccessKeyParam param) {
|
||||
PageParam<AccessKey, AccessKeyParam> page = new PageParam<>(param);
|
||||
page.setDefaultOrder("create_time desc");
|
||||
return success(accessKeyService.list(page.getOrderWrapper()));
|
||||
// 使用关联查询
|
||||
//return success(accessKeyService.listRel(param));
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:list')")
|
||||
@OperationLog
|
||||
@Operation(summary = "根据id查询访问凭证管理")
|
||||
@GetMapping("/{id}")
|
||||
public ApiResult<AccessKey> get(@PathVariable("id") Integer id) {
|
||||
return success(accessKeyService.getById(id));
|
||||
// 使用关联查询
|
||||
//return success(accessKeyService.getByIdRel(id));
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:list')")
|
||||
@OperationLog
|
||||
@Operation(summary = "添加访问凭证管理")
|
||||
@PostMapping()
|
||||
public ApiResult<?> save(@RequestBody AccessKey accessKey) {
|
||||
final int count = accessKeyService.count();
|
||||
if (count >= 5) {
|
||||
return fail("当前账号只能绑定 5 个 AccessKey");
|
||||
}
|
||||
if(accessKey.getAccessKey() == null){
|
||||
accessKey.setAccessKey(CommonUtil.randomUUID16());
|
||||
}
|
||||
accessKey.setAccessSecret("sk-" + CommonUtil.randomUUID16().concat(CommonUtil.randomUUID16()));
|
||||
if (accessKeyService.save(accessKey)) {
|
||||
return success("创建成功", accessKey);
|
||||
}
|
||||
return fail("创建失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:update')")
|
||||
@OperationLog
|
||||
@Operation(summary = "修改访问凭证管理")
|
||||
@PutMapping()
|
||||
public ApiResult<?> update(@RequestBody AccessKey accessKey) {
|
||||
if (accessKeyService.updateById(accessKey)) {
|
||||
return success("修改成功");
|
||||
}
|
||||
return fail("修改失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:remove')")
|
||||
@OperationLog
|
||||
@Operation(summary = "删除访问凭证管理")
|
||||
@DeleteMapping("/{id}")
|
||||
public ApiResult<?> remove(@PathVariable("id") Integer id) {
|
||||
if (accessKeyService.removeById(id)) {
|
||||
return success("删除成功");
|
||||
}
|
||||
return fail("删除失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:save')")
|
||||
@OperationLog
|
||||
@Operation(summary = "批量添加访问凭证管理")
|
||||
@PostMapping("/batch")
|
||||
public ApiResult<?> saveBatch(@RequestBody List<AccessKey> list) {
|
||||
if (accessKeyService.saveBatch(list)) {
|
||||
return success("添加成功");
|
||||
}
|
||||
return fail("添加失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:update')")
|
||||
@OperationLog
|
||||
@Operation(summary = "批量修改访问凭证管理")
|
||||
@PutMapping("/batch")
|
||||
public ApiResult<?> removeBatch(@RequestBody BatchParam<AccessKey> batchParam) {
|
||||
if (batchParam.update(accessKeyService, "id")) {
|
||||
return success("修改成功");
|
||||
}
|
||||
return fail("修改失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:remove')")
|
||||
@OperationLog
|
||||
@Operation(summary = "批量删除访问凭证管理")
|
||||
@DeleteMapping("/batch")
|
||||
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
|
||||
if (accessKeyService.removeByIds(ids)) {
|
||||
return success("删除成功");
|
||||
}
|
||||
return fail("删除失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('sys:accessKey:resetSMSCode')")
|
||||
@OperationLog
|
||||
@Operation(summary = "重置万能短信验证码")
|
||||
@PostMapping("/resetSMSCode")
|
||||
public ApiResult<?> resetSMSCode() {
|
||||
// 生成短信验证码
|
||||
Random randObj = new Random();
|
||||
String code = Integer.toString(100000 + randObj.nextInt(900000));
|
||||
redisUtil.set(CACHE_KEY_VERIFICATION_CODE_BY_DEV_SMS, code, 5L, TimeUnit.MINUTES);
|
||||
return success("新验证码:".concat(code));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
package com.gxwebsoft.common.system.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import com.aliyun.oss.common.auth.CredentialsProvider;
|
||||
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||
import com.aliyun.oss.common.utils.BinaryUtil;
|
||||
import com.aliyun.oss.model.PolicyConditions;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.auth.sts.AssumeRoleRequest;
|
||||
import com.aliyuncs.auth.sts.AssumeRoleResponse;
|
||||
import com.aliyuncs.http.MethodType;
|
||||
import com.aliyuncs.profile.DefaultProfile;
|
||||
import com.aliyuncs.profile.IClientProfile;
|
||||
import com.gxwebsoft.common.core.annotation.OperationLog;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.utils.FileServerUtil;
|
||||
import com.gxwebsoft.common.core.utils.RedisUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.system.entity.Company;
|
||||
import com.gxwebsoft.common.system.entity.FileRecord;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.service.CompanyService;
|
||||
import com.gxwebsoft.common.system.service.FileRecordService;
|
||||
import com.gxwebsoft.common.system.service.SettingService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 阿里云OSS云存储
|
||||
*
|
||||
* @author WebSoft
|
||||
* @since 2022-11-19 13:54:27
|
||||
*/
|
||||
@Tag(name = "阿里云OSS")
|
||||
@RestController
|
||||
@RequestMapping("/api/oss")
|
||||
public class AliOssController extends BaseController {
|
||||
@Resource
|
||||
private ConfigProperties config;
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
@Resource
|
||||
private FileRecordService fileRecordService;
|
||||
@Resource
|
||||
private CompanyService companyService;
|
||||
@Resource
|
||||
private SettingService settingService;
|
||||
|
||||
@Operation(summary = "上传文件")
|
||||
@PostMapping("/upload")
|
||||
public ApiResult<FileRecord> upload(@RequestParam MultipartFile file, HttpServletRequest request) throws Exception{
|
||||
// 获取租户ID
|
||||
String tenantId = request.getHeader("TenantId");
|
||||
String companyId = request.getHeader("CompanyId");
|
||||
String merchantId = request.getHeader("MerchantId");
|
||||
String groupId = request.getHeader("GroupId");
|
||||
String appId = request.getHeader("AppId");
|
||||
if(StrUtil.isBlank(tenantId)){
|
||||
return fail("传参错误",null);
|
||||
}
|
||||
|
||||
// 读取配置信息
|
||||
JSONObject settingInfo;
|
||||
String key3 = "Upload:" + tenantId;
|
||||
settingInfo = redisUtil.get(key3, JSONObject.class);
|
||||
if (ObjectUtil.isEmpty(settingInfo)) {
|
||||
settingInfo = settingService.getBySettingKey("upload");
|
||||
if (ObjectUtil.isNotEmpty(settingInfo)) {
|
||||
redisUtil.set(key3,settingInfo);
|
||||
}else {
|
||||
return fail("请先配置云存储",null);
|
||||
}
|
||||
}
|
||||
String endpoint = settingInfo.getString("bucketEndpoint");
|
||||
String bucketDomain = settingInfo.getString("bucketDomain");
|
||||
String bucketName = settingInfo.getString("bucketName");
|
||||
String accessKeyId = settingInfo.getString("accessKeyId");
|
||||
String accessKeySecret = settingInfo.getString("accessKeySecret");
|
||||
|
||||
// 判断是否登录
|
||||
String authorization = getAuthorization();
|
||||
|
||||
// 判断存储空间是否已满
|
||||
String key = "StorageIsFull:" + tenantId;
|
||||
String storageIsFull = redisUtil.get(key);
|
||||
if(StrUtil.isNotBlank(storageIsFull)){
|
||||
// 使用自定义云存储不限制
|
||||
if(bucketName.equals("oss-gxwebsoft")){
|
||||
return fail("存储空间已满", null);
|
||||
}
|
||||
}
|
||||
|
||||
// 上传文件结果
|
||||
FileRecord result;
|
||||
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
|
||||
|
||||
try {
|
||||
|
||||
String dir = getUploadDir();
|
||||
File upload = FileServerUtil.upload(file, dir, config.getUploadUuidName());
|
||||
String path = upload.getAbsolutePath().replace("\\", "/").substring(dir.length());
|
||||
String originalName = file.getOriginalFilename();
|
||||
|
||||
// 创建PutObjectRequest对象。
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, upload);
|
||||
// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
|
||||
// ObjectMetadata metadata = new ObjectMetadata();
|
||||
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
|
||||
// metadata.setObjectAcl(CannedAccessControlList.Private);
|
||||
// putObjectRequest.setMetadata(metadata);
|
||||
|
||||
// 上传文件。
|
||||
PutObjectResult ossResult = ossClient.putObject(putObjectRequest);
|
||||
|
||||
// 保存记录并返回
|
||||
result = new FileRecord();
|
||||
if(StrUtil.isNotBlank(authorization)){
|
||||
result.setCreateUserId(getLoginUserId());
|
||||
}
|
||||
if(StrUtil.isNotBlank(companyId)){
|
||||
result.setCompanyId(Integer.valueOf(companyId));
|
||||
}
|
||||
if(StrUtil.isNotBlank(merchantId)){
|
||||
result.setMerchantId(Long.valueOf(merchantId));
|
||||
}
|
||||
if(StrUtil.isNotBlank(groupId)){
|
||||
result.setGroupId(Integer.valueOf(groupId));
|
||||
}
|
||||
if(StrUtil.isNotBlank(appId)){
|
||||
result.setAppId(Integer.valueOf(appId));
|
||||
}
|
||||
path = "/".concat(path);
|
||||
result.setName(StrUtil.isBlank(originalName) ? upload.getName() : originalName);
|
||||
result.setLength(upload.length());
|
||||
result.setPath(bucketDomain + path);
|
||||
result.setThumbnail(bucketDomain + path + "?x-oss-process=image/resize,m_fixed,w_100,h_100/quality,Q_90");
|
||||
result.setUrl(bucketDomain + path + "?x-oss-process=image/resize,w_750/quality,Q_90");
|
||||
result.setDownloadUrl(bucketDomain + path);
|
||||
String contentType = FileServerUtil.getContentType(upload);
|
||||
result.setContentType(contentType);
|
||||
result.setTenantId(Integer.valueOf(tenantId));
|
||||
upload.delete();
|
||||
fileRecordService.save(result);
|
||||
// 更新存储空间
|
||||
if(companyId != null){
|
||||
Company company = companyService.getById(Integer.valueOf(companyId));
|
||||
company.setStorage(company.getStorage() + result.getLength());
|
||||
if(company.getStorage().compareTo(company.getStorageMax()) > 0){
|
||||
redisUtil.set(key,1);
|
||||
}
|
||||
companyService.updateById(company);
|
||||
}
|
||||
return success(result);
|
||||
|
||||
} catch (OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
System.out.println("Error Message:" + oe.getErrorMessage());
|
||||
System.out.println("Error Code:" + oe.getErrorCode());
|
||||
System.out.println("Request ID:" + oe.getRequestId());
|
||||
System.out.println("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
return fail("上传失败", null);
|
||||
}
|
||||
|
||||
@OperationLog
|
||||
@Operation(summary = "获取临时osstoken")
|
||||
@GetMapping("/getSTSToken")
|
||||
public ApiResult<?> getSTSToken() {
|
||||
final User loginUser = getLoginUser();
|
||||
if(loginUser == null){
|
||||
return fail("请先登录");
|
||||
}
|
||||
// STS接入地址,例如sts.cn-hangzhou.aliyuncs.com。
|
||||
String endpoint = "sts.cn-shenzhen.aliyuncs.com";
|
||||
// 填写步骤1生成的RAM用户访问密钥AccessKey ID和AccessKey Secret。
|
||||
String accessKeyId = "AAAAA";
|
||||
String accessKeySecret = "123456";
|
||||
// 填写步骤3获取的角色ARN。
|
||||
String roleArn = "acs:ram::1470199532233684:role/wsoss";
|
||||
// 自定义角色会话名称,用来区分不同的令牌,例如可填写为SessionTest。
|
||||
String roleSessionName = "wsoss";
|
||||
// 设置临时访问凭证的有效时间为3600秒。
|
||||
Long durationSeconds = 3600L;
|
||||
try {
|
||||
// regionId表示RAM的地域ID。以华东1(杭州)地域为例,regionID填写为cn-hangzhou。也可以保留默认值,默认值为空字符串("")。
|
||||
String regionId = "";
|
||||
// 添加endpoint。适用于Java SDK 3.12.0及以上版本。
|
||||
DefaultProfile.addEndpoint(regionId, "Sts", endpoint);
|
||||
// 构造default profile。
|
||||
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
|
||||
// 构造client。
|
||||
DefaultAcsClient client = new DefaultAcsClient(profile);
|
||||
final AssumeRoleRequest request = new AssumeRoleRequest();
|
||||
// 适用于Java SDK 3.12.0及以上版本。
|
||||
request.setSysMethod(MethodType.POST);
|
||||
// 适用于Java SDK 3.12.0以下版本。
|
||||
//request.setMethod(MethodType.POST);
|
||||
request.setRoleArn(roleArn);
|
||||
request.setRoleSessionName(roleSessionName);
|
||||
// request.setPolicy(policy);
|
||||
request.setDurationSeconds(durationSeconds);
|
||||
final AssumeRoleResponse response = client.getAcsResponse(request);
|
||||
return success(response);
|
||||
} catch (ClientException | com.aliyuncs.exceptions.ClientException e) {
|
||||
System.out.println("Failed:");
|
||||
System.out.println("Error message: " + e.getMessage());
|
||||
return fail(e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前端表单提交的参数
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "获取前端表单提交的参数")
|
||||
@GetMapping("/getPostForm")
|
||||
public ApiResult<?> getPostForm(){
|
||||
String endpoint = config.getEndpoint();
|
||||
// RAM用户的访问密钥(AccessKey ID和AccessKey Secret)。
|
||||
String accessKeyId = "LTAI5t8UTh8CTXEi2dYxobhj";
|
||||
String accessKeySecret = "123456";
|
||||
// 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
|
||||
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
|
||||
// 填写Bucket名称,例如examplebucket。
|
||||
String bucket = config.getBucketName();
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
|
||||
|
||||
try {
|
||||
String host = "https://" + bucket + "." + endpoint;
|
||||
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||
|
||||
|
||||
long expireTime = 60;
|
||||
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
PolicyConditions policyConds = new PolicyConditions();
|
||||
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE,0,100*1024*1024);
|
||||
|
||||
|
||||
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
|
||||
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
|
||||
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
||||
String postSignature = ossClient.calculatePostSignature(postPolicy);
|
||||
Map result = new HashMap<>();
|
||||
result.put("polocyBase64",encodedPolicy);
|
||||
result.put("signature",postSignature);
|
||||
result.put("expireEndTime",expireEndTime);
|
||||
return success(result);
|
||||
} finally {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传位置(服务器)
|
||||
*/
|
||||
private String getUploadDir() {
|
||||
return config.getUploadPath() + "/";
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user