DESResponseSecretAspect.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.template.aop;
  2. import com.alibaba.fastjson.JSON;
  3. import com.template.annotation.DESRespondSecret;
  4. import com.template.common.utils.EncryptUtil;
  5. import com.template.model.result.CommonResult;
  6. import com.template.model.seewo.OrganizationNodeServiceInitSchoolClassesParam;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.ProceedingJoinPoint;
  9. import org.aspectj.lang.annotation.AfterReturning;
  10. import org.aspectj.lang.annotation.Around;
  11. import org.aspectj.lang.annotation.Aspect;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.aspectj.lang.reflect.MethodSignature;
  14. import org.springframework.stereotype.Component;
  15. import java.lang.reflect.Method;
  16. /**
  17. * @Author: binguo
  18. * @Date: 2024/2/26 星期一 10:02
  19. * @Description: com.template.aop
  20. * @Version: 1.0
  21. */
  22. @Component
  23. @Aspect
  24. //规定AOP的执行顺序
  25. //Order(1) 多个aop时可通过该注解来确定执行顺序
  26. public class DESResponseSecretAspect {
  27. // 定义切点,使用了@DESRespondSecret注解的类 或 使用了@DESRespondSecret注解的方法
  28. /**
  29. * 切点,方法上有注解或类上有注解
  30. * 拦截类或者是方法上标注注解的方法
  31. */
  32. @Pointcut("@within(com.template.annotation.DESRespondSecret) || @annotation(com.template.annotation.DESRespondSecret)")
  33. public void pointCut() {
  34. }
  35. @Around("pointCut()")
  36. public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
  37. Object rvt = joinPoint.proceed(joinPoint.getArgs());
  38. return rvt;
  39. }
  40. /**
  41. * 与After的区别在于AfterReturning只有在方法执行成功之后才会被植入,如果After和
  42. * AfterReturning同时存在于一个文件中,谁写在前面谁先运行
  43. *
  44. * @param joinpoint
  45. * @param rvt
  46. * @return
  47. */
  48. @AfterReturning(pointcut = "execution(public * com.template.controller.*.*(..))", returning = "rvt")
  49. public Object log(JoinPoint joinpoint, Object rvt) {
  50. //获取被代理对象
  51. Object target = joinpoint.getTarget();
  52. //获取通知签名 获取方法上的DESRespondSecret注解
  53. MethodSignature signature = (MethodSignature) joinpoint.getSignature();
  54. try {
  55. // 获取被代理方法
  56. Method pointMethod = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
  57. // 获取被代理方法上面的注解@DESRespondSecret
  58. DESRespondSecret secret = pointMethod.getAnnotation(DESRespondSecret.class);
  59. // 被代理方法上没有,则说明@DESRespondSecret注解在被代理类上
  60. if (secret == null) {
  61. secret = target.getClass().getAnnotation(DESRespondSecret.class);
  62. }
  63. //类上也没有 直接返回
  64. if (secret == null) {
  65. return rvt;
  66. }
  67. // 如果有,并且值为false,则不进行加密
  68. if (secret != null && !secret.validated()) {
  69. return rvt;
  70. } else {
  71. CommonResult baseVo1 = (CommonResult) rvt;
  72. if(baseVo1 != null){
  73. // 获取返回值json字符串
  74. Object data = baseVo1.getData();
  75. if (null != data) {
  76. String jsonString = JSON.toJSONString(data);
  77. // 加密
  78. String s = EncryptUtil.encrypt(jsonString);
  79. baseVo1.setData(s);
  80. }
  81. }
  82. return baseVo1;
  83. }
  84. } catch (Throwable throwable) {
  85. throwable.printStackTrace();
  86. }
  87. return rvt;
  88. }
  89. }