在我们的web开发过程中,经常需要用到功能权限校验,验证用户是否有某个角色或者权限,目前有很多框架,如Shiro

Shiro有基于自定义登录界面的版本,也有基于CAS登录的版本,目前我们的系统是基于CAS单点登录,各个公司的单点登录机制略有差异,和Shiro CAS的标准单点登录校验方式也自然略有不同。

在尝试将自定义登录的普通版Shiro改造失败后,在系统登录、校验角色、权限我认为相对简单后,觉得模仿Shiro自己实现一个权限校验小框架,说是框架,其实就是一个aop advisor,几个注解(Shiro不就是这个功能吗)

RequiresRoles和RequiresPermissions相似,不截图了

接下来是实现aop拦截功能了,先来说下注解的用法,和Shiro的注解一样,都是放在class或者method上

上述配置的意思是需要角色user和admin,需要权限home:*,上面仅是一个例子,实际业务中的角色已经确定有哪些权限了。

以下是aop拦截方式

/**
* 权限校验advisor,负责对RequiresRoles、RequiresPermissions标记的controller进行权限校验
* 他执行的时机是interceptor之后、方法执行之前
* Created by Administrator on 2015/2/9.
*/
@Aspect
@Component
public class AuthExecuteAdvisor implements PointcutAdvisor, Pointcut { private static final Logger logger = LoggerFactory.getLogger(AuthExecuteAdvisor.class); @Autowired
private UserService userService; /**
* 生成一个始终匹配class的Filter对象,任何类都可以通过这个过滤器
*/
private static final ClassFilter TrueClassFilter = new ClassFilter() {
@Override
public boolean matches(Class<?> clazz) {
return true;
}
}; /**
* 生成根据特定注解进行过滤的方法匹配器
* 如果class上有注解、或方法上有注解、或接口方法上有注解,都能通过
*/
private MethodMatcher annotationMethodMatcher = new StaticMethodMatcher() {
@Override
public boolean matches(Method method, Class<?> targetClass) { if (targetClass.isAnnotationPresent(RequiresRoles.class) || targetClass.isAnnotationPresent(RequiresPermissions.class)) {
return true;
} if (method.isAnnotationPresent(RequiresRoles.class) || method.isAnnotationPresent(RequiresPermissions.class)) {
return true;
}
// The method may be on an interface, so let's check on the target class as well.
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); return (specificMethod != method && (specificMethod.isAnnotationPresent(RequiresRoles.class)
|| specificMethod.isAnnotationPresent(RequiresPermissions.class)));
} }; @Override
public ClassFilter getClassFilter() {
//只执行注解的类
return TrueClassFilter;
} @Override
public MethodMatcher getMethodMatcher() {
return annotationMethodMatcher;
} /**
* 当前对象就是切面对象,根据classFilter、MethodFilter定义执行范围
*
* @return
*/
@Override
public Pointcut getPointcut() {
return this;
} /**
* 切面对应的处理策略
*
* @return
*/
@Override
public Advice getAdvice() {
//这个MethodInterceptor相当于MethodBeforeAdvice
return new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable { LoginContext loginContext = ActionContext.getLoginContext();
if (loginContext == null) {
throw new AuthFailException("校验用户权限失败,用户未登录");
} // TODO: 2017/11/21 管理员全权限
if (loginContext.getUserType().equals(0)) {
return invocation.proceed();
} Set<String> alreadyRoles = new HashSet<>(userService.getRolesByUserId(loginContext.getUserId()));
Set<String> alreadyPermissions = new HashSet<>(userService.getPermissionByUserId(loginContext.getUserId())); List<String> requireRole = getRequiredRole(invocation);
List<String> requirePermission = getRequiredPermission(invocation); if (!CollectionUtils.isEmpty(requireRole) && !alreadyRoles.containsAll(requireRole)) {
//role权限不足
logger.error("校验用户权限失败,role角色不足"); throw new AuthFailException("校验用户权限失败,role角色不足");
} if (!CollectionUtils.isEmpty(requirePermission) && !alreadyPermissions.containsAll(requirePermission)) {
//permission不足
logger.error("校验用户权限失败,permission权限不足");
throw new AuthFailException("校验用户权限失败,权限不足");
} return invocation.proceed();
} };
} /**
* 获取方法需要的角色
*
* @param invocation
* @return
*/
private List<String> getRequiredRole(MethodInvocation invocation) {
List<String> requiredRoles = new ArrayList<>();
Class<?> clazz = invocation.getThis().getClass(); if (clazz.isAnnotationPresent(RequiresRoles.class)) {
Collections.addAll(requiredRoles, clazz.getAnnotation(RequiresRoles.class).value());
} if (invocation.getMethod().isAnnotationPresent(RequiresRoles.class)) {
Collections.addAll(requiredRoles, invocation.getMethod().getAnnotation(RequiresRoles.class).value());
} return requiredRoles;
} /**
* 获取方法需要的权限
*
* @param invocation
* @return
*/
private List<String> getRequiredPermission(MethodInvocation invocation) {
List<String> requirePermission = new ArrayList<>();
Class<?> clazz = invocation.getThis().getClass(); if (clazz.isAnnotationPresent(RequiresPermissions.class)) {
Collections.addAll(requirePermission, clazz.getAnnotation(RequiresPermissions.class).value());
} if (invocation.getMethod().isAnnotationPresent(RequiresPermissions.class)) {
Collections.addAll(requirePermission, invocation.getMethod().getAnnotation(RequiresPermissions.class).value());
} return requirePermission;
} /**
* 每个切面的通知一个实例,或可分享的实例
* 此处使用分享的实例即可,分享的实例在内存中只会创建一个Advice对象
*
* @return
*/
@Override
public boolean isPerInstance() {
return false;
}
}

  

