2012-08-02 81 views
0

我正在使用基于注释的配置,目前为止没有使用web.xml。现在有关web.xml的Spring Security注释配置

according to documentation,我需要创建一个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等等..)。

回答

2

web.xml是标准网络应用程序打包结构的一部分。这种结构允许你在Tomcat和Jetty等不同的服务器上部署打包的war文件。

你可以阅读更多关于web.xml中的位置:http://en.wikipedia.org/wiki/Deployment_descriptor

你可以阅读这里的标准目录结构(这是Tomcat的,但大部分网络服务器都遵循相同/相似的结构): http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html#Standard_Directory_Layout

如果你的应用程序是一个web应用程序,你应该已经有一个web.xml。如果没有,那么你应该而不是创建一个web.xml,但找到另一种挂钩在Spring安全的方式。请让我们知道您的应用程序目前如何部署。

这里是春天有个web.xml中使用Spring Security的例子:

<?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> 
+0

所有我想我必须包括一个web.xml后。 问题在于,使用Spring 3.1,您可以删除web.xml并仅使用基于注释的配置继续。但对于这样的情况 - 我无法找到合适的注释或指定Java类中的配置的方法。 也为'''我也必须包括web.xml,所以这是一个足够的理由。 – 2012-08-03 06:23:36

+0

我会再添加一个提交,而不是指向XML Spring配置,web-application-config.xml,我可以说配置是注释驱动的,就像这个例子:https://gist.github。 com/1299632 – 2012-08-03 06:57:15

+0

是的,这是正确的。如果你想使用@Configuration来配置你的bean,你可以在web.xml中使用AnnotationConfigWebApplicationContext。我认为这种方法是最新最好的方法。我将不得不更新我的项目以遵循它! – jasop 2012-08-03 07:54:30

1

对于你需要一个web.xml一个Web应用程序。

关于你的错误失踪的ContextLoaderListener,只需添加这到web.xml

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

是的,我说那个:)但是事情是我需要添加更多的东西,但最终也许它是它的方式。 – 2012-08-03 06:28:08

+0

:)你的web.xml不会超过几行。正如你所说,休息你可以有一个@配置类,并配置你的豆子.. – shazinltc 2012-08-04 04:39:14