ApiValidateAspect.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.sooka.common.aop;
  2. import com.sooka.common.annotation.ApiValidate;
  3. import com.sooka.common.exception.ApiException;
  4. import com.sooka.common.utils.CheckSumUtil;
  5. import com.sooka.common.utils.StrUtil;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.web.context.request.RequestContextHolder;
  13. import org.springframework.web.context.request.ServletRequestAttributes;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import java.lang.reflect.Method;
  17. @Aspect
  18. @Component
  19. public class ApiValidateAspect {
  20. private String appSecrt ="jdeFDS89HFassdsfFDNDS73FDJK";
  21. private String[] appids={"1000","2000"};
  22. private static final Logger log = LoggerFactory.getLogger(ApiValidateAspect.class);
  23. @Around("@annotation(com.sooka.common.annotation.ApiValidate)")
  24. public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
  25. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  26. HttpServletRequest request = attributes.getRequest();
  27. HttpServletResponse response = attributes.getResponse();
  28. return this.validation(joinPoint,request,response);
  29. }
  30. public Object validation(ProceedingJoinPoint joinPoint, HttpServletRequest request,HttpServletResponse response) throws Throwable {
  31. /*获取请求参数*/
  32. String appId= request.getParameter("appid");
  33. String nonce= request.getParameter("nonce");
  34. String signature= request.getParameter("signature");
  35. String timestamp= request.getParameter("timestamp");
  36. /*获取方法名称*/
  37. String methodName = joinPoint.getSignature().getName();
  38. Method method = currentMethod(joinPoint, methodName);
  39. ApiValidate log = method.getAnnotation(ApiValidate.class);
  40. /* 验证appId */
  41. if(StrUtil.isBlank(appId)) {
  42. throw new ApiException("appId Can not be empty!");
  43. }
  44. if(!StrUtil.isContain(appids,appId)) {
  45. throw new ApiException("appId validate failed!");
  46. }
  47. /* 是否需要验证 Signature */
  48. if(!log.checkSignature()) {
  49. return joinPoint.proceed();
  50. }
  51. /* 验证timestamp */
  52. if(StrUtil.isBlank(timestamp)) {
  53. throw new ApiException("timestamp Can not be empty!");
  54. }
  55. if((Long.parseLong(CheckSumUtil.getTimestamp())-Long.parseLong(timestamp))>220) {
  56. throw new ApiException("the signature has be Expired!");
  57. }
  58. /* 验证signature */
  59. if(StrUtil.isBlank(signature)) {
  60. throw new ApiException("signature Can not be empty!");
  61. }
  62. if(!CheckSumUtil.getCheckSum(appSecrt,nonce,timestamp).trim().equals(signature.trim())) {
  63. throw new ApiException("API interface parameter validation failed!");
  64. }
  65. return joinPoint.proceed();
  66. }
  67. public Method currentMethod(ProceedingJoinPoint joinPoint,String methodName){
  68. Method[] methods = joinPoint.getTarget().getClass().getMethods();
  69. Method resultMethod = null;
  70. for (Method method : methods) {
  71. if (method.getName().equals(methodName)) {
  72. resultMethod = method;
  73. break;
  74. }
  75. }
  76. return resultMethod;
  77. }
  78. }