此处主要有以下几个关键点:
当前类既是一个切入点Pointcut(一群方法),也是一个通知持有者Advisor

Pointcut接口通过ClassFilter、MethodMatcher锁定哪些方法会用到Advisor

Advisor接口通过getAdvice获取Pointcut需要进行的拦截操作

 

ClassFilter是一个始终返回True的类过滤器,因为我们拦截执行的最小单位是method,所以即使class上没有注解,也要让他通过拦截
MethodMatcher则会判断method所在的class是否有注解,如果没有,则判断method是否有注解,二者满足其一就能通过校验 Advice的具体拦截过程为:
获取登录上下文
获取当前method需要的权限和角色(这里实际可以再优化,因为method可能只需要Role或Permission其中一种权限)
判断当前用户是否具有这些权限

  

 

开启spring的aop功能,权限拦截开始生效

 

基于Spring Aop实现类似shiro的简单权限校验功能的更多相关文章

  1. 基于spring的quartz定时框架,实现简单的定时任务功能

    在项目中,经常会用到定时任务,这就需要使用quartz框架去进行操作. 今天就把我最近做的个人主页项目里面的定时刷新功能分享一下,很简单. 首先需要配置一个配置文件,因为我是基于spring框架的,所 ...

  2. 基于spring security 实现前后端分离项目权限控制

    前后端分离的项目,前端有菜单(menu),后端有API(backendApi),一个menu对应的页面有N个API接口来支持,本文介绍如何基于spring security实现前后端的同步权限控制. ...

  3. 基于Spring Cloud、JWT 的微服务权限系统设计

    基于Spring Cloud.JWT 的微服务权限系统设计 https://gitee.com/log4j/pig https://github.com/kioyong/spring-cloud-de ...

  4. 基于Spring aop写的一个简单的耗时监控

    前言:毕业后应该有一两年没有好好的更新博客了,回头看看自己这一年,似乎少了太多的沉淀了.让自己做一个爱分享的人,好的知识点拿出来和大家一起分享,一起学习. 背景: 在做项目的时候,大家肯定都遇到对一些 ...

  5. spring aop 使用xml方式的简单总结

    spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...

  6. 基于Spring AOP的JDK动态代理和CGLIB代理

    一.AOP的概念  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...

  7. 基于Spring AOP实现的权限控制

    1.AOP简介 AOP,面向切面编程,往往被定义为促使软件系统实现关注点的分离的技术.系统是由许多不同的组件所组成的,每一个组件负责一块特定的功能.除了实现自身核心功能之外,这些组件还经常承担着额外的 ...

  8. s2sh框架搭建(基于spring aop)

    对于spring aop 是如何管理事务的,请看一下:http://bbs.csdn.net/topics/290021423 1.applicationContext.xml <?xml ve ...

  9. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

随机推荐

  1. [译]ASP.NET Core 2.0 网址重定向

    问题 如何在ASP.NET Core 2.0中实现网址重定向? 答案 新建一个空项目,在Startup.cs文件中,配置RewriteOptions参数并添加网址重定向中间件(UseRewriter) ...

  2. IDEA 护眼色设置 背景行颜色取消等设置

    首先做一些简答的记录,护眼色 等等的设置很久以前机器上已经设置过了,今天偶尔要在其他机器上重新做一些设置反而忘记了很多步骤, 设置后的HTML页面如何所示: 默认情况下,当只是设置General通用的 ...

  3. 微信小程序之bindtap事件绑定传参

    wxml代码: <view class='fen'> <text bindtap='prev' data-page="{{pageDang}}">上一页&l ...

  4. Angular服务的5种创建方式

    config配置块 Angular应用的运行主要分为两部分:app.config()和app.run(),config是你设置任何的provider的阶段,从而使应用可以使用正确的服务,需要注意的是在 ...

  5. HDU1150 Machine Schedule(二分图最大匹配、最小点覆盖)

    As we all know, machine scheduling is a very classical problem in computer science and has been stud ...

  6. Cable master

    Problem Description Inhabitants of the Wonderland have decided to hold a regional programming contes ...

  7. kafka单机模式部署安装,zookeeper启动

    在root的用户下 1):前提 安装JDK环境,设置JAVA环境变量 2):下载kafka,命令:wget  http://mirrors.shuosc.org/apache/kafka/0.10.2 ...

  8. 解决网络不可用--Using_Service_Workers

    Using_Service_Workers:https://developer.mozilla.org/zh-CN/docs/Web/API/Service_Worker_API/Using_Serv ...

  9. 认识Java WEB应用

    JavaWeb应用概念 在Sun的JavaServlet规范中,对Java Web应用作了这样定义:JAVA Web应用由一组Servlet.HTML页.类.以及其它可以被绑定的资源构造.它可以在各种 ...

  10. win10出现"本地计算机上的MySQL57服务启动后停止"

    在window10下mysql57出现"本地计算机上的MySQL57服务启动后停止.某些服务在未由其他服务或程序使用时将自动停止"错误 环境:win10.MySQL Communi ...