java 如何使用 spring boot 抑制 url 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40348836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress url encoding with spring boot
提问by user5423592
I have created a GET/POST API using Spring boot which has a http url parameter say refid. Now this parameter is already encoded before invoking GET/POST request e.g. http://localhost:8080/users/TESTFNkJXiQAH%2FJBKxigBx
我使用 Spring boot 创建了一个 GET/POST API,它有一个 http url 参数,比如 refid。现在这个参数在调用 GET/POST 请求之前已经被编码,例如http://localhost:8080/users/TESTFNkJXiQAH%2FJBKxigBx
But, when I deploy this through Spring Boot, the encoded refid is encoded again and the refid changes. i.e. it becomes:
但是,当我通过 Spring Boot 部署它时,编码的 refid 会再次编码并且 refid 发生变化。即它变成:
http://localhost:8080/users/TESTFNkJXiQAH%252FJBKxigBx
http://localhost:8080/users/TESTFNkJXiQAH%252FJBKxigBx
I want to suppress this 2nd encoding by Spring boot. Can anyone advise here?
我想通过 Spring Boot 抑制第二个编码。有人可以在这里建议吗?
回答by Alex P.
Don't know if you are still having this problem or you found out why it's happening, but because I was trying to explain to someone the phenomenon, I looked if there is already a good explanation. But since you also ask and I didn't find any, here is my answer.
不知道您是否仍然遇到这个问题,或者您是否找到了它发生的原因,但是因为我试图向某人解释该现象,所以我查看了是否已经有一个很好的解释。但既然你也问了,我没有找到,这是我的答案。
So you encode your refid
所以你编码你的 refid
TESTFNkJXiQAH%2FJBKxigBx
TESTFNkJXiQAH%2FJBKxigBx
before you send it through the url, which then you give into a browser. Now this is only the encoded refid. When you call it through a URL directly you have to encode it again, according to the HTML URL encoding standards. That's why the double escape. Also read this. E.g. so if your refid looks like this
在您通过 url 发送它之前,然后您将其提供给浏览器。现在这只是编码的 refid。当您直接通过 URL 调用它时,您必须根据 HTML URL 编码标准再次对其进行编码。这就是双重逃生的原因。也读这个。例如,如果您的 refid 看起来像这样
test%123
测试%123
and you encode it you turn it into
你编码它你把它变成
test%25123
测试%25123
now if you also want to pass it through a url on the browser you'd have to encode it again.
现在,如果您还想通过浏览器上的 url 传递它,则必须再次对其进行编码。
test%2525123
测试%2525123
But if a service A is using this service and service A encodes this refid properly then you wont have this problem. It's happening only because you are trying to call this api endpoint through the browser.
但是如果服务 A 正在使用这个服务并且服务 A 正确地编码了这个 refid,那么你就不会遇到这个问题。发生这种情况只是因为您试图通过浏览器调用此 api 端点。
Of course I take for granted that you are doing this:
当然,我认为你这样做是理所当然的:
String decoded = URLDecoder.decode(refid, "UTF-8");
in your controller
在你的控制器中
回答by Neel Salpe
Pass the decoded URL in first place instead of doing inconvenient things to stop double encoding. You get already decoded field in rest controller. Example if you pass www.xyz.com?name=nilesh%20salpe
首先传递解码后的URL,而不是做不方便的事情来停止双重编码。你已经在休息控制器中解码了字段。例如,如果您通过 www.xyz.com?name=nilesh%20salpe
you will get value of param name as "nilesh salpe" and not "nilesh%20salpe"
您将获得参数名称的值为“nilesh salpe”而不是“nilesh%20salpe”
回答by Alexey Soshin
This is a basic example of URLDecoder:
这是 URLDecoder 的基本示例:
@RequestMapping(value = "/users/{refId}", method = GET)
public void yourMethod(@PathVariable("refId") String refId) {
// This is what you get in Spring Boot
String encoded = refId; //"TESTFNkJXiQAH%252FJBKxigBx"
String decoded = URLDecoder.decode(encoded, "UTF-8");
System.out.println(decoded);
// Result TESTFNkJXiQAH%2FJBKxigBx
}