0

我正在尝试使用 Spring 安全性执行一些自定义身份验证。

我读过一些关于它的东西:

我还发现了一个关于这个 ClassNotFoundException DelegatingFilterProxy 的有趣对话:http: //forum.springsource.org/showthread.php? 53972-Problem-when-finding-springSecurityFilterChain-bean

所以,在我的 web.xml 我有

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 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_3_0.xsd">
  <display-name>Secured Account</display-name>

  <servlet>
    <servlet-name>mysecuredapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mysecuredapp</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mysecuredapp-security.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>

</web-app>

在 mysecuredapp-servlet.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" 
    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.xsd" >

    <http auto-config="true">
        <intercept-url pattern="/account/*" access="ROLE_USER, ROLE_ADMIN, ROLE_MASTER" />
        <intercept-url pattern="/admin/*" access="ROLE_ADMIN, ROLE_MASTER" />
        <intercept-url pattern="/master/*" access="ROLE_MASTER" />
    </http>


    <authentication-manager>
        <authentication-provider ref="daoAuthenticationProvider" />
    </authentication-manager>

    <beans:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider" >
        <beans:property name="userDetailsService" ref="userDetailsService" />
    </beans:bean>


</beans:beans>

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <groupId>MySecuredApp</groupId>
  <artifactId>securedApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>My Secured App - Spring Hibernate Maven</name>
  <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
        <javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
        <org.hibernate>3.6.10.Final</org.hibernate>
        <org.springsecurity.version>3.1.2.RELEASE</org.springsecurity.version>
    </properties>

  <dependencies>
        <!-- Hibernate deps -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>${org.hibernate}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${org.hibernate}</version>
        </dependency>

        <!-- Spring security deps -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springsecurity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springsecurity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springsecurity.version}</version>
        </dependency>
        <!-- Spring deps -->
        <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Misc deps -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${javax.servlet.jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

       <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>

       <dependency>
         <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version>
        <scope>test</scope>
       </dependency>

  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF8</encoding>
            </configuration>
            <inherited>true</inherited>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
        <testResource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
  </build>
</project>

当我启动 tomcat(在 Eclipse 中)时,我得到了这个 ClassNotFoundException DelegatingFilterProxy。知道为什么会这样吗?

提前致谢

4

2 回答 2

2

这是我自己犯的一个愚蠢的错误……因为我遇到问题的原因完全相同:不包括部署程序集的 Spring 依赖项。

有关更多详细信息,请参阅我之前发布的这个答案:Maven + Spring + Dynamic Web Module (Eclipse) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

于 2012-10-17T10:40:30.347 回答
1

过滤器链是否有可能没有选择您的用户详细信息服务 bean?您的 mysecuredapp-servlet.xml 文件中似乎没有组件扫描指令,并且委派过滤器的 contextConfigLocation 仅指向该文件。

我可能是错的,但我相当确定过滤器的 Spring 上下文与调度程序 servlet 的上下文是分开的。您需要在安全 XML 文件中包含组件扫描,或者将主 Spring beans 文件添加到 contextConfigLocation。

于 2012-10-15T16:18:22.733 回答