【问题标题】:Tomcat 8.5 - Error 403 Forbidden or empty params on POST requestTomcat 8.5 - 错误 403 Forbidden or empty params on POST request
【发布时间】:2019-07-12 15:32:24
【问题描述】:

我的 Web 应用程序包含一个 html 页面,该页面将数据提交给 java servlet,java servlet 从中生成 pdf 文件。 在本地测试和测试服务器上一切正常,但由于我使用生产服务器,它与证书一起映射到注册的子域并使用 https,我收到错误 403 - Forbidden You don't have permission to access /Project/servlet on this server。 html 将包含在与 tomcat 服务器不同的机器上的不同网站中。

如何允许访问 servlet?

编辑: 我尝试通过 JavaScript 提交表单,这给了我没有 403 错误但状态码为 200,但发送了一个空的请求正文 - 没有参数,这是非常没用的。

let xhr = new XMLHttpRequest();
const url = serverpath + "/servlet";
 xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        window.open(xhr.response);
    }
}

xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/json');

xhr.send(JSON.stringify({
    value1: "1",
    value2: "2",
    value3: "3"
 }));

当使用 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 并以 xhr.send('value1='1'&value2='2'+'&value3='3'); 的形式发送时,它会返回错误 403 Forbidden。

这是我的项目的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>StaticsCalculator</display-name>
    <welcome-file-list>
        <welcome-file>html/htmlpage.html</welcome-file>
        <welcome-file>servlet</welcome-file>
    </welcome-file-list>
<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.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>
    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>logging.xml</param-value>
    </context-param>
</web-app>

【问题讨论】:

    标签: java tomcat servlets configuration


    【解决方案1】:

    这也发生在我身上。对于发布请求,我的请求是这样的:

    { url:url, method: 'POST', dataType : 'json', }

    它在其中一个 Tomcat 实例中引发 403 错误。

    添加 data 和 contentType 后,它开始正常工作了。

    { url:url, method: 'POST', contentType:'application/x-www-form-urlencoded', dataType : 'json', data:'', }

    我的网址包含查询参数。我相信,在这种情况下出现的 403 禁止与 tomcat 配置有关,因为对我来说它在我的本地工作,但在其他一些 tomcat 实例上出现问题。

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多