我在Tomcat 8上设置了一个简单的Spring Java(1.8)应用程序,它由一个Web表单和一个java控制器组成。当我通过Tomcat直接连接到Web应用程序时,我的所有控制器方法都能正常工作。但是,当我使用Apache 2.4使用反向代理时,POST方法不再起作用并返回405错误。 Tomcat和Apache都在同一个VM中运行。我使用的是Ubuntu 14.0.4。使用反向代理,我可以加载初始页面,该页面使用以下注释的控制器方法: @RequestMapping(method = RequestMethod.GET,value =“/ upload”)。 但是我的所有POST方法都失败了:
我在java日志中收到以下消息: o.s.web.servlet.PageNotFound:不支持请求方法'POST'
在我的浏览器控制台中,我在控制台中看到以下内容。请注意,当我不使用反向代理时,允许:HEAD,GET 不会出现。
Request URL:http://192.168.9.131/file-upload/logout Request Method:POST Status Code:405 Method Not Allowed Remote Address:192.168.9.131:80 Response Headers view source Allow:HEAD, GET Connection:Keep-Alive Content-Language:en-US Content-Length:338 Content-Type:text/html;charset=UTF-8 Date:Wed, 13 Jul 2016 12:28:35 GMT Keep-Alive:timeout=5, max=99 Server:Apache-Coyote/1.1 Request Headers Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:0 Content-Type:application/x-www-form-urlencoded Cookie:JSESSIONID=F41DA5B69E6AE49C710906C4591F2445 Host:192.168.9.131 Origin:http://192.168.9.131 Referer:http://192.168.9.131/ Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
启用了proxy_http和代理模块。我将以下内容放在我的Apache 2(版本2.4.7)虚拟主机文件中:
ServerAdmin admin@localhost
ServerName www.example.com
ServerAlias deanhm.com
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://www.example.com:8081/file-upload/upload
ProxyPassReverse / http://www.example.com:8081/file-upload/upload
要求全部授予
我在Tomcat的server.xml中添加了以下内容
**<Connector connectionTimeout="20000" port="8081" protocol="HTTP/1.1" redirectPort="8443"
scheme="http" proxyName="deanhm.com" proxyPort="80"/>**
我无法访问的控制器方法示例(最简单):注意调试时我不输入方法。
@RequestMapping(method = RequestMethod.POST, value = "/logout")
public String handleLogout(HttpSession session, HttpServletRequest request) {
session.setAttribute("LogoutMessage", "You have logged out!");
System.out.println(" My id is " + request.getHeader("HTTP_UID"));
try {
logoutOpenAM(request);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "logout";
}
调用注销的html页面代码段为:
<div>
<form method="POST" action="/file-upload/logout">
<input type="submit" value="Logout" />
</form>
</div>
目前我正在通过Eclipse运行我的Tomcat。
以下是我的POM文件的摘录。
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
感谢您的帮助。