我已使用以下 web.xml 文件在 TomEE Web 服务器中启用了 CORS:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!-- 
    Licensed to the Apache Software Foundation (ASF) under one or more 
    contributor license agreements.  See the NOTICE file distributed with 
    this work for additional information regarding copyright ownership. 
    The ASF licenses this file to You under the Apache License, Version 2.0 
    (the "License"); you may not use this file except in compliance with 
    the License.  You may obtain a copy of the License at 
       http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 
<web-app 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" 
         version="3.0" metadata-complete="true"> 
 
    <display-name>Spotitube</display-name> 
 
 
    <filter> 
        <filter-name>CorsFilter</filter-name> 
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> 
    </filter> 
    <filter-mapping> 
        <filter-name>CorsFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter> 
        <filter-name>CorsFilter</filter-name> 
        <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> 
        <init-param> 
            <param-name>cors.allowed.origins</param-name> 
            <param-value>*</param-value> 
        </init-param> 
        <init-param> 
            <param-name>cors.allowed.methods</param-name> 
            <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value> 
        </init-param> 
        <init-param> 
            <param-name>cors.allowed.headers</param-name> 
            <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value> 
        </init-param> 
        <init-param> 
            <param-name>cors.exposed.headers</param-name> 
            <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value> 
        </init-param> 
        <init-param> 
            <param-name>cors.support.credentials</param-name> 
            <param-value>true</param-value> 
        </init-param> 
        <init-param> 
            <param-name>cors.preflight.maxage</param-name> 
            <param-value>10</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>CorsFilter</filter-name> 
        <url-pattern>*</url-pattern> 
    </filter-mapping> 
 
 
</web-app> 

自此更改以来,我的所有路线都从同一来源以及另一个来源返回 404。有谁知道这可能是什么原因造成的? 我还尝试了这里最基本的 TomEE cors 过滤器:https://enable-cors.org/server_tomcat.html但这只会导致相同的结果。

提前致谢。

请您参考如下方法:

您是否打算设置metadata-complete="true"?它会停用一些扫描,因此您可能希望将其保留为 false

您在服务器上部署了什么?这个 web.xml 没有 servlet,所以我猜它应该是扫描过的东西?


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!