470555454ba7830567735c8a9439e8ba52fda74a.svn-base 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package cn.com.goldenwater.dcproj.target;
  2. import cn.com.goldenwater.dcproj.constValue.CommonLabel;
  3. import cn.com.goldenwater.dcproj.dao.BisInspAllRlationPersDao;
  4. import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers;
  5. import cn.com.goldenwater.dcproj.utils.RequestUtils;
  6. import cn.com.goldenwater.target.CheckException;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import io.swagger.annotations.ApiModelProperty;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.aspectj.lang.JoinPoint;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.annotation.Before;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.web.context.request.RequestContextHolder;
  20. import org.springframework.web.context.request.ServletRequestAttributes;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.lang.reflect.Field;
  24. import java.util.Arrays;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * zhengdafei bean验证切面,作用域为系统中所有的实体参数
  30. */
  31. @Aspect
  32. @Component("verifyBeanAspect")
  33. public class VerifyBeanAspect {
  34. private static final Logger log = LoggerFactory.getLogger(VerifyBeanAspect.class);
  35. private final static String[] badSqlflag = new String[]{"$", " or", "select"
  36. , "&", " and", "drop", " 1=", "delete", "update", "from", "insert", "\\'", "execute", "dual", " order ", " by "};
  37. @Autowired
  38. private BisInspAllRlationPersDao inspAllRlationPersDao;
  39. @Pointcut("execution(* cn.com.goldenwater.dcproj.controller..*(..)) && !execution(* cn.com.goldenwater.dcproj.controller.index..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.sso..*(..))&& !execution(* cn.com.goldenwater.dcproj.controller.ducha..*(..))&& !execution(* cn.com.goldenwater.dcproj.task..*(..))\"")
  40. // @Pointcut("@annotation(cn.com.goldenwater.dcproj.target.VerifyBean)")
  41. public void verifyBeanAspect() {
  42. }
  43. /**
  44. * 前置通知:目标方法执行之前执行以下方法体的内容
  45. *
  46. * @param jp
  47. */
  48. @Before(value = "verifyBeanAspect()")
  49. public void beforeMethod(JoinPoint jp) throws Exception {
  50. getAnnotationDesc(jp);
  51. }
  52. private String getAnnotationDesc(JoinPoint joinPoint) throws Exception {
  53. List args = Arrays.asList(joinPoint.getArgs());
  54. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  55. HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
  56. String paramPersId = RequestUtils.getPersId(request, response);
  57. String orgId = request.getHeader(CommonLabel.ORGId);
  58. if ("041".equals(orgId)) {
  59. return "";
  60. }
  61. if (args.size() > 0) {
  62. String reqPath = request.getRequestURL().toString();
  63. if (reqPath.contains("bis/insp/key/register") || reqPath.contains("pers/position")) {
  64. //172工程先放过
  65. return "";
  66. }
  67. String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
  68. if (path.equals("/bis/insp/news") ) {
  69. //通知公告 放过
  70. return "";
  71. }
  72. String persId = request.getHeader(CommonLabel.PERSID);
  73. BisInspAllRlationPers inspAllRlationPers = inspAllRlationPersDao.get(persId);
  74. for (Object object : args) {
  75. if (object == null) {
  76. continue;
  77. }
  78. Field[] fields = object.getClass().getDeclaredFields();
  79. for (Field field : fields) {
  80. field.setAccessible(true);
  81. //所有字符数据校验
  82. String name = field.getName();
  83. if ("inIdsSql".equals(name)) {
  84. continue;
  85. }
  86. String fieldStr = String.valueOf(field.get(object));
  87. if (StringUtils.isBlank(fieldStr) || "null".equals(fieldStr)) {
  88. continue;
  89. }
  90. if ("orgId".equals(name) || "orgid".equals(name) || "persType".equals(name) || "ownerSystem".equals(name)) {
  91. if ("persType".equals(name)) {
  92. if (!"041".equals(orgId)) {
  93. if (!reqPath.contains("/sys/roles/updateRole")) {
  94. if (!fieldStr.equals(inspAllRlationPers.getPersType())) {
  95. int sysRole = Integer.parseInt(inspAllRlationPers.getPersType());
  96. //参数权限
  97. int paramRole = Integer.parseInt(fieldStr);
  98. if (sysRole > paramRole) {
  99. throw new CheckException("role系统存在危险操作!!");
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if ("ownerSystem".equals(name)) {
  106. if (!fieldStr.equals(inspAllRlationPers.getOwnerSystem())) {
  107. throw new CheckException("ownerSystem系统存在危险操作!!");
  108. }
  109. }
  110. }
  111. //针对该参数的二次校验,防止系统中有遗漏
  112. if ("persid".equals(name.toLowerCase())) {
  113. //针对特定接口进行排除。有些接口可能不需要处理
  114. if (StringUtils.isNotBlank(fieldStr) && !"null".equals(fieldStr)) {
  115. if (StringUtils.isNotBlank(paramPersId)) {
  116. if (!fieldStr.equals(paramPersId)) {
  117. throw new CheckException("[paramPersId]:" + paramPersId + "[persId]:" + fieldStr + ",当前用户无权限执行该操作!");
  118. }
  119. }
  120. }
  121. }
  122. ValidInValue validInValue = field.getAnnotation(ValidInValue.class);
  123. if (validInValue != null) {
  124. checkBadStr(fieldStr, field, name, object);
  125. }
  126. if (field.getAnnotation(ApiModelProperty.class) != null) {
  127. String notes = field.getAnnotation(ApiModelProperty.class).notes();
  128. //有必填标准的数据
  129. if (field.getAnnotation(ApiModelProperty.class).required()) {
  130. if (field.get(object) == null || "".equals(field.get(object))) {
  131. throw new CheckException(notes + "[" + name + "]不能为空:" + field.get(object));
  132. }
  133. } else {
  134. Object fieldStr2 = field.get(object);
  135. if (fieldStr2 instanceof String) {
  136. String fieldS = String.valueOf(fieldStr2);
  137. checkBadStr(fieldS, field, name, fieldStr2);
  138. } else if (fieldStr2 instanceof List) {
  139. checkList(fieldStr2, name, field, object);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. return "";
  147. }
  148. /**
  149. * 递归检查list内部参数
  150. */
  151. private void checkList(Object fieldStr2, String name, Field field, Object object) throws Exception {
  152. String jsonObject = JSONObject.toJSONString(fieldStr2);
  153. JSONArray jsonArray = JSONArray.parseArray(jsonObject);
  154. JSONObject object1 = null;
  155. for (int i = 0; i < jsonArray.size(); i++) {
  156. object1 = jsonArray.getJSONObject(i);
  157. Iterator iter = object1.entrySet().iterator();
  158. while (iter.hasNext()) {
  159. Map.Entry entry = (Map.Entry) iter.next();
  160. Object value = entry.getValue();
  161. if (value == null || "".equals(value)) {
  162. continue;
  163. }
  164. if (value instanceof String) {
  165. checkBadStr(String.valueOf(value), field, name, object);
  166. } else if (value instanceof List) {
  167. checkList(value, name, field, object);
  168. }
  169. }
  170. }
  171. }
  172. private void checkBadStr(String fieldStr, Field field, String name, Object object) throws Exception {
  173. if (StringUtils.isNotBlank(fieldStr)) {
  174. for (String str : badSqlflag) {
  175. if (fieldStr.contains(str)) {
  176. if (fieldStr.startsWith("[")) {
  177. continue;
  178. }
  179. throw new CheckException("[" + name + "]存在特殊字符:" + field.get(object));
  180. }
  181. }
  182. }
  183. }
  184. }