天天看点

Shiro(二)—— spring环境下shiro权限控制

首先可以先了解一下我的上一篇博客

Shiro(一)—— spring环境下shiro认证过程

里面搭建了spring环境下shiro整合以及登录认证流程,这篇shiro权限控制是在上一篇的配置基础上

  1. 使用shiro提供的注解形式,进行请求的权限控制

    spring-shiro中添加aop配置,开启注解支持

    <!--配置授权注解-->
        <!--配置为true表示即使用cglib代理的方式-->
        <aop:config proxy-target-class="true"/>
        <!--配置第三方扫描shiro注解-->
        <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
            <property name="securityManager" ref="securityManager"/>
        </bean>
               
  2. 在请求的Controller方法上,添加注解

    @RequiresPermissions("create")

    :表示该请求必须要求当前用户有“create”权限

    @RequiresRoles("hr")

    :表示该请求必须要求当前用户有“hr”角色

    这两个是常用的控制权限的注解

    @ResponseBody
        @RequestMapping("/saveEmployee")
        @RequiresPermissions("create")
        public ResultMessage saveEmployee(Employee employee){
        	......
        }
               
  3. 在自定义realm中的

    doGetAuthorizationInfo()

    方法查询数据库当前用户的所有权限,如果不是空,返回

    SimpleAuthorizationInfo

    ,会自动判断有没有相关权限
    /**
         * @function: 授权
         * 数据库查询当前认证的用户的权限
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    
            LOG.warn("参数对象是:" + principalCollection.toString());
    
            Employee employee = (Employee) principalCollection.getPrimaryPrincipal();
    
            //数据库查询当前用户的角色
            List<String> roles = employeeService.getEmployeeRoles(employee.getId());
    
            if (roles == null || roles.size() == 0) {
                return null;
            }
    
            LOG.info("当前用户的角色为");
            roles.stream().forEach(System.out::println);
    
            //数据库查询当前用户的权限
            List<String> premissions=employeeService.getEmployeePremission(roles);
    
            if (premissions == null || premissions.size() == 0) {
                return null;
            }
    
            LOG.info("当前用户的权限为");
            premissions.stream().forEach(System.out::println);
    
            /**
             * @function: 权限信息
             */
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.addRoles(roles);
            info.addStringPermissions(premissions);
    
            return info;
        }
               
  4. 可以使用SpringMVC的方法,捕获一下出现的

    AuthorizationException

    (没有权限异常),返回给前端相应信息
    /**
         * @function: 没有权限异常处理
         */
        @ExceptionHandler(AuthorizationException.class)
        @ResponseBody
        public ResultMessage ShiroException(){
            ResultMessage<String> message = new ResultMessage<>();
            message.setResultCode(200);
            message.setResultMes("你没有权限访问进行该操作");
            message.setResultData(null);
            return message;
        }
               
  1. 最后使用Shiro进行权限控制除了注解式还有其他方法,比如在spring-shiro.xml中配置的shiro过滤器中的过滤链对相应请求加上指定过滤器
    /saveEmployee = perms["create"]
               
    /saveEmployee = roles["hr"]
               
    还有shiro标签形式等,没有尝试过,就不介绍了

这就是我使用shiro进行注解式权限控制的流程及代码,如果有问题谢谢指正,要不点个赞吧,哈哈

继续阅读