办法一
通过request取得用户的URI,再逐个循环判断是否能够操作。只是这种办法很让人好受。
办法二
通过用户要拜访的办法来判断是否有权限:
preHandle办法中handler理论为HandlerMethod,(看网上说的有时候不是HandlerMethod),加个instanceof验证吧
能够失去办法名:h.getMethod().getName()
能够失去RequestMapping注解中的值:h.getMethodAnnotation(RequestMapping.class)
这种办法还是不太不便
举荐一个 Spring Boot 基础教程及实战示例:
https://www.javastack.cn/cate...
办法三
自定义注解,自定义注解代码:
@Retention(RUNTIME)@Target(METHOD)public @interface MyOperation { String value() default "";//默认为空,因为名字是value,实际操作中能够不写"value="}
Controller代码:
@Controller("testController")public class TestController { @MyOperation("用户批改")//次要看这里 @RequestMapping("test") @ResponseBody public String test(String id) { return "Hello,2018!"+id; }}
拦截器的代码:
@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("进入拦截器"); if(handler instanceof HandlerMethod) { HandlerMethod h = (HandlerMethod)handler; System.out.println("用户想执行的操作是:"+h.getMethodAnnotation(MyOperation.class).value()); //判断后执行操作... } return HandlerInterceptor.super.preHandle(request, response, handler);}
在每个办法下面加注解太麻烦啦,能够在类上加注解
@Retention(RUNTIME)@Target(TYPE)public @interface MyOperation { String value() default "";}//拦截器中这样取得h.getMethod().getDeclaringClass().getAnnotation(MyOperation.class);
我能够获取requestMapping,不必创立自定义注解啊,值得注意的是,不要应用GetMapping等,要应用requestMapping。
原文链接:https://blog.csdn.net/howroad...
版权申明:本文为CSDN博主「howroad」的原创文章,遵循CC 4.0 BY-SA版权协定,转载请附上原文出处链接及本申明。
近期热文举荐:
1.1,000+ 道 Java面试题及答案整顿(2021最新版)
2.别在再满屏的 if/ else 了,试试策略模式,真香!!
3.卧槽!Java 中的 xx ≠ null 是什么新语法?
4.Spring Boot 2.5 重磅公布,光明模式太炸了!
5.《Java开发手册(嵩山版)》最新公布,速速下载!
感觉不错,别忘了顺手点赞+转发哦!