♻️ PigSecurityInnerAspect使用前置通知替代环绕通知

This commit is contained in:
knight 2023-07-26 11:03:37 +08:00
parent d0cddfd89c
commit e427126bcc

View File

@ -16,17 +16,15 @@
package com.pig4cloud.pig.common.security.component; package com.pig4cloud.pig.common.security.component;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.security.annotation.Inner; import com.pig4cloud.pig.common.security.annotation.Inner;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -45,8 +43,8 @@ public class PigSecurityInnerAspect implements Ordered {
private final HttpServletRequest request; private final HttpServletRequest request;
@SneakyThrows @SneakyThrows
@Around("@within(inner) || @annotation(inner)") @Before("@within(inner) || @annotation(inner)")
public Object around(ProceedingJoinPoint point, Inner inner) { public void around(JoinPoint point, Inner inner) {
// 实际注入的inner实体由表达式后一个注解决定即是方法上的@Inner注解实体若方法上无@Inner注解则获取类上的 // 实际注入的inner实体由表达式后一个注解决定即是方法上的@Inner注解实体若方法上无@Inner注解则获取类上的
// 这段代码没有意义拦截的就是@Inner注解怎么会为null呢 // 这段代码没有意义拦截的就是@Inner注解怎么会为null呢
// if (inner == null) { // if (inner == null) {
@ -58,7 +56,6 @@ public class PigSecurityInnerAspect implements Ordered {
log.warn("访问接口 {} 没有权限", point.getSignature().getName()); log.warn("访问接口 {} 没有权限", point.getSignature().getName());
throw new AccessDeniedException("Access is denied"); throw new AccessDeniedException("Access is denied");
} }
return point.proceed();
} }
@Override @Override