我用spring-security和spring-mvc写了一个小的webapp,带有一个基于注解的配置(@Secured)。为了完成这项工作,我必须拆分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方法。
当我将其全部放入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)我不明白:/
发布于 2010-01-22 12:10:52
在后一种情况下,<security:http />元素可能不是所定义的xml schema的一部分。因此,它很可能会出现异常。顺便问一下,有什么例外?
在前一种情况下,它不起作用。可能是因为Spring在DispatcherServlet加载的xml config中查找此元素,否则会忽略它。我也不确定,但看起来是这样的。:)
看看这个spring forum thread。他们正在讨论同样的问题。总而言之,是“从主上下文看不到*-servlet.xml bean”。
发布于 2010-01-22 18:51:44
<security:http .../>必须在configContextLocation的配置中声明。它不能在...-servlet.xml中声明,因为在请求处理期间,它应该在目标servlet被识别之前可用。
<security:global-method-security .../> (据我所知)注册了一个bean后处理器,它应用于声明它的上下文(即,当在configContextLocation的xml中声明时,它将应用于在那里声明的bean,但不会应用于在...-servlet.xml中声明的bean)。
发布于 2011-02-08 07:54:36
在处女座中,主要配置由org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext定义
所以我必须将安全配置导入到applicationContext.xml中,而不是*-servlet.xml
<import resource="applicationContext-security.xml"/>
这修复了
No bean named 'springSecurityFilterChain' is defined
https://stackoverflow.com/questions/2114814
复制相似问题