mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 01:58:05 +08:00
二级认证新增指定业务标识功能
This commit is contained in:
@@ -23,7 +23,7 @@ public class SafeAuthController {
|
||||
* 测试步骤:
|
||||
1、前端调用 deleteProject 接口,尝试删除仓库。 ---- http://localhost:8081/safe/deleteProject
|
||||
2、后端校验会话尚未完成二级认证,返回: 仓库删除失败,请完成二级认证后再次访问接口。
|
||||
3、前端将信息提示给用户,用户输入密码,调用 openSafe 接口。 ---- http://localhost:8081/safe/openSafe
|
||||
3、前端将信息提示给用户,用户输入密码,调用 openSafe 接口。 ---- http://localhost:8081/safe/openSafe?password=123456
|
||||
4、后端比对用户输入的密码,完成二级认证,有效期为:120秒。
|
||||
5、前端在 120 秒内再次调用 deleteProject 接口,尝试删除仓库。 ---- http://localhost:8081/safe/deleteProject
|
||||
6、后端校验会话已完成二级认证,返回:仓库删除成功。
|
||||
@@ -46,13 +46,13 @@ public class SafeAuthController {
|
||||
return SaResult.ok("仓库删除成功");
|
||||
}
|
||||
|
||||
// 提供密码进行二级认证 ---- http://localhost:8081/safe/openSafe
|
||||
// 提供密码进行二级认证 ---- http://localhost:8081/safe/openSafe?password=123456
|
||||
@RequestMapping("openSafe")
|
||||
public SaResult openSafe(String password) {
|
||||
// 比对密码(此处只是举例,真实项目时可拿其它参数进行校验)
|
||||
if("123456".equals(password)) {
|
||||
|
||||
// 比对成功,为当前会话打开二级认证,有效期为120秒
|
||||
// 比对成功,为当前会话打开二级认证,有效期为120秒,意为在120秒内再调用 deleteProject 接口都无需提供密码
|
||||
StpUtil.openSafe(120);
|
||||
return SaResult.ok("二级认证成功");
|
||||
}
|
||||
@@ -61,4 +61,40 @@ public class SafeAuthController {
|
||||
return SaResult.error("二级认证失败");
|
||||
}
|
||||
|
||||
|
||||
// ------------------ 指定业务类型进行二级认证
|
||||
|
||||
// 获取应用秘钥 ---- http://localhost:8081/safe/getClientSecret
|
||||
@RequestMapping("getClientSecret")
|
||||
public SaResult getClientSecret() {
|
||||
// 第1步,先检查当前会话是否已完成 client业务 的二级认证
|
||||
StpUtil.checkSafe("client");
|
||||
|
||||
// 第2步,如果已完成二级认证,则返回数据
|
||||
return SaResult.data("aaaa-bbbb-cccc-dddd-eeee");
|
||||
}
|
||||
|
||||
// 提供手势密码进行二级认证 ---- http://localhost:8081/safe/openClientSafe?gesture=35789
|
||||
@RequestMapping("openClientSafe")
|
||||
public SaResult openClientSafe(String gesture) {
|
||||
// 比对手势密码(此处只是举例,真实项目时可拿其它参数进行校验)
|
||||
if("35789".equals(gesture)) {
|
||||
|
||||
// 比对成功,为当前会话打开二级认证:
|
||||
// 业务类型为:client
|
||||
// 有效期为600秒==10分钟,意为在10分钟内,调用 getClientSecret 时都无需再提供手势密码
|
||||
StpUtil.openSafe("client", 600);
|
||||
return SaResult.ok("二级认证成功");
|
||||
}
|
||||
|
||||
// 如果密码校验失败,则二级认证也会失败
|
||||
return SaResult.error("二级认证失败");
|
||||
}
|
||||
|
||||
// 查询当前会话是否已完成指定的二级认证 ---- http://localhost:8081/safe/isClientSafe
|
||||
@RequestMapping("isClientSafe")
|
||||
public SaResult isClientSafe() {
|
||||
return SaResult.ok("当前是否已完成 client 二级认证:" + StpUtil.isSafe("client"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -46,7 +46,7 @@ public class GlobalException {
|
||||
@ExceptionHandler(NotSafeException.class)
|
||||
public SaResult handlerException(NotSafeException e) {
|
||||
e.printStackTrace();
|
||||
return SaResult.error("二级认证校验失败");
|
||||
return SaResult.error("二级认证校验失败:" + e.getService());
|
||||
}
|
||||
|
||||
// 拦截:服务封禁异常
|
||||
|
Reference in New Issue
Block a user