2012-06-11 25 views
5

我有相當螺母在這裏破解..一直試圖綜合所述3個技術整合到我們的web應用。我們要使用結合的DispatcherServlet,ContextLoaderListener的和SpringSecurity

  1. 的Spring Web
  2. Spring MVC的作爲視圖技術(與freemarker的)
  3. 春季安全作爲安全層

但無論怎樣配置我web.xml和其他方面的文件,我不能把一切在同一時間到工作.. 以我目前的配置一切都將正常工作,除了SpringSecurity不會攔截的URL模式

一些google搜索(和公共意識)跟我說,合併的DispatcherServlet和ContextLoaderListener的可能是一個問題。

所以這裏是我的配置。 (對不起,這麼多的文字和感謝閱讀):

的web.xml:

<!-- Needed by Spring --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/dw-manager-context.xml</param-value> 
</context-param> 

<!-- Needed by Spring MVC --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/servlet-context.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- Needed by Spring Security --> 
<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> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
</filter-mapping> 

我的servlet-context.xml中:

<!-- Scan for controllers --> 
<context:component-scan base-package="dw.manager" /> 

<!-- Need to declare annotation driven transactions again so they are picked up above controller methods --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<mvc:annotation-driven /> 

<bean id="viewResolver"  class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
    <!-- ... --> 
</bean> 

<bean id="freemarkerConfig" 
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
    <!-- ... --> 
</bean> 

我的經理-context.xml中:

<context:annotation-config /> 

    <!-- deployment-setup just loads properties (files) --> 
<import resource="deployment-setup.xml" /> 
<import resource="spring-security-context.xml" /> 
<import resource="dw-manager-datasource.xml" /> 

<!-- Import sub-modules --> 
<import resource="classpath:dw-security-context.xml" /> 
<!-- ... --> 

Spring-Security-context.xml:

<http pattern="/login" security="none" /> 
<http pattern="/accessDenied" security="none" /> 
<http pattern="/" security="none" /> 

<!-- enable use of expressions, define URL patterns and login/logout forms 
    and targets) --> 
<http use-expressions="true" access-denied-page="/accessDenied"> 
    <intercept-url pattern="/*" access="hasRole('ROLE_USER')" /> 
    <!-- more stuff --> 
</http> 

<!-- a lot more... --> 

經理,數據源只是建立數據庫...


感謝的閱讀這一切,並希望能幫助我..;)


編輯:一些更多的信息

我不能跳過ContextLoaderListener,它是SpringSecurity所必需的。我也不能跳過contextConfigLocation,因爲這是ContextLoaderListener所需的上下文。我只需定義名稱,否則它將搜索applicationContext.xml。也許我可以添加一個空的contextConfigLocation?但是,這可能意味着,底層模塊將無法找到他們的bean注入..?


EDIT2

我認爲主要的問題是,SpringSecurity需要一個Web應用程序上下文(ContextLoaderListener的)工作,但web應用的servlet上下文中運行。控制器方法通過servlet上下文進行映射,從而彈簧安全性運行在servlet上下文的「外部」不會被事件通知,並且過濾器不會進入。

+0

你能告訴我,如果你給一個請求ILIKE'/ test',莫非要在發生什麼控制器還是重定向到登錄頁面? –

+0

'2012-06-11 15:31:23 PageNotFound [WARN]在名爲'appServlet''的DispatcherServlet中找不到具有URI [/ dw-manager/test]的HTTP請求的映射 – Pete

+0

如果您提供了有效的url,回覆回覆? –

回答

0

嘆息..我不知道爲什麼它第一次沒有工作,可能是錯誤的解決方案是在嘗試正確的解決方案時在緩存中。但是現在它的工作方式就像我發佈它的方式一樣。感謝Arun你的時間。

現在唯一的問題是:彈簧試驗MVC不支持Spring Security的FilterChain但這是另一個問題..

相關問題