From d029a28488afcd23946c7e0f98101f55d8ad3b74 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Sat, 11 Sep 2021 00:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B3=A8=E8=A7=A3=E9=89=B4?= =?UTF-8?q?=E6=9D=83demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/pj/test/AtController.java | 80 +++++++++++++++++++ .../java/com/pj/test/LoginController.java | 42 ++++++++++ .../main/java/com/pj/test/UserController.java | 34 -------- sa-token-doc/index.html | 4 + 4 files changed, 126 insertions(+), 34 deletions(-) create mode 100644 sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/AtController.java create mode 100644 sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/LoginController.java delete mode 100644 sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/UserController.java diff --git a/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/AtController.java b/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/AtController.java new file mode 100644 index 00000000..dc7c6f95 --- /dev/null +++ b/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/AtController.java @@ -0,0 +1,80 @@ +package com.pj.test; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import cn.dev33.satoken.annotation.SaCheckBasic; +import cn.dev33.satoken.annotation.SaCheckLogin; +import cn.dev33.satoken.annotation.SaCheckPermission; +import cn.dev33.satoken.annotation.SaCheckRole; +import cn.dev33.satoken.annotation.SaCheckSafe; +import cn.dev33.satoken.annotation.SaMode; +import cn.dev33.satoken.stp.StpUtil; +import cn.dev33.satoken.util.SaResult; + +/** + * 注解鉴权测试 + * @author kong + * + */ +@RestController +@RequestMapping("/at/") +public class AtController { + + // 登录认证,登录之后才可以进入方法 ---- http://localhost:8081/at/checkLogin + @SaCheckLogin + @RequestMapping("checkLogin") + public SaResult checkLogin() { + return SaResult.ok(); + } + + // 权限认证,具备user-add权限才可以进入方法 ---- http://localhost:8081/at/checkPermission + @SaCheckPermission("user-add") + @RequestMapping("checkPermission") + public SaResult checkPermission() { + return SaResult.ok(); + } + + // 权限认证,同时具备所有权限才可以进入 ---- http://localhost:8081/at/checkPermissionAnd + @SaCheckPermission({"user-add", "user-delete", "user-update"}) + @RequestMapping("checkPermissionAnd") + public SaResult checkPermissionAnd() { + return SaResult.ok(); + } + + // 权限认证,只要具备其中一个就可以进入 ---- http://localhost:8081/at/checkPermissionOr + @SaCheckPermission(value = {"user-add", "user-delete", "user-update"}, mode = SaMode.OR) + @RequestMapping("checkPermissionOr") + public SaResult checkPermissionOr() { + return SaResult.ok(); + } + + // 角色认证,只有具备admin角色才可以进入 ---- http://localhost:8081/at/checkRole + @SaCheckRole("admin") + @RequestMapping("checkRole") + public SaResult checkRole() { + return SaResult.ok(); + } + + // 完成二级认证 ---- http://localhost:8081/at/openSafe + @RequestMapping("openSafe") + public SaResult openSafe() { + StpUtil.openSafe(200); // 打开二级认证,有效期为200秒 + return SaResult.ok(); + } + + // 通过二级认证后才可以进入 ---- http://localhost:8081/at/checkSafe + @SaCheckSafe + @RequestMapping("checkSafe") + public SaResult checkSafe() { + return SaResult.ok(); + } + + // 通过Basic认证后才可以进入 ---- http://localhost:8081/at/checkBasic + @SaCheckBasic(account = "sa:123456") + @RequestMapping("checkBasic") + public SaResult checkBasic() { + return SaResult.ok(); + } + +} diff --git a/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/LoginController.java b/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/LoginController.java new file mode 100644 index 00000000..4a072ad7 --- /dev/null +++ b/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/LoginController.java @@ -0,0 +1,42 @@ +package com.pj.test; + +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; + +/** + * 登录测试 + * @author kong + * + */ +@RestController +@RequestMapping("/acc/") +public class LoginController { + + // 测试登录 ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456 + @RequestMapping("doLogin") + public SaResult doLogin(String name, String pwd) { + // 此处仅作模拟示例,真实项目需要从数据库中查询数据进行比对 + if("zhang".equals(name) && "123456".equals(pwd)) { + StpUtil.login(10001); + return SaResult.ok("登录成功"); + } + return SaResult.error("登录失败"); + } + + // 测试注销 ---- http://localhost:8081/acc/logout + @RequestMapping("logout") + public SaResult logout() { + StpUtil.logout(); + return SaResult.ok(); + } + + // 查询登录状态 ---- http://localhost:8081/acc/isLogin + @RequestMapping("isLogin") + public SaResult isLogin(String username, String password) { + return SaResult.ok("是否登录:" + StpUtil.isLogin()); + } + +} diff --git a/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/UserController.java b/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/UserController.java deleted file mode 100644 index 7006ed29..00000000 --- a/sa-token-demo/sa-token-demo-springboot/src/main/java/com/pj/test/UserController.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.pj.test; - -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import cn.dev33.satoken.stp.StpUtil; - -/** - * 登录测试 - * @author kong - * - */ -@RestController -@RequestMapping("/user/") -public class UserController { - - // 测试登录,浏览器访问: http://localhost:8081/user/doLogin?username=zhang&password=123456 - @RequestMapping("doLogin") - public String doLogin(String username, String password) { - // 此处仅作模拟示例,真实项目需要从数据库中查询数据进行比对 - if("zhang".equals(username) && "123456".equals(password)) { - StpUtil.login(10001); - return "登录成功"; - } - return "登录失败"; - } - - // 查询登录状态,浏览器访问: http://localhost:8081/user/isLogin - @RequestMapping("isLogin") - public String isLogin(String username, String password) { - return "当前会话是否登录:" + StpUtil.isLogin(); - } - -} diff --git a/sa-token-doc/index.html b/sa-token-doc/index.html index 12e5fa4d..91beda0b 100644 --- a/sa-token-doc/index.html +++ b/sa-token-doc/index.html @@ -248,6 +248,10 @@ + + + +