我使用的是基于注释的配置,到目前为止没有web.xml。
现在,根据文档,我需要创建一个web.xml文件,并向其中添加以下字段:
<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.xml,并将只放在这个位置,我会在运行时遇到一些其他错误(比如缺少ContextLoaderListener等)。
web.xml是标准web应用程序打包结构的一部分。这种结构允许您在不同的服务器(如Tomcat和Jetty)上部署打包的war文件。
您可以在此处阅读有关web.xml的更多信息:http://en.wikipedia.org/wiki/Deployment_descriptor
您可以在这里阅读标准目录结构(这是针对Tomcat的,但大多数web服务器都遵循相同/相似的结构):http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html#Standard_Directory_Layout
如果您的应用程序是web应用程序,那么您应该已经有了web.xml。如果没有,那么您应该而不是创建web.xml,而是在SpringSecurity中找到另一种挂钩方式。请告诉我们您的应用程序当前是如何部署的。
下面是一个带有Spring Security的Spring的web.xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Spring Security Filter -->
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- The front controller of the Spring MVC Web application, responsible
for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/web-application-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
对于web应用程序,您需要一个web.xml。
关于您的错误缺少ContextLoaderListener,只需将其添加到web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>