SignVerificationAspect.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. @Log4j2
  27. @Aspect
  28. @Component
  29. @Order(101)
  30. public class SignVerificationAspect {
  31. private static final String GET_PRIVATEKEY_API = "/ucenter/_inner/pdsfsdfsrvateddsfeky/";
  32. @Value("${ucenter.publicKey}")
  33. private String publicKey;
  34. @Value("${ucenter.appId}")
  35. private String ucenterAppId;
  36. @Value("${fdService.basePath}")
  37. private String fdServiceBasePath;
  38. @Resource
  39. private MyClient myClient;
  40. /**
  41. * 前置通知 用于判断用户协作场景是否有协作权限
  42. *
  43. * @param joinPoint
  44. * 切点
  45. * @throws IOException
  46. */
  47. @Before("@annotation(com.fdkankan.openApi.aop.SignVerification)")
  48. public void doBefore(JoinPoint joinPoint) throws Exception {
  49. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  50. String sign = request.getHeader("sign");
  51. String appId = request.getHeader("appId");
  52. if(StrUtil.isEmpty(sign) || StrUtil.isEmpty(appId)){
  53. throw new BusinessException(ErrorCode.AUTH_FAIL);
  54. }
  55. //通过appid查询私钥
  56. Instant now = Instant.now();
  57. long epochSecond = now.getEpochSecond();
  58. JSONObject playload = new JSONObject();
  59. playload.put("appId", ucenterAppId);
  60. playload.put("timestamp", epochSecond);
  61. String ucenterSign = RsaUtils.encipher(playload.toJSONString(), publicKey);
  62. Map<String, String> headerMap = new HashMap<>();
  63. headerMap.put("sign", ucenterSign);
  64. headerMap.put("appId", ucenterAppId);
  65. String url = fdServiceBasePath + GET_PRIVATEKEY_API + appId;
  66. Result result = myClient.get(url, headerMap);
  67. if(result.getCode() != ServerCode.SUCCESS.code()){
  68. throw new RuntimeException("系统异常");
  69. }
  70. String privateKey = (String) result.getData();
  71. //签名解密
  72. if(!SignUtils.checkSign(sign, appId, privateKey)){
  73. throw new BusinessException(ErrorCode.AUTH_FAIL);
  74. }
  75. }
  76. }