需求描述:

对指定资源(方法或者类)进行权限控制。

解决方案

在需要权限控制的资源,加上权限控制注解

软件版本

  • JAVA JDK 1.8
  • Spring 4.3.6.RELEASE

实现细节

  1. 注解接口
package com.xmasq.framework.core.annotation.permission;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.springframework.stereotype.Component;/*** 权限控制* * @author 艾思祺-非渝* @version 1.0.0*/
@Component
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface Permission {/** 进行权限过滤的操作类型 用户所拥有的权限集仅需要1项权限符合 */public final static String OPER_OR = "OR";/** 进行权限过滤的操作类型 用户所拥有的权限集需要覆盖全部 */public final static String OPER_AND = "AND";/*** 需求的权限的操作模式,默认为OR*/public String oper() default OPER_OR;/** 需要的权限 */String[] value() default {};}

2.对注解的实现进行一层封装,方便其它注解实现

package com.xmasq.framework.core.annotation;import java.lang.annotation.Annotation;import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;/*** 注解处理器的简单封装,可以简单获取指定注解* * @author 艾思祺-非渝* @version 1.0.0*/
public abstract class BaseAnnotationHandler<T extends Annotation> extends HandlerInterceptorAdapter {/*** 获取指定注解* * @param handler* @return*/public T getAnnotation(Object handler, Class<T> annotationClass) {if (handler instanceof HandlerMethod) {T annotation = null;HandlerMethod handlerMethod = (HandlerMethod) handler;annotation = handlerMethod.getMethodAnnotation(annotationClass);if (annotation == null) {annotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), annotationClass);}return annotation;} else {return AnnotationUtils.findAnnotation(ClassUtils.getUserClass(handler), annotationClass);}}}

3.Permission注解的具体实现

package com.xmasq.framework.core.annotation.permission;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;import com.xmasq.framework.core.annotation.BaseAnnotationHandler;
import com.xmasq.framework.core.exception.BusinessException;
import com.xmasq.framework.module.user.UserContext;
import com.xmasq.framework.module.user.UserContextHelper;/*** Promission注解的处理* * @see com.xmasq.framework.core.annotation.permission.Permission* * @author 艾思祺-非渝* @version 1.0.0*/
@Component
public class PermissionHandler extends BaseAnnotationHandler<Permission> {public static final String BEAN_NAME = "promissionInterceptor";@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {Permission promission = getAnnotation(handler, Permission.class);// 具体实现细节}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {}}

4.使用方式

@Permission(value = {"p_menu_view"})
@RestController
@RequestMapping(value = "/manage/menu")
public class MenuRestController extends BaseRestController {@Autowiredprivate MenuService menuService;@Permission(value = {"p_menu_edit"})@PostMapping(value = "/save")public AjaxResponse save(@RequestBody Menu menu) {menuService.saveMenu(menu);return AjaxResponse.success();}
}

PS:关于获取注解,如果运用在类上,必须要要对注解加上spring的@Component注解,这样才能通过AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), annotationClass);获取到指定注解

基于Spring拦截器实现拦截器注解相关推荐

  1. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  2. Spring Boot实战:拦截器与过滤器

    一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...

  3. spring 两次进入拦截器_过滤器和拦截器的 6 个区别,别再傻傻分不清了

    点击上方 肉眼品世界,选择 设为星标 深度价值体系传递 作者 :程序员内点事 来源 :toutiao.com/i6834310440495874563 毕竟这两种工具开发中用到的频率都相当高,应用起来 ...

  4. Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用

    转载自 https://www.cnblogs.com/EasonJim/p/7704740.html 一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filt ...

  5. spring过滤器Filter 、 拦截器Interceptor 、 切片Aspect 详解

    springboot 过滤器Filter vs 拦截器Interceptor vs 切片Aspect 详解 1 前言 最近接触到了过滤器和拦截器,网上查了查资料,这里记录一下,这篇文章就来仔细剖析下过 ...

  6. spring 两次进入拦截器_Spring Boot+Redis 扛住,瞬间千次重复提交(实例)

    前言: 在实际的开发项目中,一个对外暴露的接口往往会面临,瞬间大量的重复的请求提交,如果想过滤掉重复请求造成对业务的伤害,那就需要实现幂等! 我们来解释一下幂等的概念: 任意多次执行所产生的影响均与一 ...

  7. Spring AOP原理及拦截器

    原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...

  8. Spring Boot 实现登录拦截器,这才是正确的姿势!!

    原文:https://blog.csdn.net/qq_27198345/article/details/111401610 对于管理系统或其他需要用户登录的系统,登录验证都是必不可少的环节,在Spr ...

  9. spring mvc中的拦截器

    本文说下spring MVC中的拦截器 文章目录 拦截器介绍 拦截器注入适配器 自定义拦截器 controller测试 测试结果 本文小结 拦截器介绍 拦截器是在servlet执行之前执行的程序(这里 ...

  10. spring MVC - Inteceptors(拦截器)

    Spring的Interceptor(拦截器)是Controller 方法级别的 当某一个Controller的方法进行request处理的时候 可以通过Interceptor进行拦截, 拦截器可以在 ...

最新文章

  1. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)
  2. 自由移动的气泡_STARCCM+标准算例展示之——曳力作用下的气泡上升速度
  3. DM365 dvsdk_2_10_01_18开发环境搭建
  4. 【译】Diving Into The Ethereum VM Part 3 — The Hidden Costs of Arrays
  5. STM32 C/C++ uCOSII 函数调用return 无法返回或者函数无法正常反回上一层函数的问题
  6. 【Protocol Buffer】Protocol Buffer入门教程(七):导入定义
  7. hadoop调优之一
  8. python2.7 tab,自动补全
  9. 线上环境 Linux 系统调用追踪
  10. C++中for循环的5种语法
  11. linux 文件上传扫描_SecureCRT实现windows与linux文件上传下载
  12. Windows 2012部署Exchange2013
  13. 哎呀,人家不小心变油腻了呢
  14. 【模版】求单个/多个欧拉函数值
  15. 桂林理工大学 2020秋程序设计实践课程 课程设计与实习报告
  16. 2019-9-9,中兴笔试
  17. 《Python数据科学手册》—学习笔记
  18. EndNote20 快捷键
  19. 关于计算机专业励志的话,励志语录经典短句100句
  20. 【SE】异常、枚举类

热门文章

  1. 怎样查看Linux服务器配置
  2. 文献引用 .bib文件里有公式符号
  3. WPS表格COUNTIF函数用法实例教程
  4. 语音交互程序:语音识别、语音输出、录音计算
  5. wallpaper-engine 操作热键
  6. 如何用快捷键打开windows下的画图工具
  7. 推荐几个不错的 CSS 工具,持续收录!
  8. C# async / await 用法
  9. ESP32 partition(分区表)(15)
  10. 5g聚合路由器的那些事儿,你都知道多少?