您的位置:首页 > 其它

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的配置文件这样写:

/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
2.注解

这个是最简单的,在控制层的方法上声明@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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: