跳到主要内容

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);