【问题标题】:spring not enforcing method security annotationsspring 不强制执行方法安全注释
【发布时间】:2010-10-05 18:36:36
【问题描述】:

对于为什么 spring 没有在我的服务接口上强制执行 @Secured("ROLE_USER") ,我有些迷茫。我的控制器是使用注释建立的。

我的服务接口示例

public interface MyServiceManager {

    @Secured("ROLE_USER")
    public void delete(int cid);

    @RolesAllowed({"ROLE_USER"})
    public Contact getContact(int contactId);
}

我的安全上下文:

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

<http auto-config="true" >
    <intercept-url pattern="/secure/**" access="ROLE_SUPERVISOR" />
    <intercept-url pattern="/addcontact**" access="IS_AUTHENTICATED_REMEMBERED" />
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />

    <concurrent-session-control max-sessions="1"
        exception-if-maximum-exceeded="true"/>
    <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/>
    <logout logout-success-url="/welcome.do" logout-url="/logout"/>
</http>
    <authentication-provider>
    <password-encoder hash="md5"/>
    <user-service>
        <user name="rod" password="a564de63c2d0da68cf47586ee05984d7" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
    </user-service>
</authentication-provider>

【问题讨论】:

    标签: java annotations spring-security


    【解决方案1】:

    我遇到了同样的问题。 我添加后:

    <context:annotation-config />
    

    在我的 spring-security.xml 文件中它消失了。

    希望这会对某人有所帮助:)

    【讨论】:

      【解决方案2】:

      就我而言,这句话的确切位置:

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

      被证明是非常重要的。确保将它放在声明应扫描并用作控制器的类之后。

      <context:component-scan base-package="com.test.controller" />
      

      这是确保@Secured 注解也能进入游戏的方法

      【讨论】:

      • 在组件扫描之后并不重要,它必须在同一个文件中!
      【解决方案3】:

      你有没有在你的 web.xml 中使用过类似的东西

      <servlet>
          <servlet-name>name</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      

      我不知道为什么,但如果我使用 DispatcherServlet,我无法强制执行安全注释

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。使用 Kent Lai 在这里回复的信息,我能够修复它。

        我将&lt;global-method-security&gt; 元素放在我的app-servlet.xml 中,但在security.xml 中将安全定义分开,其中web.xml 具有contextConfigLocation 用于app-servlet.xmlsecurity.xml

        现在就像一个魅力!

        【讨论】:

        • 按照我的理解,将 app-servlet.xml 文件定义为 contextConfigLocation 的一部分有点多余。如果您使用 DispatcherServlet,它将加载继承 WebApplicationContext 的 servletname-servlet.xml。在这种情况下,在我看来,您的 bean 将首先由您的 ContextLoader 在您的 WebApplicationContext 中创建一次,然后再次由您的 DispatcherServlet 用相同的定义覆盖。
        • 哇,我得检查一下。谢谢!
        • @Sam 和@Joe 实际上这不是多余的,因为它们是分开的。如果您放置 -servlet.xml 路由,将创建一个子应用程序上下文,其父应用程序上下文是 WebApplicationContext。 @Joe 可能让它工作,因为他没有子上下文(app-servlet 和安全性在同一个上下文中)。
        【解决方案5】:

        在对这个问题进行了更多研究之后,我得出了以下结论/解决方案。我不确定它是否 100% 正确..但它有效。

        我将所有配置都放在了 dispatcher-servlet.xml 文件中。因此,不要使用 disptacher-servlet.xml 和 application-context.xml。 dispatcher-servlet.xml 由应用程序加载(contextConfigLocation)。在 dispatcher-servlet.xml 中,我导入了我的 security-context.xml 和 datasource-context.xml。之后,一切正常。

        【讨论】:

          【解决方案6】:

          尝试将注释放在实现类而不是接口上,看看是否可行。我最终在最近的一个项目中这样做了,因为我也在我的服务层上使用了 @Transactional 属性,并且 Spring 文档建议将它们放在类上而不是接口上。我不知道同样的问题是否适用于@Secured,但我想将注释保留在同一个地方。见Spring Docs

          关于 Kent Lai 的回答...这是个好主意...确保您的安全配置文件实际上已被 Spring 包含。

          【讨论】:

            【解决方案7】:

            你有声明吗

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

            在与您定义 MyServiceManager bean 相同的配置文件中?在我打开 org.springframework 的调试之前,我遇到了同样的问题,并注意到 spring security 仅应用于与定义 global-method-security 的文件相同的文件。

            【讨论】:

            • 我正在将 security-context.xml 和 datasource-context.xml 导入到 applicationContext.xml 中。向上移动 似乎修复了 url 路径安全角色问题。但不是方法安全问题。
            • 是的,重要的是你在与组件相同的 Spring 上下文中拥有 global-method-security。
            猜你喜欢
            • 1970-01-01
            • 2012-01-01
            • 1970-01-01
            • 2015-09-20
            • 1970-01-01
            • 2012-01-04
            • 1970-01-01
            • 2016-04-26
            • 1970-01-01
            相关资源
            最近更新 更多