RequestCipherInterceptor.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.izouma.nineth.aspect;
  2. import com.anji.captcha.util.StringUtils;
  3. import com.izouma.nineth.exception.BusinessException;
  4. import com.izouma.nineth.service.security.RequestCipherService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Pointcut;
  10. import org.aspectj.lang.reflect.MethodSignature;
  11. import org.springframework.aop.support.AopUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
  14. import org.springframework.expression.ExpressionParser;
  15. import org.springframework.expression.spel.standard.SpelExpressionParser;
  16. import org.springframework.stereotype.Component;
  17. import java.lang.annotation.Annotation;
  18. import java.lang.reflect.Method;
  19. import java.util.Objects;
  20. /**
  21. *
  22. * 加解密处理
  23. *
  24. */
  25. @Aspect
  26. @Component
  27. @Slf4j
  28. public class RequestCipherInterceptor {
  29. private static final LocalVariableTableParameterNameDiscoverer DISCOVERER = new LocalVariableTableParameterNameDiscoverer();
  30. private static final ExpressionParser PARSER = new SpelExpressionParser();
  31. @Autowired
  32. private RequestCipherService requestCipherService;
  33. @Pointcut("@annotation(com.izouma.nineth.aspect.RequestCipher)")
  34. public void pointcut() {
  35. }
  36. @Around("pointcut()")
  37. public Object doAround(ProceedingJoinPoint point) throws Throwable {
  38. MethodSignature methodSignature = (MethodSignature) point.getSignature();
  39. Method targetMethod = AopUtils.getMostSpecificMethod(methodSignature.getMethod(), point.getTarget().getClass());
  40. String targetName = point.getTarget().getClass().getName();
  41. String methodName = point.getSignature().getName();
  42. Object[] arguments = point.getArgs();
  43. boolean hasProcess = false;
  44. Annotation[][] annotations = targetMethod.getParameterAnnotations();
  45. for(int i = 0 ; i < annotations.length; i++){
  46. Annotation[] itemAry = annotations[i];
  47. //加密或解密参数
  48. for(Annotation item : itemAry){
  49. if(item instanceof CipherParam){
  50. CipherParam cipherParam = (CipherParam) item;
  51. Object res = null;
  52. String cipherData = Objects.toString(arguments[i], "");
  53. if(StringUtils.isEmpty(cipherData)){
  54. throw new BusinessException("密文不能为空");
  55. }
  56. if(cipherParam.mode()){
  57. res = requestCipherService.encryptData(cipherData);
  58. }else{
  59. res = requestCipherService.decryptData(cipherData);
  60. }
  61. //设置处理后的数据
  62. arguments[i] = res;
  63. hasProcess = true;
  64. }
  65. }
  66. }
  67. //如果没有标记处理,默认处理第一个
  68. if(!hasProcess && arguments.length > 0){
  69. //默认解密操作
  70. Object res = requestCipherService.decryptData(Objects.toString(arguments[0], ""));
  71. arguments[0] = res;
  72. }
  73. Object result = point.proceed(arguments);
  74. return result;
  75. }
  76. }