2012-08-02 82 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