简体   繁体   English

在 Tomcat 中部署 Spring Boot 应用程序时,没有名为“loggingFilter”的 bean 可用错误

[英]No bean named 'loggingFilter' available error when deploying Spring Boot application in Tomcat

I have a Spring Boot application with Spring Security enabled.我有一个启用了 Spring Security 的 Spring Boot 应用程序。 When I run it on IntelliJ it works fine and I am able to log in and use the application as expected.当我在 IntelliJ 上运行它时,它工作正常,我能够按预期登录并使用该应用程序。 But when I try to deploy it on tomcat 8.5.x and 9.x I get the error No bean named 'loggingFilter' available error when deploying the application in Tomcat .但是,当我尝试在 tomcat 8.5.x 和 9.x 上部署它时,在 Tomcat 中部署应用程序时出现错误No bean named 'loggingFilter' available 错误 Here is the complete log这是完整的日志

08-Feb-2020 00:59:35.839 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log 3 Spring WebApplicationInitializers detected on classpath
08-Feb-2020 00:59:39.959 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
08-Feb-2020 00:59:57.047 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.filterStart Exception starting filter [delegatingFilterProxy]
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'loggingFilter' available
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1114)
        at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:338)
        at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:243)
        at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:239)
        at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:283)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:112)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4546)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5191)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1720)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:479)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:428)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:809)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1406)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:827)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)
08-Feb-2020 00:59:57.052 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext

If it helps, I use logback.xml file to show INFO logs如果有帮助,我使用logback.xml文件来显示 INFO 日志

<configuration debug="true" scan="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

pom.xml pom.xml

<properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    <javax.servlet-api.version>3.0.1</javax.servlet-api.version>
    <jsp-api.version>2.2</jsp-api.version>
    <tiles-jsp.version>3.0.8</tiles-jsp.version>
    <json-simple.version>1.1</json-simple.version>
    <commons-fileupload.version>1.3.1</commons-fileupload.version>
    <joda-time.version>2.10.4</joda-time.version>
    <modelmapper.version>2.3.0</modelmapper.version>
    <lombok.version>1.18.10</lombok.version>
    <jackson.version>2.9.3</jackson.version>
    <junit.version>4.12</junit.version>
    <spring-boot.version>2.2.2.RELEASE</spring-boot.version>
    <json.version>20190722</json.version>
    <validation-api.version>2.0.1.Final</validation-api.version>
    <hibernate.version>5.4.10.Final</hibernate.version>
    <mssql-jdbc.version>7.4.1.jre11</mssql-jdbc.version>
    <hazelcast.version>3.12.6</hazelcast.version>
    <jjwt.version>0.9.1</jjwt.version>
</properties>
<dependencies>
        <!-- Spring Core dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring-boot.version}</version>
            <optional>false</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>


        <!-- Other Core dependencies-->

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-envers</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>${mssql-jdbc.version}</version>
        </dependency>

        <!-- Utilities-->
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>${hazelcast.version}</version>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-spring</artifactId>
            <version>${hazelcast.version}</version>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-hibernate53</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-client</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${json.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>${modelmapper.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${validation-api.version}</version>
        </dependency>


        <!-- Test dependencies-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>${spring-boot.version}</version>
        </dependency>

    </dependencies>

The cause of this exception is straightforward – Spring Security looks for a bean named loggingFilter , and cannot find it.这个异常的原因很简单——Spring Security 寻找一个名为loggingFilter的 bean,但找不到它。 This bean is required by the main Spring Security Filter – the DelegatingFilterProxy – defined in the web.xml这个bean由主春季安全过滤器要求-在DelegatingFilterProxy的-在规定web.xml

DelegatingFilterProxy is a class in Spring's Web module. DelegatingFilterProxy 是 Spring 的 Web 模块中的一个类。 It provides features for making HTTP calls pass through filters before reaching the actual destination.它提供了使 HTTP 调用在到达实际目的地之前通过过滤器的功能。 With the help of DelegatingFilterProxy, a class implementing the javax.Servlet.Filter interface can be wired into the filter chain.在 DelegatingFilterProxy 的帮助下,可以将实现 javax.Servlet.Filter 接口的类连接到过滤器链中。

DelegatingFilterProxy also leverages invoking specific or multiple filters as per Request URI paths by providing the configuration in Spring's application context or in web.xml. DelegatingFilterProxy 还通过在 Spring 的应用程序上下文或 web.xml 中提供配置来利用根据请求 URI 路径调用特定或多个过滤器。

Therefore, you must have a custom filter class and a configuration associated with this class.因此,您必须具有自定义过滤器类和与此类关联的配置。 Check it because there is something you didn't see.检查它,因为有一些你没有看到的东西。

@Component("loggingFilter")
public class CustomFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void doFilter(
      ServletRequest request, ServletResponse response, 
      FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        LOGGER.info("Request Info : " + req);
        chain.doFilter(request, response);
    }
    @Override
    public void destroy() {
    }
}

To register a custom filter using Java settings :要使用Java 设置注册自定义过滤器:

public class ApplicationInitializer 
  extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected javax.servlet.Filter[] getServletFilters() {
        DelegatingFilterProxy delegateFilterProxy = new DelegatingFilterProxy();
        delegateFilterProxy.setTargetBeanName("loggingFilter");
        return new Filter[]{delegateFilterProxy};
    }
}

Custom filter using configuration in web.xml使用web.xml 中的配置自定义过滤器

<filter>
    <filter-name>loggingFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>loggingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

