Spring 4 websocket + Tomcat 7.54 async-supported not working

偶尔善良 提交于 2019-12-29 07:54:30

问题


I am creating a sample chat application using the Spring websockets and stomp.js , I am using the tomcat 7.54 but while runing the application I am gettting a async-supported error when browser is making xhr request.

Server info: Apache Tomcat/7.0.54 Servlet version: 3.0 JSP version: 2.2 Java version: 1.7.0_25

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
    <async-supported>true</async-supported>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="hello" />

    <websocket:message-broker application-destination-prefix="/app">
        <websocket:stomp-endpoint path="/hello">
            <websocket:sockjs/>
        </websocket:stomp-endpoint>
        <websocket:simple-broker prefix="/topic"/>
    </websocket:message-broker>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

ERROR

java.lang.IllegalArgumentException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml. Also you must use a Servlet 3.0+ container
    at org.springframework.util.Assert.isTrue(Assert.java:65) 

回答1:


I guess you don't show entire web.xml.

<async-supported>true</async-supported> should be configured for <filter>, too.

UPDATE

Well, your issue is very simple:

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

You really should map for all requests, not only the root.




回答2:


The <async-supported>true</async-supported> should be included in both the servlet and filter tags. Use the following snippet as reference:

<web-app ...>
    ...
    <servlet>
        <servlet-name>instantaction</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:META-INF/spring/web/my-servlet-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        ...
    </servlet-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>



回答3:


Add the below in nginx.conf file if you are using the reverse proxy.

  # For WebSocket upgrade header
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";



回答4:


Try upgrade you jdk and tomcat version. I encountered this problem also, I upgrade jdk from 1.7 to 1.8, upgrade tomcat form 7.0.54 to 7.0.75, and resolved this problem.




回答5:


Make sure no other injected component disables async support.


DETAILS

I learned that Spring comes with asynchronous support by default.

And (remotely related) sync logging configuration may disable async handling for entire service.

Specifically, my logback integration was missing IMPORTANT line:

public EmbeddedServletContainerCustomizer containerCustomizer(
    final String logbackAccessClasspathConfig
) {
    return container -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            ((TomcatEmbeddedServletContainerFactory) container)
                .addContextCustomizers(context -> {
                    LogbackValve logbackValve = new LogbackValve();
                    logbackValve.setFilename(logbackAccessClasspathConfig);

                    // IMPORTANT:
                    logbackValve.setAsyncSupported(true);

                    context.getPipeline().addValve(logbackValve);
                }
            );
        }
    };
}

Thanks to other answer:

  • LogbackValve is configured for synchronous processing by default
  • disable the bean that was injecting LogbackAccess
  • set the asyncSupported="true" attribute where you configure the valve



回答6:


I was having the same issue with SpringBoot app and for me, it started working

1) after adding asyncSupported to the filter like @WebFilter(urlPatterns="/api-acess/*",asyncSupported = true )

2) added sameOrigin() support to WebSecurityConfigurerAdapter extended class like http.headers().frameOptions().sameOrigin();

I hope it will help someone else.



来源:https://stackoverflow.com/questions/24903475/spring-4-websocket-tomcat-7-54-async-supported-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!