spring-mvc - SpringSecurity 多个命名空间和安全注解。大困惑

标签 spring-mvc namespaces annotations spring-security

我正在制作一个带有一些 RESTfull 资源作为 API 的 Spring MVC web 应用程序。

我需要 RESTfull 部分具有一些自定义过滤器,因为我不想要任何重定向,并且我希望使用相应的 HTTP 错误代码和基本的 JSON 描述来转换任何异常。

另一方面,网站的其余部分必须更常见,并在人们未登录时重定向等。

还有一件事,我希望在某些情况下使用 @Secured 注释和后验证。

如何正确定义多个 http 命名空间(在 Spring 3.1 上)?

这是我的错误配置:

    <global-method-security secured-annotations="enabled" />

<http pattern="/rest/**" authentication-manager-ref="authenticationManager" entry-point-ref="restAuthenticationEntryPoint">
    <form-login login-page="/rest/login" login-processing-url="/rest/postlogin"
        authentication-success-handler-ref="restAuthenticationSuccessHandler"
        authentication-failure-handler-ref="restAuthenticationFailureHandler"
        username-parameter="username" password-parameter="password" />
    <logout logout-url="/rest/logout" invalidate-session="true" />
</http>

<http pattern="/**" authentication-manager-ref="authenticationManager">
    <form-login login-page="/login" login-processing-url="/postlogin"
        username-parameter="username" password-parameter="password" />
    <logout />
</http> 

有趣的是,此配置部分起作用,因为我可以使用/rest/login 登录并从自定义成功处理程序获得响应。我也可以从/login 登录,然后正确重定向到/。注销也工作正常。

接下来,所有 Controller bean 在安全方法中都有 @Secured("ROLE_USER") 。但是所有安全的方法都不会得到保护。 为什么呢?
    @Secured({"ROLE_USER"})
@RequestMapping(method = RequestMethod.GET, headers = { "Range" })
public @ResponseBody
HttpEntity<List<T>> list(@RequestHeader("Range") String range) {

我到处阅读文档,但比以往任何时候都更加困惑。
  • 为什么我的方法没有得到保护?
  • http 命名空间是否必须定义访问权限才能使 @Secured 注释起作用?
  • http 命名空间是否覆盖了我的 @Secured 注释?如果是这样,我如何使用自定义过滤器定义多个“登录页面”并能够使用注释?

  • 以下是一些事实:
    * 我使用的是 Spring 和 SpringSecurity 3.1
    * 我有一个自定义的 AuthenticationManager 来从 hibernate daos 中检索用户详细信息。
    * 一些 Controller 正在扩展@Secured 注释所在的抽象类。但它仍然不适用于简单的 Controller 。
    * 我的 Controller 是通过 context:component-scan 和 base-package 发现的。
    * 使用一个 http 命名空间时安全性工作正常。

    请帮忙,我对此很生气!

    最佳答案

    退房 this answer关于确保 Web 上下文对 global-method-security 可见声明并可能使用类代理。

    要回答您的其他问题,没有 http命名空间不应影响 @Secured 的使用注释,除了用户通过应用程序的 Web 部分进行身份验证以及该信息将由方法安全拦截器在做出访问决策时使用。除非您覆盖它(使用 access-decision-manager-ref ),方法安全性将使用标准 AccessDecisionManager它根据用户拥有的角色授予或拒绝访问权限。

    关于spring-mvc - SpringSecurity 多个命名空间和安全注解。大困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790957/

    相关文章:

    javascript - 如何创建子行来显示 jquery 数据表中的订单详细信息?

    java - 不满意的依赖异常 : Error creating bean with name 'accountController'

    java - Spring @PropertySource,如何读取不在类路径上,而是在WEB-INF下的文件

    java - 如何使用java注释设置类属性的默认值

    swift - 如何连接两个注释点?

    java - Java 中的页面导航/工作流框架

    XML 在命名空间内查找命名空间

    c++ - 在模板参数的函数中使用一个或另一个命名空间

    f# - 在 F# 中自动打开命名空间

    android - 在 kotlin 中定义 StringDef