我正在做一个springboot项目,其中包括登录和帐户。我尝试对所有控制器方法调用执行@Pointcut
操作并验证登录信息,然后对切入点执行@Before
操作以确保会话存在。因此代码如下:
@Aspect
@Component
public class AuthAspect {
Logger logger = LoggerFactory.getLogger(AuthAspect.class);
@Pointcut("execution(* show.xianwu.game.frisbeescorer.controller.*.*(..))")
public void validateLogin(JoinPoint joinPoint) {
// check the login information
}
@Before("validateLogin()")
public void validateSession(JoinPoint joinPoint) {
// check the session
}
}
但是,这会产生org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'projectingArgumentResolverBeanPostProcessor' defined in class path resource [org/springframework/data/web/config/ProjectingArgumentResolverRegistrar.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
。
删除validateSession()
和@Before
可使@Pointcut
正常工作。我该如何解决这个问题呢?
发布于 2021-04-13 03:41:59
问题是您在切入点中定义了一个JoinPoint
参数。它只属于使用切入点的通知方法,而不属于切入点本身。您无论如何都不会在那里使用它,因为切入点永远不会被执行,该方法只是一个由@Poinctut
注释修饰的虚拟对象。所以你想要的是:
@Pointcut("execution(* show.xianwu.game.frisbeescorer.controller.*.*(..))")
public void validateLogin() {
// check the login information
}
除此之外(与您的问题无关),.*.*
是非常具体的,并且只与show.xianwu.game.frisbeescorer.controller
包中的类中的方法相匹配。如果还想在子包中包含类,请改用..
语法,在本例中为show.xianwu.game.frisbeescorer.controller..*
。
发布于 2021-04-12 02:37:55
由于您正在处理基于Springboot的项目,因此我建议您使用Spring Security特性或其他授权和身份验证框架,如Shiro。
如果您不想使用任何控制器方法,则可以在调用任何控制器方法之前,在超类中使用@ModelAttributes
来调用方法。
@Controller
public class ExampleController extends BaseController {...}
public class BaseController {
@ModelAttribute
public void invokeBefore(HttpServletRequest request,
HttpServletResponse response) {
// Check the auth info (e.g., Authorization header) of the request.
}
}
此外,根据我的经验,在SpringBoot应用程序中直接使用@Pointcut
是一种糟糕的做法。请改用customized Spring annotation。
https://stackoverflow.com/questions/67051840
复制相似问题