2016-11-28 107 views
2

我有一个Spring MVC控制器,并希望通过Spring Method Security来保护它。

在下面的例子它的工作原理 - @RequestMapping@PreAuthorize注释相同的方法:Spring Security:@PreAuthorize仅与@RequestMapping一起使用

@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 { 
     ... 
    } 


什么可能是这个奇怪的原因b ehaviour?

+1

http://stackoverflow.com/a/19421786/1291150 –

+0

谢谢你 - 这有助于! – olivmir

回答

2

在第二个示例中,test方法直接从handleRequest方法中调用。 Spring没有机制在同一个类中拦截方法调用。因此,@PreAutorize开始的代理/ AOP方法从不被调用。

More on the topic of Spring Proxy

+0

谢谢 - 伟大的答案,这有助于!我只想知道,为什么在描述'@ PreAuthorize'时,这在教程的任何地方都没有提及。 :|多好,在stackoverflow上有专家。 – olivmir

+2

@olivmir对Spring在大多数Spring文档中的工作方式有一定程度的假定知识。如果文档试图用这种假定的知识来描述一个特性,或者在Spring如何工作的情况下,那么这些文档将非常混乱和重复。 – MarkOfHall

相关问题