4

我已经阅读了大多数已经发布的类似问题,但仍然找不到解决方案。请注意,常规 CRUD 应用程序运行良好。当我尝试通过 spring-security 添加登录功能时出现此错误。

我的工作环境如下;

1.Tomcat 7.0.35

2.Spring 3.2.0

3.弹簧安全

spring-security-config-3.1.3.RELEASE.jar

spring-security-core-3.1.3.RELEASE.jar

spring-security-taglibs-3.1.3.RELEASE.jar

spring-security-web-3.1.3.RELEASE.jar

4.我的web.xml(/src/main/webapp/WEB-INF/web.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/root-context.xml</param-value>
</context-param>

<filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>


<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/conf/*-context.xml
        </param-value>
    </init-param>
    <!-- 
    <load-on-startup>1</load-on-startup>
     -->
</servlet>


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

5.我的security-context.xml (/src/main/resources/spring/conf/security-context.xml)

   <?xml version="1.0" encoding="UTF-8"?>

   <beans:beans xmlns="http://www.springframework.org/schema/security"
     xmlns:beans="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<http pattern="/resources" security="none" />

<http auto-config="true"  use-expressions="true">
    <intercept-url pattern="/login" access="permitAll"/>
    <intercept-url pattern="/logout" access="permitAll"/>
    <intercept-url pattern="/denied" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/user" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/>

    <form-login login-page="/login" 
        authentication-failure-url="/login/failure" 
        default-target-url="/"/>

    <access-denied-handler error-page="/denied"/>

    <logout invalidate-session="true" 
        logout-success-url="/logout/success" 
        logout-url="/logout"/>
</http>

<authentication-manager>
        <authentication-provider user-service-ref="customUserDetailsService">
                <password-encoder hash="md5"/>
        </authentication-provider>
</authentication-manager>

6.错误

INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Feb 10 22:17:40 IST 2013]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/root-context.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@58d51a54: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 353 ms
10-Feb-2013 22:17:40 org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter springSecurityFilterChain
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at  org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4656)
    at  org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5312)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at  org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

请帮我...谢谢

4

3 回答 3

6

您已经配置了两个contextConfigLocation元素。

您需要contextConfigLocation从元素中删除定义appServlet并添加classpath:spring/conf/security-context.xml到基本contextConfigLocation元素

于 2013-02-11T06:29:39.843 回答
0

我有同样的错误。

我将安全 bean (?) 的配置移到了 root-context.xml 之上:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="jimi" password="jimispassword"
                    authorities="ROLE_USER, ROLE_ADMIN" />
                <security:user name="bob" password="bobspassword"
                    authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

    <security:http auto-config='true'>
        <security:intercept-url pattern="/**" access="ROLE_USER" />
    </security:http>

--

现在它停止给予

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“springSecurityFilterChain”的bean

启动时出现异常。(尚未测试网页:))

于 2014-02-07T10:04:40.073 回答
0

提供最小的 spring-security 配置

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-安全-3.0.3.xsd ">

<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

于 2015-12-31T12:58:58.323 回答