java - spring安全配置不起作用

标签 java xml spring spring-security

我试图将 Spring Security 添加到我的应用程序中。下面是我的 web.xml 和 sprin-security.xml 文件。当我运行该应用程序时,出现以下错误。您能让我知道我的代码有什么问题吗?这很可能与我的 web.xml 有关

  java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:252)

web.xml

     <web-app 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"
version="2.5">

<display-name>UniteID Rest WebService</display-name>

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

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<!-- Spring Security -->
<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>



</web-app>

Spring-security.xml

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

<!-- <http auto-config="true"> <intercept-url pattern="/welcome*" access="ROLE_USER" 
    /> <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" 
    /> <logout logout-success-url="/logout" /> </http> -->

<http>
    <intercept-url pattern="/welcome*" access="ROLE_USER" />
    <http-basic />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="mkyong" password="123456" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

</beans:beans>

spring-config.xml

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

<context:component-scan base-package="com.uniteid.controller" />
<mvc:annotation-driven />


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>mymessages</value>
        </list>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@nyvm0467.ptc.un.org:1521:EIDMSUAT" />
    <property name="username" value="DBO_EIDMSUAT" />
    <property name="password" value="NewPassDBO_EIDMSUAT" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.uniteid.model.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.connection.pool_size">10</prop>    
        </props>
    </property>
</bean>



<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="persistenceExceptionTranslationPostProcessor"
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="dataDao" class="com.uniteid.dao.DataDaoImpl"></bean>
    <bean id="dataServices" class="com.uniteid.services.DataServicesImpl">         </bean>
  </beans>

最佳答案

您的配置缺少 ContextLoaderListener。

将其添加到您的 web.xml:

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

(请更仔细地阅读异常输出。)

关于java - spring安全配置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661377/

相关文章:

xml - 使用XSLT遍历XML标签

java - 是否有通过 Java 从 XML 模式生成 JSON 模式的工具?

java - Spring + Hibernate - 持久/提交数据不起作用

java - 数据库驱动消息 bean 缺少默认 Spring 消息

java - Spring Boot中如何解决 "Exception evaluating SpringEL expression"?

java - NullPointerException 在 org.springframework.roo.project.packaging.WarPackaging.createOtherArtifacts(WarPackaging.java :41)

java - 没有 USB 设备识别 libusbJava/ubuntu 13.04

java - 读取复杂的 XML Java

java - 如何使用 2 个 JPanel 对象对齐 JButton 以形成基本的 java GUI

java - 将按钮中的数字插入到 EditText 中选定的位置