36a8594a9061422f9e7edc225132bb0f3ea71657.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package org.jeecg.common.aspect;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Pointcut;
  7. import org.aspectj.lang.reflect.MethodSignature;
  8. import org.jeecg.common.api.CommonAPI;
  9. import org.jeecg.common.aspect.annotation.PermissionData;
  10. import org.jeecg.common.system.util.JeecgDataAutorUtils;
  11. import org.jeecg.common.system.util.JwtUtil;
  12. import org.jeecg.common.system.vo.SysPermissionDataRuleModel;
  13. import org.jeecg.common.system.vo.SysUserCacheInfo;
  14. import org.jeecg.common.util.SpringContextUtils;
  15. import org.jeecg.common.util.oConvertUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Component;
  18. import javax.servlet.http.HttpServletRequest;
  19. import java.lang.reflect.Method;
  20. import java.util.List;
  21. /**
  22. * 数据权限切面处理类
  23. * 当被请求的方法有注解PermissionData时,会在往当前request中写入数据权限信息
  24. * @Date 2019年4月10日
  25. * @Version: 1.0
  26. */
  27. @Aspect
  28. @Component
  29. @Slf4j
  30. public class PermissionDataAspect {
  31. @Autowired
  32. private CommonAPI commonAPI;
  33. @Pointcut("@annotation(org.jeecg.common.aspect.annotation.PermissionData)")
  34. public void pointCut() {
  35. }
  36. @Around("pointCut()")
  37. public Object arround(ProceedingJoinPoint point) throws Throwable{
  38. HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
  39. MethodSignature signature = (MethodSignature) point.getSignature();
  40. Method method = signature.getMethod();
  41. PermissionData pd = method.getAnnotation(PermissionData.class);
  42. String component = pd.pageComponent();
  43. String requestMethod = request.getMethod();
  44. String requestPath = request.getRequestURI().substring(request.getContextPath().length());
  45. requestPath = filterUrl(requestPath);
  46. log.debug("拦截请求 >> "+requestPath+";请求类型 >> "+requestMethod);
  47. String username = JwtUtil.getUserNameByToken(request);
  48. //查询数据权限信息
  49. //TODO 微服务情况下也得支持缓存机制
  50. List<SysPermissionDataRuleModel> dataRules = commonAPI.queryPermissionDataRule(component, requestPath, username);
  51. if(dataRules!=null && dataRules.size()>0) {
  52. //临时存储
  53. JeecgDataAutorUtils.installDataSearchConditon(request, dataRules);
  54. //TODO 微服务情况下也得支持缓存机制
  55. SysUserCacheInfo userinfo = commonAPI.getCacheUser(username);
  56. JeecgDataAutorUtils.installUserInfo(request, userinfo);
  57. }
  58. return point.proceed();
  59. }
  60. private String filterUrl(String requestPath){
  61. String url = "";
  62. if(oConvertUtils.isNotEmpty(requestPath)){
  63. url = requestPath.replace("\\", "/");
  64. url = url.replace("//", "/");
  65. if(url.indexOf("//")>=0){
  66. url = filterUrl(url);
  67. }
  68. /*if(url.startsWith("/")){
  69. url=url.substring(1);
  70. }*/
  71. }
  72. return url;
  73. }
  74. /**
  75. * 获取请求地址
  76. * @param request
  77. * @return
  78. */
  79. private String getJgAuthRequsetPath(HttpServletRequest request) {
  80. String queryString = request.getQueryString();
  81. String requestPath = request.getRequestURI();
  82. if(oConvertUtils.isNotEmpty(queryString)){
  83. requestPath += "?" + queryString;
  84. }
  85. if (requestPath.indexOf("&") > -1) {// 去掉其他参数(保留一个参数) 例如:loginController.do?login
  86. requestPath = requestPath.substring(0, requestPath.indexOf("&"));
  87. }
  88. if(requestPath.indexOf("=")!=-1){
  89. if(requestPath.indexOf(".do")!=-1){
  90. requestPath = requestPath.substring(0,requestPath.indexOf(".do")+3);
  91. }else{
  92. requestPath = requestPath.substring(0,requestPath.indexOf("?"));
  93. }
  94. }
  95. requestPath = requestPath.substring(request.getContextPath().length() + 1);// 去掉项目路径
  96. return filterUrl(requestPath);
  97. }
  98. private boolean moHuContain(List<String> list,String key){
  99. for(String str : list){
  100. if(key.contains(str)){
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. }