使用 Spring 引导应用程序的 Wildfly:来自 SPA 应用程序的并发请求导致 403

Wildfly with Spring Boot app: concurrent requests from SPA app result in 403

我们在 Wildfly 18 中部署了一个 Spring Boot 应用程序,通过 war in ear(使用 SpringBootServletInitializer)。其他 war 出现在耳朵中,会话共享已启用。 war 还共享一个安全域。

从浏览器或邮递员查询 Spring 引导应用程序 REST API 工作正常。当它是 SPA 应用程序时,它会失败:并发请求得到 403 禁止,例如在访问日志中:

127.0.0.1 [02/Jul/2020:22:54:46 +0200] "GET /api/Foo?bar=1 HTTP/1.1" 403 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"

序列化 SPA 请求使其再次工作,但这只是为了测试目的:我们需要来自 SPA 的并发请求!

我在 standalone.xml 中尝试了很多配置,例如,因为我们使用会话共享并且我希望每个会话一次有多个请求:

     <subsystem xmlns="urn:jboss:domain:infinispan:9.0">
        <cache-container name="web" default-cache="passivation" module="org.wildfly.clustering.web.infinispan">
            <local-cache name="passivation">
                <locking isolation="NONE"/>
                <transaction mode="BATCH"/>
                <file-store passivation="true" purge="false"/>
            </local-cache>
            <local-cache name="persistent">
                <transaction mode="BATCH"/>
                <file-store passivation="false" purge="false"/>
            </local-cache>
        </cache-container>

或者这个有底流允许并发请求:

    <subsystem xmlns="urn:jboss:domain:undertow:10.0" statistics-enabled="true">
        <buffer-cache name="default"/>
        <server name="default-server">
            <http-listener name="default" max-connections="100" socket-binding="http" max-parameters="5000" allow-unescaped-characters-in-url="true"/>
            <host name="default-host" alias="localhost">
                <access-log pattern="%h %t &quot;%r&quot; %s &quot;%{i,User-Agent}&quot;" use-server-log="false"/>
                <filter-ref name="x-frame-options"/>
                <filter-ref name="limit-connections"/>
            </host>
        </server>
        <servlet-container name="default" default-encoding="UTF-8" max-sessions="10000">
            <jsp-config/>
            <session-cookie http-only="true"/>
        </servlet-container>
        <filters>
            <request-limit name="limit-connections" max-concurrent-requests="3000" queue-size="100"/>
            <response-header name="x-frame-options" header-name="X-Frame-Options" header-value="SAMEORIGIN"/>
        </filters>
    </subsystem>

欢迎提供有关消除并发请求中这些令人讨厌的 403 的任何帮助!

我认为您需要允许 CORS 使用 SPA 托管服务器。

您可以通过Java Configuration(WebMvcConfigurer) or @CrossOrigin annotaion设置它。

问题已解决:war 耳中的前端应用程序不在安全域中。并且默认安全域不是其他 wars 使用的域。

我们已经将前面的 war 放在同一个域中(通过 jboss-web.xml)并且...没有更多的 403!我们有点惊讶 403 并没有出现在每次通话中,但我们很高兴它能正常工作。