结合DispatcherServlet,ContextLoaderListener和SpringSecurity

时间:2012-06-11 13:20:02

标签: java hibernate servlets spring-mvc spring-security

我有一个很难解决的问题..试图将所述3种技术整合到我们的webapp中..我们想要使用

  1. Spring Web
  2. Spring MVC as view technology(with freemarker)
  3. Spring Security as security layer
  4. 但无论我配置web.xml和其他上下文文件的哪种方式,都无法让所有内容同时工作.. 使用我当前的配置一切都会正常工作,除了SpringSecurity不会拦截URL模式< / strong>

    一些谷歌搜索(和公共感觉)告诉我,组合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>
    

    我的manager-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... -->
    

    manager-datasource只是设置数据库......


    感谢您阅读本文,希望能帮助我......;)


    修改:更多信息

    我不能只跳过ContextLoaderListener,它是SpringSecurity所必需的。我也不能跳过contextConfigLocation,因为这是ContextLoaderListener所需的上下文。我只是自己定义名称,否则它将搜索applicationContext.xml。也许我可以添加一个空的contextConfigLocation?但这可能仅仅意味着,底层模块将无法找到他们的bean来注入..?


    EDIT2

    我认为主要的问题是SpringSecurity需要一个webapp上下文(ContextLoaderListener)才能工作,但Web应用程序在servlet上下文中运行。控制器方法由servlet上下文映射,因此在servlet上下文“外部”运行的spring安全性不会被事件通知,并且过滤器也没有启动..

1 个答案:

答案 0 :(得分:0)

叹息..我不知道为什么它第一次没有工作,可能错误的解决方案是在缓存中尝试正确的一个..但它现在正在我发布的方式工作。感谢Arun的时间。

现在只有问题:spring-test-mvc不支持Spring Security的FilterChain,但这是另一个问题..