From b3d899e20458316d9ebe5ef07213cb5ddb124c36 Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Sun, 30 Jul 2023 22:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=8F=AF=E4=BB=A5=E5=8C=BF?= =?UTF-8?q?=E5=90=8D=E8=AE=BF=E9=97=AE=E7=9A=84=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lihongjie/coal/annotation/Anonymous.java | 19 ++++++++++++ .../cn/lihongjie/coal/aop/ControllerAop.java | 29 ++++++++++++++++--- .../java/cn/lihongjie/coal/common/Ctx.java | 5 ++++ .../coal/controller/LoginController.java | 6 +++- 4 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/main/java/cn/lihongjie/coal/annotation/Anonymous.java diff --git a/src/main/java/cn/lihongjie/coal/annotation/Anonymous.java b/src/main/java/cn/lihongjie/coal/annotation/Anonymous.java new file mode 100644 index 00000000..3f14f58b --- /dev/null +++ b/src/main/java/cn/lihongjie/coal/annotation/Anonymous.java @@ -0,0 +1,19 @@ +package cn.lihongjie.coal.annotation; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface Anonymous { + + boolean value() default true; + + + + + +} diff --git a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java index d5df0b2d..3dcc9c37 100644 --- a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java +++ b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java @@ -1,6 +1,8 @@ package cn.lihongjie.coal.aop; +import cn.lihongjie.coal.annotation.Anonymous; import cn.lihongjie.coal.annotation.SysLog; +import cn.lihongjie.coal.common.Ctx; import cn.lihongjie.coal.common.RequestUtils; import cn.lihongjie.coal.dto.R; import cn.lihongjie.coal.entity.SysLogEntity; @@ -30,7 +32,6 @@ import java.util.Arrays; public class ControllerAop { - @Pointcut("execution (* cn.lihongjie.coal.controller.*.*(..))") public void controllerMethods() { @@ -44,6 +45,29 @@ public class ControllerAop { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + // 处理接口权限 + if (!Ctx.isLoggedIn()) { + + + Anonymous anonymous = AnnotationUtils.findAnnotation(method, Anonymous.class); + + if (anonymous != null && !anonymous.value()) { + + + return R.fail("invalidToken", "登录状态失效,请重新登录"); + + + } + + Anonymous clsAnonymous = AnnotationUtils.findAnnotation(method.getClass(), Anonymous.class); + + if (clsAnonymous == null || !clsAnonymous.value()) { + return R.fail("invalidToken", "登录状态失效,请重新登录"); + } + + } + + long start = System.currentTimeMillis(); SysLogEntity sysLogEntity = createSysLog(method, request); try { @@ -54,7 +78,6 @@ public class ControllerAop { } catch (Throwable e) { - logException(e, proceedingJoinPoint); @@ -71,8 +94,6 @@ public class ControllerAop { } - - } finally { diff --git a/src/main/java/cn/lihongjie/coal/common/Ctx.java b/src/main/java/cn/lihongjie/coal/common/Ctx.java index 69bba7c3..0e0b0969 100644 --- a/src/main/java/cn/lihongjie/coal/common/Ctx.java +++ b/src/main/java/cn/lihongjie/coal/common/Ctx.java @@ -15,6 +15,11 @@ public class Ctx { } + public static boolean isLoggedIn(){ + + return getAuthentication() != null && getAuthentication().isAuthenticated(); + } + public static String getSessionId(){ return getAuthentication().getSessionId(); diff --git a/src/main/java/cn/lihongjie/coal/controller/LoginController.java b/src/main/java/cn/lihongjie/coal/controller/LoginController.java index 12e31b9a..5f5a1019 100644 --- a/src/main/java/cn/lihongjie/coal/controller/LoginController.java +++ b/src/main/java/cn/lihongjie/coal/controller/LoginController.java @@ -1,5 +1,6 @@ package cn.lihongjie.coal.controller; +import cn.lihongjie.coal.annotation.Anonymous; import cn.lihongjie.coal.annotation.SysLog; import cn.lihongjie.coal.common.Ctx; import cn.lihongjie.coal.dto.CaptchaDto; @@ -25,6 +26,7 @@ public class LoginController { @PostMapping("/login") @SysLog(msg = "登录") + @Anonymous public UserDto login(@RequestBody LoginDto dto) { this.service.login(dto); @@ -33,6 +35,7 @@ public class LoginController { } @PostMapping("/genCaptcha") + @Anonymous public CaptchaDto genCaptcha() { return this.service.genCaptcha(); @@ -47,9 +50,10 @@ public class LoginController { } @PostMapping("/isValid") + @Anonymous public Boolean isValid() { - return Ctx.getUserId() != null; + return Ctx.isLoggedIn(); }