前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shiro教程7(整合SSM项目-授权)

Shiro教程7(整合SSM项目-授权)

作者头像
用户4919348
发布2019-04-02 11:41:27
7080
发布2019-04-02 11:41:27
举报
文章被收录于专栏:波波烤鸭波波烤鸭

授权原理分析

  首先授权必须是在认证通过之后才会执行的操作,之前我们在Shiro教程4(授权操作)该教程中讲过,获取权限我们是通过如下方法实现的

在这里插入图片描述
在这里插入图片描述

那么在自定义Realm中授权是怎么实现的呢? 我们跟踪代码来看 首先从 hasRole()方法来看。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  到此我们发现要对用户授权的话,我们只需要在定义Realm的doGetAuthorizationInfo方法中来授权。

授权具体实现

授权

代码语言:javascript
复制
/**
 * 授权方法
 */
@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注解

添加依赖

代码语言:javascript
复制
<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配置文件

代码语言:javascript
复制
<?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>

正常访问

在这里插入图片描述
在这里插入图片描述

登录成功后可以正常访问

在这里插入图片描述
在这里插入图片描述

注解权限验证

代码语言:javascript
复制
   @RequiresRoles("role2") // 验证角色
   //@RequiresPermissions("user:delete") // 验证权限
   @RequestMapping("/query")
   public String query(){
   	System.out.println("查询用户的方法");
   	return "/user.jsp";
   }

测试

在这里插入图片描述
在这里插入图片描述

权限验证生效。

指定没有权限访问的跳转地址

在这里插入图片描述
在这里插入图片描述

注意虽然配置了此设置,但是在使用注解的形式下,未授权的情况下还是不会跳转,此时我们需要在SpringMVC中添加对应的拦截器处理

代码语言:javascript
复制
<?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后被重定向到了此地址:

在这里插入图片描述
在这里插入图片描述

jsp标签验证

引入标签库

代码语言:javascript
复制
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>

标签验证

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
<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标签说明

shiro:authenticated

表示已认证通过

代码语言:javascript
复制
<shiro:authenticated>
    <label>用户身份验证已通过 </label>
</shiro:authenticated>

说明:只有已通过用户认证,但不是通过记住我(remember me)浏览才会看到标签内的内容

shiro:guest

代码语言:javascript
复制
<shiro:guest>
    <label>您当前是游客,</label><a href="/login.jsp" >请登录</a>
</shiro:guest>

说明:只有是没有登录过,以游客的身份浏览才会看到标签内的内容

shiro:hashRole

表示拥有某一角色

代码语言:javascript
复制
<shiro:hasRole name="admin">
    <label>这个用户拥有的角色是admin</label>
</shiro:hasRole>

说明:只有成功登录后,且具有admin角色的用户才可以看到标签内的容,name属性中只能填写一个角色的名称

shiro:hasAnyRoles

表示拥有多个角色中的一个即可

代码语言:javascript
复制
<shiro:hasAnyRoles name="admin,user">
    <label>这是拥有admin或者是user角色的用户</label>
</shiro:hasAnyRoles>

说明:只有成功登录后,且具有admin或者user角色的用户才会看到标签内的内容;name属性中可以填写多个角色名称,以逗号(,)分隔

shiro:hasPermission

表示拥有某一权限

代码语言:javascript
复制
<shiro:hasPermission name="admin:add">
    <label>这个用户拥有admin:add的权限</label>
</shiro:hasPermission>

说明:只有成功登录后,且具有admin:add权限的用户才可以看到标签内的内容,name属性中只能填写一个权限的名称

shiro:lacksRole

表示不拥有某一角色

代码语言:javascript
复制
<shiro:lacksRole name="admin">
    <label>这个用户不拥有admin的角色</label>
</shiro:lacksRole>

说明:只有成功登录后,且不具有admin角色的用户才可以看到标签内的内容,name属性中只能填写一个角色的名称

shiro:lacksPermission

表示不拥有某一权限

代码语言:javascript
复制
<shiro:lacksPermission name="admin:delete">
    <label>这个用户不拥有admin:delete的权限</label>
</shiro:lacksPermission>

说明:只有成功登录后,且不具有admin:delete权限的用户才可以看到标签内的内容,name属性中只能填写一个权限的名称

shiro:notAuthenticated

表示没有通过验证

代码语言:javascript
复制
<shiro:notAuthenticated>
    <label>用户身份验证没有通过(包括通过记住我(remember me)登录的) </label>
</shiro:notAuthenticated>

说明:只有没有通过验证的才可以看到标签内的内容,包括通过记住我(remember me)登录的

shiro:principal

表示用户的身份 取值取的是你登录的时候,在Realm 实现类中的new SimpleAuthenticationInfo(第一个参数,…) 放的第一个参数

在这里插入图片描述
在这里插入图片描述
  1. 如果第一个放的是username或者是一个值 ,那么就可以直接用。
代码语言:javascript
复制
    <!--取到username-->
    <shiro: principal/>
  1. 如果第一个参数放的是对象,比如放User 对象。那么如果要取其中某一个值,可以通过property属性来指定。
代码语言:javascript
复制
    <!--需要指定property-->
    <shiro:principal property="username"/>

shiro:user

表示已登录

代码语言:javascript
复制
<shiro:user>
    <label>欢迎[<shiro:principal/>],</label><a href="/logout.jsp">退出</a>
</shiro:user>
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 授权原理分析
  • 授权具体实现
    • 授权
      • 授权配置
        • 正常访问
          • 注解权限验证
            • 指定没有权限访问的跳转地址
              • jsp标签验证
                • 引入标签库
                • 标签验证
                • 测试
            • shiro标签说明
              • shiro:authenticated
                • shiro:guest
                  • shiro:hashRole
                    • shiro:hasAnyRoles
                      • shiro:hasPermission
                        • shiro:lacksRole
                          • shiro:lacksPermission
                            • shiro:notAuthenticated
                              • shiro:principal
                                • shiro:user
                                相关产品与服务
                                多因子身份认证
                                多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档