Spring-Security 3 / Spring MVC和令人恐惧的@ Secured / RequestMapping
问题内容:
我在向控制器添加安全注释时遇到很多问题。
事实证明,让我的控制器实现InitializingBean是一个坏主意。
public class MyController implements InitializingBean {
@Secured(value="ROLE_ADMIN")
@RequestMapping(method = RequestMethod.GET, value = "/{id}/edit")
public String getView(Model model, @PathVariable("id") long id) {
return "some view";
}
}
这失败了:
WARN PageNotFound:962-未找到具有URI的HTTP请求的映射[…]
删除@Secured
Annotation可以工作,但是显然我不想这样做。在网上浪费了很多时间之后,我注意到工作的和不工作的控制器之间的最后一个区别是它实现了InitializingBean接口。现在,这就像一个魅力:
public class MyController{
@Secured(value="ROLE_ADMIN")
@RequestMapping(method = RequestMethod.GET, value = "/{id}/edit")
public String getView(Model model, @PathVariable("id") long id) {
return "some view";
}
}
谁能帮助我了解这种行为?
问题答案:
发生这种情况的原因是,当使用JDK动态代理应用安全方面时,对注释的访问会丢失,这在默认情况下会在建议bean实现任何接口时发生。
为了解决这个问题,您应该告诉Spring Security使用<global-method-security proxy-target-class = "true" ...> ...
(仅适用于)基于目标类的代理<aop:config proxy-target-class = "true" />
。
更多关于AOP代理这里。