[英]What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest?
[英]HttpServletRequest getRequestURI in SpringBoot
我有一個 SpringBoot 應用程序。 在 controller 中使用此方法:
@GetMapping(value = "/")
public String home(Model model, HttpServletRequest request) {
System.out.println("home ->" + request.getRequestURI() + "<-");
}
但是當我從控制台執行時:
curl localhost:7080
curl localhost:7080/./
curl localhost:7080/../
curl localhost:7080/..
我總是得到相同的結果: home ->/<-
和瀏覽器一樣的結果。 我想為所有不是localhost:7080
的映射返回 404
我試過了:
MacBook-Pro-de-nunito:~ nunito$ curl --path--as-is localhost:7080/./
curl: option --path--as-is: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
curl --version
curl 7.64.1 (x86_64-apple-darwin20.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.41.0
Release-Date: 2019-03-27
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS GSS-API HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets
這些應該已經被阻止,因為它們包含路徑遍歷。 它們不是有效的請求。
Curl 在發送到 spring 之前正在修改它們,就像瀏覽器一樣。 您可以使用--path-as-is
標志來指示 curl 按原樣發送 url。
就像是
curl --path-as-is localhost:7080/./
執行此操作后,您應該會收到帶有RequestedRejectedException
的內部服務器錯誤。
有開放的jira默認將其更改為400 - https://github.com/spring-projects/spring-security/issues/7568
我建議從 3PP 圖書館尋求幫助。 下面的例子
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-starter</artifactId>
<version>2.4.1</version>
</dependency>
並添加一個 class ,它將在請求到達實際 Z594C18AB391A8D2C4D45902A23A8B6585703DZ 之前將正確的完整 ZE6B3902A23A8B6585703DZ 放入請求 scope 中
public class LogbookSink implements Sink {
@Override
public void write(final Precorrelation precorrelation, final HttpRequest request) throws IOException {
request.setAttribute("complete_url", request.getRequestUri());
}
@Override
public void write(final Correlation correlation, final HttpRequest request, final HttpResponse response) throws IOException {
//do something else for response
}
}
現在在 controller 中,您可以簡單地通過說
String calledUri = request.getAttribute("complete_url");
//your logic to throw 404
//if (!calledUri.matches("localhost:7080/")){
// throw new ResponseStatusException(NOT_FOUND, "Unable to find resource");
//}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.