Reputation: 820
I am doing a springboot project that includes login and accounts. I am trying to @Pointcut
all controller method calls and validate the login information, and @Before
the pointcut to make sure the session exists. Hence the code:
@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
}
}
However, this yields 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
.
Deleting the validateSession()
and @Before
makes the @Pointcut
work. How can I fix this?
Upvotes: 1
Views: 449
Reputation: 67437
The problem is that you define a JoinPoint
parameter in the pointcut. It only belongs in the advice method using the pointcut, not in the pointcut itself. You are not using it there anyway because a pointcut is never executed, the method is just a dummy to be decorated by the @Poinctut
annotation. So what you want is this:
@Pointcut("execution(* show.xianwu.game.frisbeescorer.controller.*.*(..))")
public void validateLogin() {
// check the login information
}
Besides (and unrelated to your problem), the .*.*
is quite specific and only matches a method in a class which is exactly in package show.xianwu.game.frisbeescorer.controller
. If you want to also include classes in subpackages, use the ..
syntax instead, in this case show.xianwu.game.frisbeescorer.controller..*
.
Upvotes: 2
Reputation: 469
Since you are working on a Springboot-based project, I recommend you use Spring Security features or other authorization & authentication frameworks such as Shiro.
If you wish to use none of them anyway, you can use @ModelAttributes
in a super class to invoke a method before any controller method is invoked.
@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.
}
}
Besides, to my experience, it is a bad practice to directly use @Pointcut
in a SpringBoot application. Use a customized Spring annotation instead.
Upvotes: 0