MybatisSqlIntercept.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.dk.common.infrastructure.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.dk.common.util.oauth.AESSecurityUtil;
  4. import com.dk.common.util.oauth.JwtUtil;
  5. import com.google.common.base.CaseFormat;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.apache.ibatis.builder.StaticSqlSource;
  8. import org.apache.ibatis.executor.Executor;
  9. import org.apache.ibatis.mapping.MappedStatement;
  10. import org.apache.ibatis.mapping.SqlSource;
  11. import org.apache.ibatis.plugin.Interceptor;
  12. import org.apache.ibatis.plugin.Intercepts;
  13. import org.apache.ibatis.plugin.Invocation;
  14. import org.apache.ibatis.plugin.Signature;
  15. import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
  16. import org.apache.ibatis.scripting.defaults.RawSqlSource;
  17. import org.apache.ibatis.session.ResultHandler;
  18. import org.apache.ibatis.session.RowBounds;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Component;
  22. import javax.servlet.http.HttpServletRequest;
  23. import java.beans.BeanInfo;
  24. import java.beans.Introspector;
  25. import java.beans.PropertyDescriptor;
  26. import java.lang.reflect.Field;
  27. import java.lang.reflect.Method;
  28. import java.lang.reflect.Proxy;
  29. import java.util.HashMap;
  30. import java.util.LinkedHashMap;
  31. import java.util.Map;
  32. /**
  33. * @author 沈博
  34. * @date_time 2022年3月11日14:10:19
  35. * @description 拦截方法添加国际化参数 (目前有点小问题 url拼参数的无法添加)
  36. * TODO selectById的非Map类型参数的暂不支持 待修改
  37. */
  38. @Slf4j
  39. @Component
  40. @Intercepts({
  41. @Signature(type = Executor.class, method = "query", args = {MappedStatement.class,
  42. Object.class, RowBounds.class, ResultHandler.class})})
  43. public class MybatisSqlIntercept implements Interceptor {
  44. public static String I18N = "";
  45. // public static String primaryKey = null;
  46. @Value("${aes-key}")
  47. private String AESKey;
  48. @Autowired
  49. private HttpServletRequest httpServletRequest;
  50. @Override
  51. public Object intercept(Invocation invocation) throws Throwable {
  52. Integer cpId = 0;
  53. if(httpServletRequest==null){
  54. log.info("mybatis查询拦截器=>请求头为空");
  55. return new Invocation(invocation.getTarget(), invocation.getMethod(), invocation.getArgs()).proceed();
  56. }
  57. String authorization = httpServletRequest.getHeader("Authorization");
  58. if(authorization != null) {
  59. String[] tokens = authorization.split(" ");
  60. String decrypt = AESSecurityUtil.decrypt(AESKey, tokens[1]);
  61. String cpIdString = JwtUtil.getCpId(decrypt);
  62. if (cpIdString != null) {
  63. //设置企业Id
  64. cpId = Integer.valueOf(cpIdString);
  65. }
  66. }
  67. Object[] args = invocation.getArgs();
  68. Object[] args2 = new Object[4];
  69. args2[3] = args[3];
  70. args2[2] = args[2];
  71. if ((args[1] instanceof Map || args[1] instanceof Object) && checkType(args)) {
  72. Object argsNo2 = args[1];
  73. //map类型,对于数字的转换,如果直接转,Long给转成了Integer
  74. Map<String, Object> argsNo2Map = null;
  75. if (args[1] instanceof Map) {
  76. argsNo2Map = new HashMap<>();
  77. for (String key : ((Map<String, Object>) args[1]).keySet()) {
  78. argsNo2Map.put(key, ((Map<String, Object>) args[1]).get(key));
  79. }
  80. } else {
  81. try {
  82. argsNo2Map = convertBeanToMap(args[1]);
  83. } catch (Exception ignored) {
  84. }
  85. }
  86. if (argsNo2Map != null) {
  87. argsNo2Map.put("i18n", I18N);
  88. if (cpId != 0 && argsNo2Map.get("cpId") == null) {
  89. argsNo2Map.put("cpId", cpId);
  90. }
  91. args2[1] = argsNo2Map;
  92. }
  93. } else {
  94. Map<String, Object> argsNo2Map = new HashMap<>();
  95. // 默认叫id
  96. argsNo2Map.put("id", args[1]);
  97. argsNo2Map.put("i18n", I18N);
  98. if (cpId != 0) {
  99. argsNo2Map.put("cpId", cpId);
  100. }
  101. args2[1] = argsNo2Map;
  102. // if(primaryKey != null ){
  103. // Map<String, Object> argsNo2Map = new HashMap<>();
  104. // // 默认叫id
  105. // argsNo2Map.put(primaryKey,args[1]);
  106. // argsNo2Map.put("i18n", I18N);
  107. // if(cpId != 0){
  108. // argsNo2Map.put("cpId", cpId);
  109. // }
  110. // args2[1] =argsNo2Map;
  111. // }else{
  112. // args2[1] = args[1];
  113. // }
  114. //TODO 在此添加单参数的逻辑 做法是获取父类的方法的参数名 然后把单参数转成Map传给xml
  115. }
  116. args2[0] = args[0];
  117. Invocation newInvocation = new Invocation(
  118. invocation.getTarget(), invocation.getMethod(), args2
  119. );
  120. // MappedStatement mappedStatement = (MappedStatement) args[0];
  121. // String cmdType = mappedStatement.getSqlCommandType().name();
  122. // if ("update".equalsIgnoreCase(cmdType)) {
  123. // List<Map<String,Object>> objList = SpringUtils.getBean(PlugMapper.class)
  124. // }
  125. return newInvocation.proceed();
  126. }
  127. private boolean checkType(Object[] args) {
  128. return !(args[1] instanceof Long) && !(args[1] instanceof Integer) && !(args[1] instanceof String);
  129. }
  130. /**
  131. * @desc : 实体转Map
  132. * @author : 洪旭东
  133. * @date : 2023-03-27 11:18
  134. */
  135. public Map<String, Object> convertBeanToMap(Object obj) {
  136. if (obj == null) {
  137. return null;
  138. }
  139. Map<String, Object> map = new HashMap<>(16);
  140. try {
  141. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  142. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  143. for (PropertyDescriptor property : propertyDescriptors) {
  144. String key = property.getName();
  145. // 过滤class属性
  146. if (!key.equals("class")) {
  147. // 得到property对应的getter方法
  148. Method getter = property.getReadMethod();
  149. Object value = getter.invoke(obj);
  150. if (null != value) {
  151. map.put(key, value);
  152. }
  153. }
  154. }
  155. } catch (Exception ignore) {
  156. return null;
  157. }
  158. return map;
  159. }
  160. }