【问题标题】:How to implement correctly web.xml for spring-mvc and spring-security together如何为 spring-mvc 和 spring-security 一起正确实现 web.xml
【发布时间】:2017-02-15 18:27:10
【问题描述】:

您好,我正在尝试开发一个包含 spring security 和 hibernate 的 spring mvc web 应用程序。我在谷歌上搜索,但我不知道如何在 web.xml 文件中连接 spring mvc 配置和 spring 安全配置。最后我实现了我的 web.xml 文件如下,但我得到了这个错误。

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“springSecurityFilterChain”的bean可用

你能告诉我我的 web.xml 是否正确实现了吗?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SPY</display-name>

<filter>
        <filter-name>encoding-filter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
        </init-param>
 </filter>

    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>



<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
     <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/security.xml</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>



<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring.xml, WEB-INF/security.xml</param-value>
</context-param>


  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

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

</web-app>

提前致谢,

【问题讨论】:

    标签: java spring spring-mvc spring-security web.xml


    【解决方案1】:

    尝试在 web.xml 中添加 ContextLoaderListener,如下所示。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id="WebApp_ID" version="3.1">
    
        <display-name>SPY</display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring.xml, WEB-INF/security.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encoding-filter</filter-name>
            <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
            </filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>encoding-filter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <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-app>
    

    【讨论】:

    • 非常感谢,它成功了。元素的顺序重要吗?我的意思是 context-param 应该是第一个,然后是 listener 等等。这重要吗
    • 是的,XSD 要求元素按特定顺序列出。
    猜你喜欢
    • 2011-08-29
    • 2017-02-08
    • 1970-01-01
    • 2019-10-18
    • 2021-06-02
    • 2014-03-25
    • 2012-12-22
    • 2020-06-22
    • 2012-08-04
    相关资源
    最近更新 更多