@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//必须强转为HandlerMethod
HandlerMethod handlerMethod = (HandlerMethod) handler;
//获取类上的注解
IgnoreAuth clazzAnnotation = handlerMethod.getBeanType().getAnnotation(IgnoreAuth.class);
//判断类上是否有打该注解
boolean clazzAnnotationPresent = handlerMethod.getBeanType().isAnnotationPresent(IgnoreAuth.class);
//获取方法上的注解 方式1
IgnoreAuth methodAnnotation_1 = handlerMethod.getMethodAnnotation(IgnoreAuth.class);
//获取方法上的注解 方式2
IgnoreAuth methodAnnotation_2 = handlerMethod.getMethod().getAnnotation(IgnoreAuth.class);
//判断方法上是否有打该注解
boolean methodsAnnotationPresent = handlerMethod.getMethod().isAnnotationPresent(IgnoreAuth.class);
}
拦截器中获取类注解和方法注解
最新推荐文章于 2024-04-10 01:07:00 发布
这篇博客探讨了在Java Web开发中如何使用注解进行类和方法级别的权限检查。通过`@Override`方法,博客作者展示了如何获取并检查`HandlerMethod`上的`IgnoreAuth`注解,以此决定是否需要进行身份验证。示例代码详细展示了如何从类和方法获取注解,为实现自定义的权限管理系统提供基础。
摘要由CSDN通过智能技术生成