结合DispatcherServlet,ContextLoaderListener和SpringSecurity

Pet*_*ete 5 java hibernate servlets spring-mvc spring-security

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

  1. Spring Web
  2. Spring MVC作为视图技术(带有freemarker)
  3. Spring Security作为安全层

但无论我配置web.xml和其他上下文文件的哪种方式,我都无法让所有内容同时工作.使用我当前的配置一切都会正常工作,但SpringSecurity不会拦截URL模式

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

我的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>
Run Code Online (Sandbox Code Playgroud)

我的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" />
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)

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... -->
Run Code Online (Sandbox Code Playgroud)

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


感谢所有阅读,希望能帮助我..;)


编辑:更多信息

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


EDIT2

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

Pet*_*ete 0

叹息..我不知道为什么它第一次不起作用,可能在尝试正确的解决方案时缓存中存在错误的解决方案..但是它现在正在按照我发布的方式工作。感谢阿伦抽出时间。

现在唯一的问题是: spring-test-mvc 不支持 Spring Security 的 FilterChain 但这是另一个问题了。