Spring未执行方面
问题内容:
我正在编写一个几乎可以通过登录完全保护的网站(我使用的是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(...)
永远不会被调用。 一般而言,各方面都可以正常工作,实际上该方面中的以下方法将被调用:
对于控制器方法,以下建议被调用但未被调用。
@Around("execution(* *(..))")
public void redirect(ProceedingJoinPoint point) throws Throwable
{
point.proceed();
}
我的切入点做错什么了吗?
谢谢。
更新: 此问题中的最后一个代码段被调用,但对于控制器方法仍然没有被调用。
问题答案:
@satoshi,我认为您遇到的问题是因为您使用的是Spring-AOP,它只能为具有接口的bean创建AOP代理-在您的情况下,控制器没有接口。
解决方法可能是使用AspectJ使用编译时/加载时编织,而不使用Spring AOP OR在类路径中使用cglib jar并强制基于cglib的代理创建:
<aop:aspectj-autoproxy proxy-target-class="true"/>
更新:可以使用maven插件来完成编译时编织,showWeaveInfo配置将准确显示已编织的类:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.10</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>