繁体   English   中英

spring-security和spring-mvc配置

[英]spring-security & spring-mvc configuration

我已经使用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)

我不明白:/

在后一种情况下, <security:http />元素可能不是所定义的xml模式的一部分。 因此,它可能会出现异常。 顺便说一句,什么例外?

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

看看这个春季论坛的话题 他们正在讨论相同的问题。 概括起来, “ * -servlet.xml bean在主上下文中不可见”

<security:http .../>必须在configContextLocation的配置中声明。 它无法在...-servlet.xml声明,因为在请求处理期间,它应该在标识目标servlet之前可用。

<security:global-method-security .../> (据我所知)注册了一个bean后处理器,该后处理器应用于声明它的上下文(即,在configContextLocation的xml中声明时,它应用于在此声明的bean,但不在...-servlet.xml声明的bean)

在处女座中,主要配置由org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext定义
在web.xml中,因此我必须将安全配置导入到applicationContext.xml中,而不是* -servlet.xml中。
<import resource="applicationContext-security.xml"/>
固定了
No bean named 'springSecurityFilterChain' is defined

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

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

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

如果doSomething()带有安全注释,则对该注释没有任何反应。 如果您不是通过spring等方式从servlet中使用它,也是如此。

如果这没有帮助,您能否澄清问题?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM