【问题标题】:Spring Security: @PreAuthorize works only together with @RequestMappingSpring Security:@PreAuthorize 仅与 @RequestMapping 一起使用
【发布时间】:2017-04-12 08:27:23
【问题描述】:

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

在以下示例中它可以工作 - @RequestMapping@PreAuthorizeannotate 相同的方法:

@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@PreAuthorizeannotate不同的方法:

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


这种奇怪行为的原因可能是什么?

【问题讨论】:

标签: java spring spring-mvc spring-security spring-annotations


【解决方案1】:

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

More on the topic of Spring Proxy

【讨论】:

  • 谢谢你 - 很好的答案,有帮助!!!我只是想知道,为什么在教程中的任何地方都没有提到这一点,而在描述 @PreAuthorize 时。 :|太好了,有 stackoverflow 方面的专家。
  • @olivmir 在大多数 Spring 文档中,都有一定程度的关于 Spring 工作原理的假定知识。如果文档试图在没有这些假设知识的情况下描述一个特性,或者在 Spring 工作原理的上下文中,那么文档将非常混乱和重复。
猜你喜欢
  • 1970-01-01
  • 2015-12-09
  • 1970-01-01
  • 2021-06-28
  • 2016-02-05
  • 2020-10-15
  • 2013-10-13
  • 2020-07-25
  • 2021-09-03
相关资源
最近更新 更多