目录贴: 跟我学Shiro目录贴
在一些场景中,比如某个领导因为一些原因不能进行登录网站进行一些操作,他想把他网站上的工作委托给他的秘书,但是他不想把帐号/密码告诉他秘书,只是想把工作委托给他;此时和我们可以使用Shiro的RunAs功能,即允许一个用户假装为另一个用户(如果他们允许)的身份进行访问。
本章代码基于《第十六章 综合实例》,请先了解相关数据模型及基本流程后再学习本章。
表及数据SQL
请运行shiro-example-chapter21/sql/ shiro-schema.sql 表结构
请运行shiro-example-chapter21/sql/ shiro-schema.sql 数据
实体
具体请参考com.github.zhangkaitao.shiro.chapter21包下的实体。
public class UserRunAs implements Serializable { private Long fromUserId;//授予身份帐号 private Long toUserId;//被授予身份帐号 }
该实体定义了授予身份帐号(A)与被授予身份帐号(B)的关系,意思是B帐号将可以假装为A帐号的身份进行访问。
DAO
具体请参考com.github.zhangkaitao.shiro.chapter21.dao包下的DAO接口及实现。
Service
具体请参考com.github.zhangkaitao.shiro.chapter21.service包下的Service接口及实现。
public interface UserRunAsService { public void grantRunAs(Long fromUserId, Long toUserId); public void revokeRunAs(Long fromUserId, Long toUserId); public boolean exists(Long fromUserId, Long toUserId); public List<Long> findFromUserIds(Long toUserId); public List<Long> findToUserIds(Long fromUserId); }
提供授予身份、回收身份、关系存在判断及查找API。
Web控制器RunAsController
该控制器完成:授予身份/回收身份/切换身份功能。
展示当前用户能切换到身份列表,及授予给其他人的身份列表:
@RequestMapping public String runasList(@CurrentUser User loginUser, Model model) { model.addAttribute("fromUserIds", userRunAsService.findFromUserIds(loginUser.getId())); model.addAttribute("toUserIds", userRunAsService.findToUserIds(loginUser.getId())); List<User> allUsers = userService.findAll(); allUsers.remove(loginUser); model.addAttribute("allUsers", allUsers); Subject subject = SecurityUtils.getSubject(); model.addAttribute("isRunas", subject.isRunAs()); if(subject.isRunAs()) { String previousUsername = (String)subject.getPreviousPrincipals().getPrimaryPrincipal(); model.addAttribute("previousUsername", previousUsername); } return "runas"; }
1、Subject.isRunAs():表示当前用户是否是RunAs用户,即已经切换身份了;
2、Subject.getPreviousPrincipals():得到切换身份之前的身份,一个用户可以切换很多次身份,之前的身份使用栈数据结构来存储;
授予身份
把当前用户身份授予给另一个用户,这样另一个用户可以切换身份到该用户。
@RequestMapping("/grant/{toUserId}") public String grant( @CurrentUser User loginUser, @PathVariable("toUserId") Long toUserId, RedirectAttributes redirectAttributes) { if(loginUser.getId().equals(toUserId)) { redirectAttributes.addFlashAttribute("msg", "自己不能切换到自己的身份"); return "redirect:/runas"; } userRunAsService.grantRunAs(loginUser.getId(), toUserId); redirectAttributes.addFlashAttribute("msg", "操作成功"); return "redirect:/runas"; }
1、自己不能授予身份给自己;
2、调用UserRunAsService. grantRunAs把当前登录用户的身份授予给相应的用户;
回收身份
把授予给某个用户的身份回收回来。
@RequestMapping("/revoke/{toUserId}") public String revoke( @CurrentUser User loginUser, @PathVariable("toUserId") Long toUserId, RedirectAttributes redirectAttributes) { userRunAsService.revokeRunAs(loginUser.getId(), toUserId); redirectAttributes.addFlashAttribute("msg", "操作成功"); return "redirect:/runas"; }
切换身份
@RequestMapping("/switchTo/{switchToUserId}") public String switchTo( @CurrentUser User loginUser, @PathVariable("switchToUserId") Long switchToUserId, RedirectAttributes redirectAttributes) { Subject subject = SecurityUtils.getSubject(); User switchToUser = userService.findOne(switchToUserId); if(loginUser.equals(switchToUser)) { redirectAttributes.addFlashAttribute("msg", "自己不能切换到自己的身份"); return "redirect:/runas"; } if(switchToUser == null || !userRunAsService.exists(switchToUserId, loginUser.getId())) { redirectAttributes.addFlashAttribute("msg", "对方没有授予您身份,不能切换"); return "redirect:/runas"; } subject.runAs(new SimplePrincipalCollection(switchToUser.getUsername(), "")); redirectAttributes.addFlashAttribute("msg", "操作成功"); redirectAttributes.addFlashAttribute("needRefresh", "true"); return "redirect:/runas"; }
1、首先根据switchToUserId查找到要切换到的身份;
2、然后通过UserRunAsService. exists()判断当前登录用户是否可以切换到该身份;
3、通过Subject.runAs()切换到该身份;
切换到上一个身份
@RequestMapping("/switchBack") public String switchBack(RedirectAttributes redirectAttributes) { Subject subject = SecurityUtils.getSubject(); if(subject.isRunAs()) { subject.releaseRunAs(); } redirectAttributes.addFlashAttribute("msg", "操作成功"); redirectAttributes.addFlashAttribute("needRefresh", "true"); return "redirect:/runas"; }
1、通过Subject.releaseRunAs()切换会上一个身份;
此处注意的是我们可以切换多次身份,如A切换到B,然后再切换到C;那么需要调用两次Subject. releaseRunAs()才能切换会A;即内部使用栈数据结构存储着切换过的用户;Subject. getPreviousPrincipals()得到上一次切换到的身份,比如当前是C;那么调用该API将得到B的身份。
其他代码和配置和《第十六章 综合实例》一样,请参考该章。
测试
1、首先访问http://localhost:8080/chapter21/,输入admin/123456进行登录;会看到如下界面:
2、点击切换身份按钮,跳到如下界面:
在该界面可以授权身份给其他人(点击授权身份可以把自己的身份授权给其他人/点击回收身份可以把之前授予的身份撤回)、或切换到其他身份(即假装为其他身份运行);
3、点击切换到该身份按钮,切换到相应的身份运行,如:
此时zhang用户切换到admin身份;如果点击切换回该身份,会把当前身份切换会zhang。
示例源代码:https://github.com/zhangkaitao/shiro-example;可加群 231889722 探讨Spring/Shiro技术。
相关推荐
第十七章 OAuth2集成——《跟我学Shiro》 - 开涛的博客 - IT...第1页 共13页本文把授权服务器和资源服务器整合在一起实现。依赖此处我们使用a
跟我学shiro学习资料。
跟我学Shiro
跟我学Shiro跟我学Shiro
Apache Shiro是一个强大易用的Java安全框架,...我找了一版 跟我学Shiro教程PDF,里面讲的很详细.里面还附带了每个章节的源码.值得你收藏哟!饮水思源——原文出自:http://jinnianshilongnian.iteye.com/blog/2049092
This compressed file is a detailed description of Shiro
包含以下内容的源码: 第二章 身份验证 第三章 授权 第四章 INI配置 ...第二十一章 授予身份及切换身份 第二十二章 集成验证码 第二十三章 多项目集中权限管理及分布式会话 第二十四章 在线会话管理
***txt文件中含有下载地址** 《跟我学Shiro》- 张开涛,PDF版本,带目录,清晰。 示例源代码:https://github.com/zhangkaitao/shiro-example; 加qun 231889722 探讨Spring/Shiro技术。
《跟我学Shiro》PDF完结版下载, Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Security做的功能强大,但是在实际工作时可能并不...
本资源包含: Apache_Shiro参考手册中文版.pdf; Shiro教程.pdf; shrio-example.rar; 跟我学Shiro教程.zip
跟我学Shiro源码
跟我一起学shiro张开涛,含PDF及源码,入门shiro的最好书籍。
张开涛
Apache Shiro 是 Java 的一个安全框架。目前,使用 Apache Shiro 的人越来越多,因为它相当简单,对比 Sp ring Security,可能没有 Spring Security 做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所 ...
Shiro是一个Java平台的开源权限框架,用于认证和访问授权
Apache Shiro 是一个框架,可用于身份验证和授权,是一个简单易学的框架,跟我学shiro详细讲解了shiro的开发,这里是该书的全部源代码
使用新版springboot改写跟我学shiro第16章源码。 springboot+shiro+jsp整合
这主要是跟我学shiro这一本书的pdf文档资源以及源码,希望对想学习shiro的用户能够带来帮助。