首先授权必须是在认证通过之后才会执行的操作,之前我们在Shiro教程4(授权操作)该教程中讲过,获取权限我们是通过如下方法实现的
那么在自定义Realm中授权是怎么实现的呢? 我们跟踪代码来看 首先从 hasRole()方法来看。
到此我们发现要对用户授权的话,我们只需要在定义Realm的doGetAuthorizationInfo方法中来授权。
/**
* 授权方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String userName = (String) principals.getPrimaryPrincipal();
System.out.println("登录的账号是:"+userName);
// ---- 根据账号去数据库中查询对应的角色和权限
// 此处我们将权限写死来模拟
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 固定设置一个角色
info.addRole("role1");
// 固定设置一个权限
info.addStringPermission("user:query");
return info;
}
在SpringMVC的配置文件中开启shiro注解
添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
springMVC配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.dpb.controller"/>
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 开启Shiro注解 -->
<aop:config proxy-target-class="true"></aop:config>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
</beans>
登录成功后可以正常访问
@RequiresRoles("role2") // 验证角色
//@RequiresPermissions("user:delete") // 验证权限
@RequestMapping("/query")
public String query(){
System.out.println("查询用户的方法");
return "/user.jsp";
}
测试
权限验证生效。
注意虽然配置了此设置,但是在使用注解的形式下,未授权的情况下还是不会跳转,此时我们需要在SpringMVC中添加对应的拦截器处理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.dpb.controller" />
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 开启Shiro注解 -->
<aop:config proxy-target-class="true"></aop:config>
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- shiro为集成springMvc 拦截异常 -->
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 这里你可以根据需要定义N多个错误异常转发 -->
<prop key="org.apache.shiro.authz.UnauthorizedException">redirect:/jsp/refuse.jsp</prop>
<prop key="org.apache.shiro.authz.UnauthenticatedException">redirect:/login.jsp</prop>
<!--<prop key="java.lang.IllegalArgumentException">redirect:/error.jsp</prop>
参数错误(bizError.jsp) -->
<!--<prop key="java.lang.Exception">redirect:/error.jsp</prop> 其他错误为'未定义错误'(unknowError.jsp) -->
</props>
</property>
</bean>
</beans>
再测试 当我们登录成功后访问http://localhost:8082/query后被重定向到了此地址:
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<h1>用户管理:</h1>
<shiro:hasRole name="role1">
<a href="#">添加用户1</a>
</shiro:hasRole>
<shiro:hasRole name="role2">
<a href="#">添加用户2</a>
</shiro:hasRole>
<shiro:hasPermission name="user:query">
<a href="#">查询用户</a>
</shiro:hasPermission>
<shiro:hasPermission name="user:delete">
<a href="#">删除用户</a>
</shiro:hasPermission>
用户具有的角色和权限
访问方法权限放开
登录访问:
表示已认证通过
<shiro:authenticated>
<label>用户身份验证已通过 </label>
</shiro:authenticated>
说明:只有已通过用户认证,但不是通过记住我(remember me)浏览才会看到标签内的内容
<shiro:guest>
<label>您当前是游客,</label><a href="/login.jsp" >请登录</a>
</shiro:guest>
说明:只有是没有登录过,以游客的身份浏览才会看到标签内的内容
表示拥有某一角色
<shiro:hasRole name="admin">
<label>这个用户拥有的角色是admin</label>
</shiro:hasRole>
说明:只有成功登录后,且具有admin角色的用户才可以看到标签内的容,name属性中只能填写一个角色的名称
表示拥有多个角色中的一个即可
<shiro:hasAnyRoles name="admin,user">
<label>这是拥有admin或者是user角色的用户</label>
</shiro:hasAnyRoles>
说明:只有成功登录后,且具有admin或者user角色的用户才会看到标签内的内容;name属性中可以填写多个角色名称,以逗号(,)分隔
表示拥有某一权限
<shiro:hasPermission name="admin:add">
<label>这个用户拥有admin:add的权限</label>
</shiro:hasPermission>
说明:只有成功登录后,且具有admin:add权限的用户才可以看到标签内的内容,name属性中只能填写一个权限的名称
表示不拥有某一角色
<shiro:lacksRole name="admin">
<label>这个用户不拥有admin的角色</label>
</shiro:lacksRole>
说明:只有成功登录后,且不具有admin角色的用户才可以看到标签内的内容,name属性中只能填写一个角色的名称
表示不拥有某一权限
<shiro:lacksPermission name="admin:delete">
<label>这个用户不拥有admin:delete的权限</label>
</shiro:lacksPermission>
说明:只有成功登录后,且不具有admin:delete权限的用户才可以看到标签内的内容,name属性中只能填写一个权限的名称
表示没有通过验证
<shiro:notAuthenticated>
<label>用户身份验证没有通过(包括通过记住我(remember me)登录的) </label>
</shiro:notAuthenticated>
说明:只有没有通过验证的才可以看到标签内的内容,包括通过记住我(remember me)登录的
表示用户的身份 取值取的是你登录的时候,在Realm 实现类中的new SimpleAuthenticationInfo(第一个参数,…) 放的第一个参数
<!--取到username-->
<shiro: principal/>
<!--需要指定property-->
<shiro:principal property="username"/>
表示已登录
<shiro:user>
<label>欢迎[<shiro:principal/>],</label><a href="/logout.jsp">退出</a>
</shiro:user>