shiro授权
2017-06-18 21:10
99 查看
今天我做了一下shiro的授权,之前是直接用rbac基于url拦截那一套做的权限管理。
当然,在shiro授权之前,你必须先进行用户信息的认证,就是在自定义realm里的doGetAuthenticationInfo()写的认证方法。(等一会写一下我对于shiro的理解)
现在我们开始学习如何进行shiro的认证。
实现shiro的认证有三种方式,网上有许多资料,有三种:编程式,配置文件中配置,注解声明。我们主要来探讨注解声明式的和配置文件式的,编程式认证就不赘述了,http://kdboy.iteye.com/blog/1155450里边说的很详细。
认证和授权的方法不一样,授权的方法是这个:doGetAuthorizationInfo(),千万别搞错。
1.通过配置文件配置
这个有时候会用到JSP标签,如下:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
我们需要在shiro的配置文件这样写:
在perms[]里填入访问此url需要的权限,若拥有权限即可访问,其实这是通过perms过滤器来实现的,有权限就放行。
当然,只要我们在doGetAuthorizationInfo()方法中赋予了用户相应的权限,perms过滤器就会放行。
这里说一下shiro过滤器对应的类,有兴趣的可以看一下源码:
2.注解
这个是最简单的,在控制层的方法上声明@RequiresPermissions("post:create")在控制器上即可。post:create是权限名称。
只要我们在doGetAuthorizationInfo()方法中赋予了用户相应的权限,就可以访问到这个控制器。
但是这里有个坑,由于注解授权是基于AOP动态代理的,因此我们要在springmvc的配置文件中配置动态代理,并且开启shiro的注解,如果没有配置shiro开启注解的话,注解是不会管用的,没有配置AOP代理的话,doGetAuthorizationInfo()也是不会执行的。
配置如下:
当然,在shiro授权之前,你必须先进行用户信息的认证,就是在自定义realm里的doGetAuthenticationInfo()写的认证方法。(等一会写一下我对于shiro的理解)
现在我们开始学习如何进行shiro的认证。
实现shiro的认证有三种方式,网上有许多资料,有三种:编程式,配置文件中配置,注解声明。我们主要来探讨注解声明式的和配置文件式的,编程式认证就不赘述了,http://kdboy.iteye.com/blog/1155450里边说的很详细。
认证和授权的方法不一样,授权的方法是这个:doGetAuthorizationInfo(),千万别搞错。
1.通过配置文件配置
这个有时候会用到JSP标签,如下:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
我们需要在shiro的配置文件这样写:
/post/** = perms[post]
在perms[]里填入访问此url需要的权限,若拥有权限即可访问,其实这是通过perms过滤器来实现的,有权限就放行。
当然,只要我们在doGetAuthorizationInfo()方法中赋予了用户相应的权限,perms过滤器就会放行。
这里说一下shiro过滤器对应的类,有兴趣的可以看一下源码:
过滤器简称 | 对应的java类 |
anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
port | org.apache.shiro.web.filter.authz.PortFilter |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
ssl | org.apache.shiro.web.filter.authz.SslFilter |
user | org.apache.shiro.web.filter.authc.UserFilter |
logout | org.apache.shiro.web.filter.authc.LogoutFilter |
这个是最简单的,在控制层的方法上声明@RequiresPermissions("post:create")在控制器上即可。post:create是权限名称。
只要我们在doGetAuthorizationInfo()方法中赋予了用户相应的权限,就可以访问到这个控制器。
但是这里有个坑,由于注解授权是基于AOP动态代理的,因此我们要在springmvc的配置文件中配置动态代理,并且开启shiro的注解,如果没有配置shiro开启注解的话,注解是不会管用的,没有配置AOP代理的话,doGetAuthorizationInfo()也是不会执行的。
配置如下:
<!-- 开启aop,对类代理 --> <aop:config proxy-target-class="true"></aop:config> <!-- 开启shiro注解支持 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean>
相关文章推荐
- Shiro 框架详解之授权
- 源码分析shiro认证授权流程
- 【Shiro权限管理】15.Shiro授权流程分析
- Shiro进认证不进授权的情况之一及解决方法
- shiro学习和使用实例(2)——登陆认证和授权
- shiro实现多个realm的认证和授权
- Shiro中的授权问题
- shiro(六)授权
- Apache Shiro 关于Shiro 授权
- Shiro【授权、整合Spirng、Shiro过滤器】
- 一头扎进Shiro-权限认证(授权)
- Shiro 学习笔记(3)—— 授权初步
- Shiro的注解(@RequiresRoles,@RequiresPermissions)授权不起作用
- shiro授权入门程序
- java鬼混笔记:shiro 4、shiro授权判断,基本ini配置
- web中采用shiro实现登录认证与权限授权管理
- Apache Shiro 标签模式授权
- spring+shiro授权简单实现
- Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制
- Apache Shiro 使用手册(三)Shiro 授权