代码示例:身份切换

This commit is contained in:
click33 2022-10-17 02:33:33 +08:00
parent 3f437b89e9
commit 877e90d3b4
3 changed files with 71 additions and 0 deletions

View File

@ -18,6 +18,7 @@ public class SafeAuthController {
/*
* 前提首先调用登录接口进行登录代码在 com.pj.cases.use.LoginAuthController 中有详细解释此处不再赘述
* ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
*
* 测试步骤
1前端调用 deleteProject 接口尝试删除仓库 ---- http://localhost:8081/safe/deleteProject

View File

@ -0,0 +1,63 @@
package com.pj.cases.up;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
/**
* Sa-Token 身份切换
*
* @author kong
* @since 2022-10-17
*/
@RestController
@RequestMapping("/SwitchTo/")
public class SwitchToController {
/*
* 前提首先调用登录接口进行登录代码在 com.pj.cases.use.LoginAuthController 中有详细解释此处不再赘述
* ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
*/
// 身份切换 ---- http://localhost:8081/SwitchTo/switchTo?userId=10044
@RequestMapping("switchTo")
public SaResult switchTo(long userId) {
// 将当前会话 [身份临时切换] 为其它账号
// --- 切换的有效期为本次请求
StpUtil.switchTo(userId);
// 此时再调用此方法会返回 10044 (我们临时切换到的账号id)
System.out.println(StpUtil.getLoginId());;
// 结束 [身份临时切换]
StpUtil.endSwitch();
// 此时再打印账号就又回到了原来的值10001
System.out.println(StpUtil.getLoginId());
return SaResult.ok();
}
// lambda 表达式的方式身份切换 ---- http://localhost:8081/SwitchTo/switchTo2?userId=10044
@RequestMapping("switchTo2")
public SaResult switchTo2(long userId) {
// 输出 10001
System.out.println("------- [身份临时切换] 调用前当前登录账号id是" + StpUtil.getLoginId());
// lambda 表达式的方式身份切换作用范围只在这个 lambda 表达式内有效
StpUtil.switchTo(userId, () -> {
System.out.println("是否正在身份临时切换中: " + StpUtil.isSwitch()); // 输出 true
System.out.println("获取当前登录账号id: " + StpUtil.getLoginId()); // 输出 10044
});
// 结束后再次获取当前登录账号输出10001
System.out.println("------- [身份临时切换] 调用结束当前登录账号id是" + StpUtil.getLoginId());
return SaResult.ok();
}
}

View File

@ -57,3 +57,10 @@ System.out.println("------- [身份临时切换]调用结束...");
```
---
<a class="case-btn" href="https://gitee.com/dromara/sa-token/blob/master/sa-token-demo/sa-token-demo-case/src/main/java/com/pj/cases/up/SwitchToController.java"
target="_blank">
本章代码示例Sa-Token 身份切换 —— [ com.pj.cases.up.SwitchToController.java ]
</a>