我正在尝试将Spring Security包含到我的Web项目中,我正在遵循本教程http://docs.spring.io/spring-security/site/docs/current/guides/html5//helloworld.html
我已经使用给定的maven项目完成了本教程中的所有操作,并且工作正常。但是,当我尝试将其包含到我的项目中时,会出现编译错误。特别是当我从AbstractSecurityWebApplicationInitializer
扩展时出现给定的错误
POM.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring.primefaces</groupId>
<artifactId>primefaces.testwebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringPrimefacesWebApp</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- JSF 2.2 core and implementation -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.11</version>
</dependency>
<!-- Prime Faces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.2</version>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
谢谢您的帮助!
使用mvn clean install -U
最佳答案
只需将 javax.servlet
API添加到编译时间相关项即可。您不需要在构建中包括它,它已经由目标servlet容器提供。
您当前的pom建议您要部署到准系统servlet容器(Tomcat,Jetty等),而不是完整的Java EE应用程序服务器(WildFly,TomEE,GlassFish,Liberty等),否则,您将遇到类加载通过与Webapp一起提供JSF而不是使用容器提供的JSF,可以解决与问题相关的麻烦。
在这种情况下,应为Tomcat 8之类的Servlet 3.1容器添加以下依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
或者,如果您实际上是针对像Tomcat 7这样的旧Servlet 3.0容器,则将
<version>
更改为3.0.1
(注意:由于它们的错误,没有3.0
)。如果您碰巧实际部署到WildFly 8之类的Java EE 7应用服务器,请改用以下依赖项。它涵盖了整个Java EE API,包括
javax.servlet
(和javax.faces
,因此您将删除那些单独的JSF API/impl依赖项):<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
同样在这里,如果您以JBoss AS 7之类的较旧Java EE 6应用服务器为目标,请将
<version>
更改为6.0
。