diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationController.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationController.java index cba7c689..95ddcc02 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationController.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationController.java @@ -3,7 +3,6 @@ package cn.dev33.satoken.integrate.annotation; 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; @@ -21,8 +20,6 @@ import cn.dev33.satoken.util.SaResult; @RequestMapping("/at/") public class SaAnnotationController { - // ------------------------ 简单测试 - // 登录 @RequestMapping("login") public SaResult login(long id) { @@ -58,16 +55,9 @@ public class SaAnnotationController { return SaResult.ok(); } - // Http Basic 认证 - @SaCheckBasic - @RequestMapping("checkBasic") - public SaResult checkBasic() { - return SaResult.ok(); - } - // 开启二级认证 @RequestMapping("openSafe") - public SaResult openSafe(long id) { + public SaResult openSafe() { StpUtil.openSafe(120); return SaResult.ok(); } @@ -79,5 +69,4 @@ public class SaAnnotationController { return SaResult.ok(); } - } diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationControllerTest.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationControllerTest.java index 4f3bc45a..967eba62 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationControllerTest.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationControllerTest.java @@ -62,6 +62,14 @@ public class SaAnnotationControllerTest { // 权限校验or角色校验,通过 SaResult res5 = request("/at/checkPermission2?satoken=" + satoken); Assertions.assertEquals(res5.getCode(), 200); + + // 开启二级认证 + SaResult res6 = request("/at/openSafe?satoken=" + satoken); + Assertions.assertEquals(res6.getCode(), 200); + + // 校验二级认证,通过 + SaResult res7 = request("/at/checkSafe?satoken=" + satoken); + Assertions.assertEquals(res7.getCode(), 200); } // 校验不通过的情况 @@ -87,8 +95,23 @@ public class SaAnnotationControllerTest { // 权限校验or角色校验,不通过 SaResult res5 = request("/at/checkPermission2?satoken=" + satoken); Assertions.assertEquals(res5.getCode(), 403); + + // 校验二级认证,不通过 + SaResult res7 = request("/at/checkSafe?satoken=" + satoken); + Assertions.assertEquals(res7.getCode(), 901); } + // 测试忽略认证 + @Test + public void testIgnore() { + // 必须登录才能访问的 + SaResult res1 = request("/ig/show1"); + Assertions.assertEquals(res1.getCode(), 401); + + // 不登录也可以访问的 + SaResult res2 = request("/ig/show2"); + Assertions.assertEquals(res2.getCode(), 200); + } // 封装请求 private SaResult request(String path) { @@ -114,5 +137,4 @@ public class SaAnnotationControllerTest { } } - } diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationIgnoreController.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationIgnoreController.java new file mode 100644 index 00000000..3f56393d --- /dev/null +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/annotation/SaAnnotationIgnoreController.java @@ -0,0 +1,34 @@ +package cn.dev33.satoken.integrate.annotation; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import cn.dev33.satoken.annotation.SaCheckLogin; +import cn.dev33.satoken.annotation.SaIgnore; +import cn.dev33.satoken.util.SaResult; + +/** + * 测试注解用的Controller + * + * @author kong + * @since: 2022-9-2 + */ +@SaCheckLogin +@RestController +@RequestMapping("/ig/") +public class SaAnnotationIgnoreController { + + // 需要登录后访问 + @RequestMapping("show1") + public SaResult show1() { + return SaResult.ok(); + } + + // 不登录也可访问 + @SaIgnore + @RequestMapping("show2") + public SaResult show2() { + return SaResult.ok(); + } + +} diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/configure/HandlerException.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/configure/HandlerException.java index 3dd03b62..1072ccb3 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/configure/HandlerException.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/integrate/configure/HandlerException.java @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotPermissionException; import cn.dev33.satoken.exception.NotRoleException; +import cn.dev33.satoken.exception.NotSafeException; import cn.dev33.satoken.util.SaResult; /** @@ -33,5 +34,11 @@ public class HandlerException { public SaResult handlerNotPermissionException(NotPermissionException e) { return SaResult.error().setCode(403); } + + // 二级认证失败,code=901 + @ExceptionHandler(NotSafeException.class) + public SaResult handlerNotSafeException(NotSafeException e) { + return SaResult.error().setCode(901); + } }