SignVerificationAspect.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.fdkankan.openApi.aop;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fdkankan.common.constant.ErrorCode;
  5. import com.fdkankan.common.constant.ServerCode;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.web.response.Result;
  8. import com.fdkankan.openApi.httpclient.client.MyClient;
  9. import com.fdkankan.sign.RsaUtils;
  10. import com.fdkankan.sign.SignUtils;
  11. import lombok.extern.log4j.Log4j2;
  12. import org.aspectj.lang.JoinPoint;
  13. import org.aspectj.lang.annotation.Aspect;
  14. import org.aspectj.lang.annotation.Before;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.core.annotation.Order;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.web.context.request.RequestContextHolder;
  19. import org.springframework.web.context.request.ServletRequestAttributes;
  20. import javax.annotation.Resource;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.io.IOException;
  23. import java.time.Instant;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. @Log4j2
  28. @Aspect
  29. @Component
  30. @Order(101)
  31. public class SignVerificationAspect {
  32. private static final String GET_PRIVATEKEY_API = "/ucenter/_inner/pdsfsdfsrvateddsfeky/";
  33. @Value("${ucenter.publicKey}")
  34. private String publicKey;
  35. @Value("${ucenter.appId}")
  36. private String ucenterAppId;
  37. @Value("${4dkk.fdService.basePath}")
  38. private String fdServiceBasePath;
  39. @Resource
  40. private MyClient myClient;
  41. /**
  42. * 前置通知 用于判断用户协作场景是否有协作权限
  43. *
  44. * @param joinPoint
  45. * 切点
  46. * @throws IOException
  47. */
  48. @Before("@annotation(com.fdkankan.openApi.aop.SignVerification)")
  49. public void doBefore(JoinPoint joinPoint) throws Exception {
  50. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  51. String sign = request.getHeader("sign");
  52. String appId = request.getHeader("appId");
  53. if(StrUtil.isEmpty(sign) || StrUtil.isEmpty(appId)){
  54. throw new BusinessException(ErrorCode.AUTH_FAIL);
  55. }
  56. //通过appid查询私钥
  57. JSONObject playload = new JSONObject();
  58. playload.put("appId", ucenterAppId);
  59. playload.put("timestamp", System.currentTimeMillis());
  60. String ucenterSign = RsaUtils.encipher(playload.toJSONString(), publicKey);
  61. Map<String, String> headerMap = new HashMap<>();
  62. headerMap.put("sign", ucenterSign);
  63. headerMap.put("appId", ucenterAppId);
  64. String url = fdServiceBasePath + GET_PRIVATEKEY_API + appId;
  65. Result result = myClient.get(url, headerMap);
  66. if(result.getCode() != ServerCode.SUCCESS.code()){
  67. throw new RuntimeException("系统异常");
  68. }
  69. JSONObject data = (JSONObject) result.getData();
  70. if(Objects.isNull(data)){
  71. throw new BusinessException(ErrorCode.AUTH_FAIL);
  72. }
  73. String privateKey = data.getString("privateKey");
  74. //签名解密
  75. if(!SignUtils.checkSign(sign, appId, privateKey)){
  76. throw new BusinessException(ErrorCode.AUTH_FAIL);
  77. }
  78. }
  79. }