I am encoding a website that will be almost completely protected by login (I use Spring Security for it). There are certain pages that are not protected, though (home page, login page, registration page, forgotten password page ...), and I'm trying to achieve:
- If the user does not register when accessing these unprotected pages, usually show them
- If the user has already registered, redirect (or on the page specified in the
redirectTo annotation element)
Of course, I want this not to be specified in every controller method:
if(loggedIn()) { // Redirect } else { // Return the view }
And for this reason, I would like to use AOP.
I created the @NonSecured annotation and I encoded the following aspect:
@Aspect public class LoggedInRedirectAspect { @Autowired private UserService userService; @Around("execution(@my.package.annotation.NonSecured * *(..))") public void redirect(ProceedingJoinPoint point) throws Throwable { System.out.println("Test"); point.proceed(); } }
An example of an annotated method:
@Controller @RequestMapping("/") public class HomeController { @NonSecured(redirectTo = "my-profile") @RequestMapping(method = RequestMethod.GET) public String index(Model model, HttpServletRequest request) throws Exception {
applicationContext.xml important bits:
<context:annotation-config /> <context:component-scan base-package="my.package" /> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <bean id="loggedInRedirectAspect" class="my.package.aspect.LoggedInRedirectAspect" /> <aop:aspectj-autoproxy proxy-target-class="true"> <aop:include name="loggedInRedirectAspect" /> </aop:aspectj-autoproxy>
The problem is that the redirect(...) method in the aspect is never called. Aspects generally work fine, in fact the following method will be called in the aspect: The next call is called, but not called for controller methods.
@Around("execution(* *(..))") public void redirect(ProceedingJoinPoint point) throws Throwable { point.proceed(); }
Am I doing something wrong in my pointcut?
Thanks.
Update: The last snippet in this question is called, but still not called for controller methods.