【问题标题】:spring-security & spring-mvc configurationspring-security & spring-mvc 配置
【发布时间】:2011-01-08 01:25:59
【问题描述】:

我使用 spring-security 和 spring-mvc 以及基于注释的配置 (@Secured) 编写了一个小型 webapp。为了完成这项工作,我不得不拆分 spring-security 配置:

app-context.xml(包含在 web.xml 的 ContextConfigLocation 中)

<security:http auto-config="true"/>

app-servlet.xml(spring-mvc 的 dispatcherservlet 加载这个)

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

为什么我必须把这些分开?当我将所有安全配置放在 app-context.xml 中时,@Secured 注释似乎被忽略了,因此您无需登录即可访问 @Secured Controller 方法。

当我把它全部放在 app-servlet.xml 中时,会引发以下异常...

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:504)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:273)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008)
        at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:217)
        at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:145)
        at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:179)

我不明白:/

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    在后一种情况下,&lt;security:http /&gt; 元素可能不是定义的 xml 模式的一部分。因此,它可能会出现异常。对了,什么例外?

    在前一种情况下,它不起作用。可能是因为 Spring 在 DispatcherServlet 加载的 xml config 中寻找这个元素,否则忽略它。我也不确定,但似乎是这样。 :)

    看看这个spring forum thread。他们在讨论同样的问题。总而言之,“*-servlet.xml bean 在主上下文中不可见”

    【讨论】:

    • 我在我的问题中添加了例外
    【解决方案2】:

    &lt;security:http .../&gt; 必须在configContextLocation 的配置中声明。它不能在...-servlet.xml 中声明,因为在请求处理期间它应该在目标servlet 被识别之前可用。

    &lt;security:global-method-security .../&gt;(据我所知)注册了一个 bean 后处理器,它应用于声明它的上下文(即,当在 configContextLocation 的 xml 中声明时,它应用于那里声明的 bean,但不是...-servlet.xml中声明的bean)

    【讨论】:

      【解决方案3】:

      在处女座中,主要配置由org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext定义
      在 web.xml 中,所以我必须将安全配置导入 applicationContext.xml 而不是 *-servlet.xml
      &lt;import resource="applicationContext-security.xml"/&gt;
      修复了
      No bean named 'springSecurityFilterChain' is defined

      【讨论】:

        【解决方案4】:

        您应该能够在一个 XML 中同时拥有这两者,只需确保正确声明了架构。我在 Spring 2.5 和 Spring 3 的同一个文件中都使用了它们。

        当您说它在两个文件中都不起作用时,我假设您的意思是您的方法调用没有应用安全性? 如果是这种情况,请确保您的应用程序通过 spring 加载和使用这些类,否则将不应用任何安全性。例如。如果你访问一个类:

        MyClass instance = new MyClass();
        instance.doSomething();
        

        如果 doSomething() 使用安全注释进行注释,则不会对该注释做出任何反应。如果您从未通过 spring 等注入的 servlet 使用它也是如此。

        如果这没有帮助,你能澄清你的问题吗?

        【讨论】:

        • 没有注释的方法被spring-mvc通过@RequetMapping调用