kangwei :mybaits拦截器添加权限注解校验方法
添加权限注解
正在显示
3 个修改的文件
包含
55 行增加
和
1 行删除
1 | package com.skua.tool.annotation; | ||
2 | |||
3 | import java.lang.annotation.Documented; | ||
4 | import java.lang.annotation.ElementType; | ||
5 | import java.lang.annotation.Retention; | ||
6 | import java.lang.annotation.RetentionPolicy; | ||
7 | import java.lang.annotation.Target; | ||
8 | |||
9 | /** | ||
10 | * 匿名访问不鉴权注解 | ||
11 | * | ||
12 | * @author 康伟 | ||
13 | */ | ||
14 | @Target({ ElementType.METHOD, ElementType.TYPE }) | ||
15 | @Retention(RetentionPolicy.RUNTIME) | ||
16 | @Documented | ||
17 | public @interface Anonymous | ||
18 | { | ||
19 | } |
... | @@ -2,6 +2,7 @@ package com.skua.modules.erp.mapper; | ... | @@ -2,6 +2,7 @@ package com.skua.modules.erp.mapper; |
2 | 2 | ||
3 | import java.util.List; | 3 | import java.util.List; |
4 | 4 | ||
5 | import com.skua.tool.annotation.Anonymous; | ||
5 | import org.apache.ibatis.annotations.Param; | 6 | import org.apache.ibatis.annotations.Param; |
6 | import com.skua.modules.erp.entity.PurchasePlanItem; | 7 | import com.skua.modules.erp.entity.PurchasePlanItem; |
7 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; | 8 | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
... | @@ -24,5 +25,6 @@ public interface PurchasePlanItemMapper extends BaseMapper<PurchasePlanItem> { | ... | @@ -24,5 +25,6 @@ public interface PurchasePlanItemMapper extends BaseMapper<PurchasePlanItem> { |
24 | * @param planId | 25 | * @param planId |
25 | * @return | 26 | * @return |
26 | */ | 27 | */ |
28 | @Anonymous | ||
27 | public List<PurchasePlanItem> queryListByPlanId(@Param("planId") String planId); | 29 | public List<PurchasePlanItem> queryListByPlanId(@Param("planId") String planId); |
28 | } | 30 | } | ... | ... |
... | @@ -16,6 +16,7 @@ import com.skua.core.context.BaseContextHandler; | ... | @@ -16,6 +16,7 @@ import com.skua.core.context.BaseContextHandler; |
16 | import com.skua.core.context.SpringContextUtils; | 16 | import com.skua.core.context.SpringContextUtils; |
17 | import com.skua.core.util.ConvertUtils; | 17 | import com.skua.core.util.ConvertUtils; |
18 | import com.skua.modules.system.service.impl.SysDepartServiceImpl; | 18 | import com.skua.modules.system.service.impl.SysDepartServiceImpl; |
19 | import com.skua.tool.annotation.Anonymous; | ||
19 | import lombok.extern.slf4j.Slf4j; | 20 | import lombok.extern.slf4j.Slf4j; |
20 | import org.apache.commons.lang3.StringUtils; | 21 | import org.apache.commons.lang3.StringUtils; |
21 | import org.apache.ibatis.binding.MapperMethod.ParamMap; | 22 | import org.apache.ibatis.binding.MapperMethod.ParamMap; |
... | @@ -30,6 +31,7 @@ import org.springframework.jdbc.core.JdbcTemplate; | ... | @@ -30,6 +31,7 @@ import org.springframework.jdbc.core.JdbcTemplate; |
30 | import org.springframework.stereotype.Component; | 31 | import org.springframework.stereotype.Component; |
31 | 32 | ||
32 | import java.lang.reflect.Field; | 33 | import java.lang.reflect.Field; |
34 | import java.lang.reflect.Method; | ||
33 | import java.util.*; | 35 | import java.util.*; |
34 | 36 | ||
35 | /** | 37 | /** |
... | @@ -100,9 +102,15 @@ public class MybatisInterceptor implements Interceptor { | ... | @@ -100,9 +102,15 @@ public class MybatisInterceptor implements Interceptor { |
100 | add("com.skua.modules.remotecontrol.mapper.SysMonitorLocationMapper.getAllData"); | 102 | add("com.skua.modules.remotecontrol.mapper.SysMonitorLocationMapper.getAllData"); |
101 | }}; | 103 | }}; |
102 | 104 | ||
105 | |||
106 | |||
107 | |||
103 | @Override | 108 | @Override |
104 | public Object intercept(Invocation invocation) throws Throwable { | 109 | public Object intercept(Invocation invocation) throws Throwable { |
105 | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; | 110 | MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; |
111 | |||
112 | // 检查方法或类上是否有自定义注解:匿名访问注解 | ||
113 | Anonymous anonymous = getAnnotationByAnonymous(mappedStatement); | ||
106 | String sqlId = mappedStatement.getId(); | 114 | String sqlId = mappedStatement.getId(); |
107 | log.debug("------sqlId------" + sqlId); | 115 | log.debug("------sqlId------" + sqlId); |
108 | SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); | 116 | SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); |
... | @@ -116,7 +124,10 @@ public class MybatisInterceptor implements Interceptor { | ... | @@ -116,7 +124,10 @@ public class MybatisInterceptor implements Interceptor { |
116 | if ("1".equals(BaseContextHandler.get("userType"))) { | 124 | if ("1".equals(BaseContextHandler.get("userType"))) { |
117 | return invocation.proceed(); | 125 | return invocation.proceed(); |
118 | } | 126 | } |
119 | System.out.println("********************************sqlId*************************** : "+sqlId); | 127 | //匿名访问注解,不涉及权限 |
128 | if(anonymous != null ){ | ||
129 | return invocation.proceed(); | ||
130 | } | ||
120 | //这些方法不涉及权限 | 131 | //这些方法不涉及权限 |
121 | if ("com.skua.modules.system.mapper.SysDepartMapper.getDepartTreeByJt".equalsIgnoreCase(sqlId) | 132 | if ("com.skua.modules.system.mapper.SysDepartMapper.getDepartTreeByJt".equalsIgnoreCase(sqlId) |
122 | || "com.skua.modules.system.mapper.SysDepartMapper.getDepartTree".equalsIgnoreCase(sqlId) | 133 | || "com.skua.modules.system.mapper.SysDepartMapper.getDepartTree".equalsIgnoreCase(sqlId) |
... | @@ -808,4 +819,26 @@ public class MybatisInterceptor implements Interceptor { | ... | @@ -808,4 +819,26 @@ public class MybatisInterceptor implements Interceptor { |
808 | } | 819 | } |
809 | } | 820 | } |
810 | 821 | ||
822 | |||
823 | |||
824 | |||
825 | /***获取匿名注解:Anonymous */ | ||
826 | public Anonymous getAnnotationByAnonymous(MappedStatement mappedStatement){ | ||
827 | Anonymous annotation = null; | ||
828 | try { | ||
829 | String id = mappedStatement.getId(); | ||
830 | String className = id.substring(0, id.lastIndexOf(".")); | ||
831 | String methodName = id.substring(id.lastIndexOf(".") + 1); | ||
832 | final Method[] method = Class.forName(className).getMethods(); | ||
833 | for (Method me : method) { | ||
834 | if (me.getName().equals(methodName) && me.isAnnotationPresent(Anonymous.class)) { | ||
835 | return me.getAnnotation(Anonymous.class); | ||
836 | } | ||
837 | } | ||
838 | } catch (Exception ex) { | ||
839 | log.error("", ex); | ||
840 | } | ||
841 | return annotation; | ||
842 | } | ||
843 | |||
811 | } | 844 | } | ... | ... |
-
请 注册 或 登录 后发表评论