Spring-security - 无法访问 ServletException

Posted

技术标签:

【中文标题】Spring-security - 无法访问 ServletException【英文标题】:Spring-security - cannot access ServletException 【发布时间】:2014-10-02 22:26:35 【问题描述】:

我喜欢使用 spring-security 模块,但是当我按照 spring 项目中的教程 (http://docs.spring.io/spring-security/site/docs/3.2.x/guides/helloworld.html) 进行操作时,编译器会返回:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] \springframework\security\samples\config\SecurityWebApplicationInitializer.java:[5,7] error: cannot access ServletException
[ERROR]\springframework\security\samples\config\SecurityConfig.java:[10,7] error: cannot access Filter
[INFO] 2 errors 

你能指导我有什么问题吗?

<project
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
        >
    <modelVersion>4.0.0</modelVersion>
    <groupId>***</groupId>
    <artifactId>***</artifactId>
    <packaging>war</packaging>
    <version>0.1</version>
    <name>Spring MVC Application</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>$project.build.sourceEncoding</project.reporting.outputEncoding>
        <java-version>1.7</java-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <ch.qos.logback-version>1.0.13</ch.qos.logback-version>
        <org.thymeleaf-version>2.1.2.RELEASE</org.thymeleaf-version>
        <org.thymeleaf.extras.springsecurity3-version>2.1.1.RELEASE</org.thymeleaf.extras.springsecurity3-version>
        <springframework.org-version>4.0.6.RELELASE</springframework.org-version>
    </properties>
    <repositories>
        <repository>
            <id>java.net2</id>
            <name>Repository hosting the jee6 artifacts</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>sonatype-oss-repository</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <version>4.0.6.RELEASE</version>
            <artifactId>spring-context</artifactId>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
                    <version>4.0.6.RELEASE</version>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <!-- Security -->
        <dependency>
                    <version>3.2.4.RELEASE</version>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
                    <version>3.2.4.RELEASE</version>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>

    <!-- View -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>$org.thymeleaf-version</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
        <version>$org.thymeleaf.extras.springsecurity3-version</version>
    </dependency>
    <!-- Persistence -->
    <dependency>
                <version>4.0.6.RELEASE</version>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
                <version>4.0.6.RELEASE</version>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
                <version>4.0.6.RELEASE</version>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <!-- Spring Data -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.4.1.RELEASE</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>$org.aspectj-version</version>
    </dependency>
    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>$org.slf4j-version</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>$org.slf4j-version</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>$ch.qos.logback-version</version>
    </dependency>
    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <!-- JSON -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
    <!-- Utilities -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>14.0.1</version>
    </dependency>
    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>1.5.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.objenesis</groupId>
        <artifactId>objenesis</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
                <version>4.0.6.RELEASE</version>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-dbcp</artifactId>
        <version>8.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.4.Final</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.29</version>
    </dependency>


</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <url>http://localhost:8080/manager/text</url>
                <server>localhost</server>
                <path>/coco</path>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>$java-version</source>
                <target>$java-version</target>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <warName>cocoadm-0.1</warName>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>sources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <encoding>$project.build.sourceEncoding</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>$org.thymeleaf-version</version>
        </dependency>
    </dependencies>
</dependencyManagement>

【问题讨论】:

【参考方案1】:

尝试添加servlet-api依赖

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

【讨论】:

是的,这就是原因。谢谢:) 一些注意事项:我在Build Path 中添加Tomcat 8.0 作为Server Runtime。虽然 eclipse 并没有抱怨这样的错误,但在命令行中运行 mvn verify 仍然会报告这个错误。无论如何,在pom.xml 中显式添加此依赖项即可解决问题。【参考方案2】:

如果你的项目是 spring-boot,添加这个:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

【讨论】:

这里会介绍servlet-api。所以mavarazy是正确的答案。因为你不能总是介绍spring-boot-starter-web。比如你在一个spring cloud gateway项目中。 确实,云网关使用 Webflux (Netty),而 Spring Boot Web 使用 Tomcat,不能一起使用

以上是关于Spring-security - 无法访问 ServletException的主要内容,如果未能解决你的问题,请参考以下文章

spring-security 开启注解权限控制为什么没有效果

spring-security 登录 无法进入UserDetailsService类

在 spring-security 的身份验证中无法获取自定义信息

带有 spring-security 的 OAuth2 - 通过 HTTP 方法限制 REST 访问

Spring-Security OAuth2 设置 - 无法找到 oauth2 命名空间处理程序

Spring-Security权限管理框架——根据角色权限登录