【发布时间】:2012-02-16 12:01:00
【问题描述】:
我正在编写一个几乎完全受登录保护的网站(我正在使用 Spring Security)。但是,某些页面不受保护(主页、登录页面、注册页面、忘记密码页面……),而我想要实现的是:
- 如果用户在访问这些非安全页面时没有登录, 正常显示
- 如果用户已经登录,重定向到
主页(或到
redirectTo注释元素中指定的页面)
当然我想避免把它放在每个控制器方法中:
if(loggedIn())
{
// Redirect
}
else
{
// Return the view
}
出于这个原因,我想使用 AOP。
我创建了注解@NonSecured,并编写了以下方面:
@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();
}
}
示例注释方法:
@Controller
@RequestMapping("/")
public class HomeController
{
@NonSecured(redirectTo = "my-profile")
@RequestMapping(method = RequestMethod.GET)
public String index(Model model,
HttpServletRequest request) throws Exception
{
// Show home page
}
}
applicationContext.xml 重要位:
<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>
问题是方面中的方法redirect(...) 永远不会被调用。 Aspects 工作正常,事实上,Aspect 中的以下方法将被调用: 以下建议被调用,但不会被控制器方法调用。
@Around("execution(* *(..))")
public void redirect(ProceedingJoinPoint point) throws Throwable
{
point.proceed();
}
我的切入点做错了吗?
谢谢。
更新:这个问题中的最后一个 sn-p 被调用,但仍然没有被控制器方法调用。
【问题讨论】:
标签: spring spring-mvc spring-aop