CustomFilter implements javax.Servlet.Filter. CustomFilter 实现 javax.Servlet.Filter。 This class has a @Component annotation to register as Spring bean in the application context.这个类有一个@Component 注释,用于在应用程序上下文中注册为 Spring bean。 This way, the DelegatingFilterProxy class can find our filter class while initializing the filter chain.这样,DelegatingFilterProxy 类就可以在初始化过滤器链时找到我们的过滤器类。

Note that the name of the Spring bean must be the same as the value in the filter-name provided during the registration of the custom filter in ApplicationInitializer class or in web.xml later because the DelegatingFilterProxy class will look for the filter bean with the exact same name in the application context.请注意,Spring bean 的名称必须与以后在 ApplicationInitializer 类或 web.xml 中注册自定义过滤器时提供的 filter-name 中的值相同,因为 DelegatingFilterProxy 类将查找具有确切名称的过滤器 bean应用程序上下文中的相同名称。

If it can't find a bean with this name, it will raise an exception at application startup.如果找不到具有此名称的 bean,它将在应用程序启动时引发异常。

You can find more information here .您可以在此处找到更多信息。

Something that you can also check is if it is an incompatibility problem between version of Spring and java that you use.您还可以检查是否是您使用的 Spring 和 java 版本之间不兼容问题

I hope it can help you and in the end find a solution.我希望它可以帮助您并最终找到解决方案。

In your question you indicate that "...When I run it on IntelliJ it works fine... But when I try to deploy it on tomcat 8.5.x and 9.x I get the error..."在您的问题中,您指出“......当我在 IntelliJ 上运行它时它工作正常......但是当我尝试在 tomcat 8.5.x 和 9.x 上部署它时,我收到错误......”

Follow these steps to find the cause:请按照以下步骤查找原因:

1) Do a complete Project clean: maven clean update, Tomcat clean, and regenerate the war files. 1)做一个完整的Project clean:maven clean update,Tomcat clean,重新生成war文件。 This will ensure that your war is the same on both tests.这将确保您的战争在两个测试中是相同的。

2) If the problem persists look at ItelliJ server version and make sure you are using the same Tomcat version for deployment. 2)如果问题仍然存在,请查看 ItelliJ 服务器版本并确保您使用相同的 Tomcat 版本进行部署。

3) If none of these is the cause you may be trying to fetch the required files from a remote location. 3)如果这些都不是原因,您可能正在尝试从远程位置获取所需的文件。 Do you have aditional war files in the Tomcat server?你在 Tomcat 服务器中有额外的战争文件吗? are they using the same version?他们使用相同的版本吗? are you able to manually deploy in a fresh Tomcat on your machine without the help of IntellJ?你能在没有 IntellJ 帮助的情况下在你的机器上手动部署一个新的 Tomcat 吗?

4) If the Tomcat is exactly the same, the wars are exactly the same and the Host is exactly the same. 4)如果Tomcat完全一样,wars完全一样,Host完全一样。 the results should be exactly the same*.结果应该完全相同*。 If this is not the case, one of the previous statements is false so double check.如果不是这种情况,则前面的陈述之一是错误的,因此请仔细检查。

*Java version, memory allocation, and Server options can also Affect the result, but most likely it's not the case here. *Java 版本、内存分配和服务器选项也会影响结果,但很可能不是这里的情况。

After you find the cause you can apply the following to solve it:找到原因后,您可以应用以下方法来解决它:

  • If your cause is the War, compare it with the previous working version, and you will probably find to have added a Custom Filter while adding Spring Security.如果您的原因是战争,将其与以前的工作版本进行比较,您可能会发现在添加 Spring Security 的同时添加了自定义过滤器。

  • If your cause is Tomcat, you may have a few extra libraries in IntelliJ's which your out of the box tomcat does not have.如果您的原因是 Tomcat,您可能在 IntelliJ 中有一些额外的库,而您的开箱即用的 tomcat 没有。

  • And if you use aditional wars make sure to update them如果您使用附加战争,请确保更新它们

For a detailed explanation of DelegatingFilterProxy and Custom Filters checkout borchvm 's awnser.有关DelegatingFilterProxy和自定义过滤器的详细说明,请查看 borchvm的 awnser。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导说没有名为“entityManagerFactory”的bean可用 - Spring Boot says no bean named 'entityManagerFactory' available Spring Boot-没有可用的名为&#39;entityManagerFactory&#39;的bean - Spring Boot - No bean named 'entityManagerFactory' available 错误启动子部署 spring 引导 web 应用程序到 Tomcat 9 - Error starting child deploying spring boot web application to Tomcat 9 HTTP错误502.3-在Azure Tomcat应用程序服务中部署Spring Boot应用程序后,网关错误 - HTTP Error 502.3 - Bad Gateway when after deploying Spring boot application in azure tomcat app service java.lang.NoSuchMethodException 部署在 tomcat 上时 Spring-boot Web 应用程序中的错误 - java.lang.NoSuchMethodException error in Spring-boot web-application when deploying on tomcat 在 Docker 镜像中的 Tomcat 中部署 Spring Boot 应用程序 - Deploying a Spring Boot Application in Tomcat in a Docker image Spring Security错误没有名为&#39;springSecurityFilterChain&#39;的bean可用 - Spring Security Error No bean named 'springSecurityFilterChain' available Spring Security“没有可用的名为&#39;filterChainProxy&#39;的bean”错误 - Spring Security “No bean named 'filterChainProxy' available” error 在 tomcat 上部署时出现 spring 引导问题的 Keycloak - Keycloak with spring boot issue when deploying on tomcat Spring 引导 v2.1.0.RELEASE - 没有名为“entityManagerFactory”的 bean 可用 - Spring Boot v2.1.0.RELEASE - No bean named 'entityManagerFactory' available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM