我有一个Spring MVC控制器,希望用Spring Method Security保护它。
在以下示例中可行 - @RequestMapping
和@PreAuthorize
注释相同的方法:
@Controller
public class MyController {
@RequestMapping(value = "/test", method = {RequestMethod.POST, RequestMethod.GET})
@PreAuthorize("isAuthenticated()")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
return test(request, response);
}
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
}
在此示例中不起作用 - @RequestMapping
和@PreAuthorize
注释不同的方法:
@Controller
public class MyController {
@RequestMapping(value = "/test", method = {RequestMethod.POST, RequestMethod.GET})
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
return test(request, response);
}
@PreAuthorize("isAuthenticated()")
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
...
}
这种奇怪行为可能是什么原因?
答案 0 :(得分:3)
在第二个示例中,test
方法直接从handleRequest
方法调用。 Spring没有机制拦截来自同一类的方法调用。因此,永远不会调用@PreAutorize
的Proxy / AOP方法初始化。