CheckSignatureAspect.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.fdkankan.cloud.acl.aop;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.fdkankan.cloud.acl.entity.AppConfig;
  6. import com.fdkankan.cloud.acl.entity.AppKeyConfig;
  7. import com.fdkankan.cloud.acl.service.IAppConfigService;
  8. import com.fdkankan.cloud.acl.service.IAppKeyConfigService;
  9. import com.fdkankan.common.constant.ErrorCode;
  10. import com.fdkankan.common.exception.BusinessException;
  11. import com.fdkankan.common.util.RsaUtil;
  12. import com.mybatisflex.core.query.QueryWrapper;
  13. import lombok.extern.log4j.Log4j2;
  14. import org.aspectj.lang.JoinPoint;
  15. import org.aspectj.lang.annotation.Aspect;
  16. import org.aspectj.lang.annotation.Before;
  17. import org.aspectj.lang.annotation.Pointcut;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.core.annotation.Order;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.web.context.request.RequestContextHolder;
  22. import org.springframework.web.context.request.ServletRequestAttributes;
  23. import javax.crypto.Cipher;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.io.IOException;
  26. import java.nio.charset.StandardCharsets;
  27. import java.security.KeyFactory;
  28. import java.security.PrivateKey;
  29. import java.security.spec.PKCS8EncodedKeySpec;
  30. import java.time.Instant;
  31. import java.util.Objects;
  32. @Log4j2
  33. @Aspect
  34. @Component
  35. @Order(101)
  36. public class CheckSignatureAspect {
  37. @Autowired
  38. private IAppConfigService appConfigService;
  39. @Autowired
  40. private IAppKeyConfigService appKeyConfigService;
  41. @Pointcut("@annotation(com.fdkankan.cloud.acl.annotation.CheckSignature)")
  42. public void CheckSignature() {
  43. }
  44. @Before("CheckSignature()")
  45. public void doBefore(JoinPoint joinPoint) throws Exception {
  46. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  47. String signature = request.getHeader("signature");
  48. if(StrUtil.isEmpty(signature)){
  49. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "signature cannot be empty");
  50. }
  51. String appCode = request.getParameter("appCode");
  52. if(StrUtil.isEmpty(appCode)){
  53. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "appCode cannot be empty");
  54. }
  55. String timestamp = request.getParameter("timestamp");
  56. if(StrUtil.isEmpty(timestamp)){
  57. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "timestamp cannot be empty");
  58. }
  59. //时间戳有效时间是10秒
  60. Instant now = Instant.now();
  61. long epochSecond = now.getEpochSecond();
  62. long expiraSecond = Long.valueOf(timestamp) + 10 * 60L;
  63. if(expiraSecond < epochSecond){
  64. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "signature expired");
  65. }
  66. AppConfig appConfig = appConfigService.getByAppCode(appCode);
  67. if(Objects.isNull(appConfig)){
  68. throw new BusinessException(ErrorCode.FAILURE_CODE_3003);
  69. }
  70. AppKeyConfig appKeyConfig = appKeyConfigService.getByAppConfigId(appConfig.getId());
  71. if(Objects.isNull(appKeyConfig)){
  72. throw new BusinessException(ErrorCode.FAILURE_CODE_3003);
  73. }
  74. signature = RsaUtil.create(appKeyConfig.getPrivateKey(), null).decryptByPrivateKey(signature);
  75. log.info("解密:{}", signature);
  76. String[] split = signature.split("-");
  77. if(split.length != 2){
  78. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "signature mismatch");
  79. }
  80. String signatureAppCode = split[0];
  81. String signatureTimestamp = split[1];
  82. if(!appCode.equals(signatureAppCode) || !timestamp.equals(signatureTimestamp)){
  83. throw new BusinessException(ErrorCode.FAILURE_CODE_3003.code(), "signature mismatch");
  84. }
  85. }
  86. public static void main(String[] args) {
  87. String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHmJkGNg0N0tKroJAbqdO6ndgdEgJBnClW3KhzUCQSLVYhBvewjlXRmc1KQbI7QHpcdbuhvGT/RVVu4npVRPnQilGSyOxLbDI4TKaM6ZMSYQ1RS5vTj2HbvJ2s21AjEhhRcDYvSEDs4KsZaOmta/Cfok8jfG46o3UB6LkwzCMHtQIDAQAB";
  88. String test = "af9c663f3fd744c6bf40dbcd1c9aada3-" + "1719828816";
  89. String s = RsaUtil.create(null, publicKey).encryptByPublicKey(test);
  90. System.out.println(s);
  91. }
  92. }