java - Tomcat 8.5 - 错误 403 Forbidden or empty params on POST request
问题描述
我的 Web 应用程序包含一个 html 页面,该页面将数据提交给一个 java servlet,该 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>
解决方案
它也发生在我身上。对于发布请求,我的请求是这样的:
{
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 实例上出现问题。
推荐阅读
- uwp - Run time broker takes more time to remove from task mahager
- javascript - Google Analytics called twice on React web apps
- swift - Google API - 凭据无效
- mercurial - Hg Mercurial over Google Drive 问题
- php - Heroku. Who can I run the project on php + reactJS?
- javascript - How to handle 401 for all Kendo UI objects in one place?
- php - 带有 PHP-ML 和回归的推荐引擎
- ajax - How to Ajaxify a Partial View which uses Razor Validation
- npm - npm i 命令连续运行
- javascript - 我如何分别选择每一行,我需要为每一行添加一些样式