Java 将过滤器添加到 web.xml 后,一个或多个过滤器无法启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35442189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:40:40  来源:igfitidea点击:

One or more Filters failed to start after adding filters to web.xml

javaxmlspringmavenspring-mvc

提问by NemanjaT

Sorry if this is something trivial, but I'm pretty new to this.

对不起,如果这是微不足道的,但我对此很陌生。

Here's my pom dependencies:

这是我的 pom 依赖项:

This all works like this, but if I uncomment the <filter>part in web.xml I get the

这一切都像这样,但是如果我取消注释<filter>web.xml 中的部分,我会得到

One or more Filters failed to start.

一个或多个过滤器无法启动。

error. I tried googling it, but without success. I'm using Tomcat 8. I have no idea what else to say about the problem, since I'm trying to figure it out my self. I was following the tutorial from thislink (with XML configurations). I tried different spring and spring security versions, but every time the war 'explodes' it gives out the error above (and the application is, of course, unreachable)

错误。我尝试使用谷歌搜索它,但没有成功。我正在使用 Tomcat 8。我不知道关于这个问题还有什么要说的,因为我正在尝试自己解决这个问题。我正在关注链接中的教程(使用 XML 配置)。我尝试了不同的 spring 和 spring 安全版本,但每次战争“爆炸”时都会出现上述错误(当然,该应用程序无法访问)

Was something updated that I didn't know about.. or am I missing something else? I'll post the full error stack at the bottom.

是否有一些我不知道的更新......还是我错过了其他东西?我将在底部发布完整的错误堆栈。

<properties>
    <spring.version>4.2.4.RELEASE</spring.version>
    <spring.security.version>4.0.3.RELEASE</spring.security.version>
</properties>

<dependencies>
    <!-- spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
</dependencies>

web.xml

网页.xml

<?xml version="1.0" encoding="utf-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <display-name>SpringMVCVezba2</display-name>

    <!--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-->

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

spring-security.xml

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
       xmlns:beans = "http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
    <http auto-config="true">
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/home**" access="permitAll" />
        <intercept-url pattern="/store**" access="isAuthenticated" />
        <form-login login-page="/home/index" authentication-failure-url="/accessdenied" />
        <csrf/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user" password="123" authorities="USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

error stack:

错误堆栈:

16-Feb-2016 21:15:34.292 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
16-Feb-2016 21:15:34.458 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
16-Feb-2016 21:15:34.757 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Tue Feb 16 21:15:34 CET 2016]; root of context hierarchy
16-Feb-2016 21:15:34.870 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
16-Feb-2016 21:15:35.990 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.register Mapped "{[/home/index || /home/ || /home || /index || /]}" onto public java.lang.String com.nemanjat94.controllers.HomeController.index(org.springframework.ui.Model,com.nemanjat94.models.User)
16-Feb-2016 21:15:36.218 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Feb 16 21:15:34 CET 2016]; root of context hierarchy
16-Feb-2016 21:15:36.327 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Feb 16 21:15:34 CET 2016]; root of context hierarchy
16-Feb-2016 21:15:36.443 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
16-Feb-2016 21:15:36.502 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 2044 ms
16-Feb-2016 21:15:36.518 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more Filters failed to start. Full details will be found in the appropriate container log file
16-Feb-2016 21:15:36.519 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
16-Feb-2016 21:15:36.531 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.doClose Closing Root WebApplicationContext: startup date [Tue Feb 16 21:15:34 CET 2016]; root of context hierarchy
[2016-02-16 09:15:36,567] Artifact SpringMVCVezba2:war exploded: Error during artifact deployment. See server log for details.

采纳答案by NemanjaT

So, apparently, the problem was that springSecurityFilterChaincouldn't be called, so I just moved the content of spring-security.xmlto spring-servlet.xmland it all works as intended now.

所以,显然,问题是springSecurityFilterChain无法调用,所以我只是移动了spring-security.xmlto的内容,spring-servlet.xml现在一切都按预期工作了。

I wanted to seperate them by putting spring-security.xmlin the contextConfigLocationin the web.xml, but I couldn't add more than one (spring-servlet.xml), but looks like that's not going to work very well.

我想通过把对它们分开spring-security.xmlcontextConfigLocationweb.xml,但我不能添加一个以上的(spring-servlet.xml),不过貌似这不会很好地工作。

A big thank you to @wero for the logging tip!

非常感谢@wero 的日志提示!

回答by NameNotFoundException

There were no logs at all in my case. It was happening because of one of my Filter implementing class/method was throwing NullPointerException.

在我的案例中根本没有日志。这是因为我的过滤器实现类/方法之一抛出 NullPointerException。

回答by sundar.sat84

Issue : Your application was not able to read "spring-security.xml" file mentioned in web.xml in "context-param".

问题:您的应用程序无法读取 web.xml 中“context-param”中提到的“spring-security.xml”文件。

RCA : Follow the steps To find why/Which Filter failed to start. (Generic approach) Step 1 : Start your application in Debug Mode and immediately attach remote debuging from your IDE(Eclipse/Intelija...). Step 2 : Apply break point in Filter class which you mentioned in your web.xml file. In my case it was "DelegatingFilterProxy". Step 3 : Once application hits the break point than enable "Java Exception BreakPoint --> org.springframework.beans.factory.BeansException". Step 4 : Now IDE will show you exact location where the exception arises. You can further debug and solve this issue easily.

RCA:按照步骤查找原因/哪个过滤器无法启动。(通用方法)第 1 步:在调试模式下启动您的应用程序,并立即从您的 IDE(Eclipse/Intelija...)附加远程调试。第 2 步:在您在 web.xml 文件中提到的 Filter 类中应用断点。就我而言,它是“DelegatingFilterProxy”。第 3 步:一旦应用程序到达断点,就启用“Java Exception BreakPoint --> org.springframework.beans.factory.BeansException”。第 4 步:现在 IDE 将显示发生异常的确切位置。您可以进一步调试并轻松解决此问题。

Solution : Make Sure your application was able to read spring-security.xml file mentioned in "context-param" will solve this issue.

解决方案:确保您的应用程序能够读取“context-param”中提到的 spring-security.xml 文件将解决此问题。

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

Note : From the logs I didnot get any info.

注意:从日志中我没有得到任何信息。

回答by user674669

I had a similar issue. I found out that one of my filters was throwing an exception in the init method and it was logged in /usr/local/tomcat/logs/localhost.2019-12-07.log instead of the 'usual' log (/usr/local/tomcat/logs/catalina.out or /usr/local/tomcat/logs/catalina.2019-12-07.log)

我有一个类似的问题。我发现我的一个过滤器在 init 方法中抛出了一个异常,它记录在 /usr/local/tomcat/logs/localhost.2019-12-07.log 而不是“通常的”日志(/usr/ local/tomcat/logs/catalina.out 或 /usr/local/tomcat/logs/catalina.2019-12-07.log)