From 524a9fe9da56f7fc487802c156377d650e8c149a Mon Sep 17 00:00:00 2001 From: lbw Date: Wed, 26 Jul 2023 16:07:20 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20SecurityInner?= =?UTF-8?q?Aspect=E4=BD=BF=E7=94=A8=E5=89=8D=E7=BD=AE=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=E7=8E=AF=E7=BB=95=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/PigxSecurityInnerAspect.java | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxSecurityInnerAspect.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxSecurityInnerAspect.java index 841b0f617..bbf0062f1 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxSecurityInnerAspect.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxSecurityInnerAspect.java @@ -19,18 +19,16 @@ package com.pig4cloud.pigx.common.security.component; import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.security.annotation.Inner; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; import org.springframework.core.Ordered; -import org.springframework.core.annotation.AnnotationUtils; import org.springframework.security.access.AccessDeniedException; -import jakarta.servlet.http.HttpServletRequest; - /** * @author lengleng * @date 2022-06-04 @@ -45,19 +43,13 @@ public class PigxSecurityInnerAspect implements Ordered { private final HttpServletRequest request; @SneakyThrows - @Around("@within(inner) || @annotation(inner)") - public Object around(ProceedingJoinPoint point, Inner inner) { - // 实际注入的inner实体由表达式后一个注解决定,即是方法上的@Inner注解实体,若方法上无@Inner注解,则获取类上的 - if (inner == null) { - Class clazz = point.getTarget().getClass(); - inner = AnnotationUtils.findAnnotation(clazz, Inner.class); - } + @Before("@within(inner) || @annotation(inner)") + public void around(JoinPoint point, Inner inner) { String header = request.getHeader(SecurityConstants.FROM); if (inner.value() && !StrUtil.equals(SecurityConstants.FROM_IN, header)) { log.warn("访问接口 {} 没有权限", point.getSignature().getName()); throw new AccessDeniedException("Access is denied"); } - return point.proceed(); } @Override