带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 给出 HTTP 400 [英] Encoded slash (%2F) with Spring RequestMapping path param gives HTTP 400

查看:35
本文介绍了带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 给出 HTTP 400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是重复的参考问题,因为它是特定于 Spring 的.谁添加了(事实发生 3 年后!)并没有费心阅读问题或评论线程以了解真正的答案是什么.接受的答案并不完全是答案,但答案的作者从未像我要求的那样回来编辑它.

This is not a duplicate referenced question, because it is Spring specific. Whoever added that (3 years after the fact!) didn't bother to read the question or comment thread to see what the real answer was. The accepted answer isn't quite the answer, but the author of the answer never came back and edited it like I asked.

鉴于下面的restful方法,Spring 3.1给出了一个400错误客户端发送的请求在语法上不正确()".当 token 参数包含 URL 编码的斜杠 (%2F) 时,例如https://somewhere.com/ws/stuff/lookup/resourceId/287559/token/R4o6lI%2FbBx43/userName/jim" 没有 %2F 一切工作正常.第 3 方已经在调用此服务(当然!)所以我无法更改他们发送的内容,至少在短期内是这样.关于如何在服务器端解决这个问题的任何想法?

Given the restful method below, Spring 3.1 gives a 400 error with "The request sent by the client was syntactically incorrect ()." when the token parameter contains a URL encoded slash (%2F), for example "https://somewhere.com/ws/stuff/lookup/resourceId/287559/token/R4o6lI%2FbBx43/userName/jim" Without the %2F everything works fine. A 3rd party is already calling this service (of course!) so I can't change what they send, in the short term at least. Any ideas on how to work around this on the server side?

这里很好地描述了这个问题https://jira.springsource.org/browse/SPR-8662 尽管该问题与 UriTemplate 相关,但我可以分辨出我没有使用过.

This problem is described very well here https://jira.springsource.org/browse/SPR-8662 though that issue is related to UriTemplate which I am not using that I can tell.

@RequestMapping("/ws/stuff/**")
@Controller
public class StuffController {
  @RequestMapping(value = "/ws/stuff/lookup/resourceId/{resourceId}/token/{token}/userName/{userName}", method = RequestMethod.GET)
   public @ResponseBody
   String provisionResource(@PathVariable("resourceId") String resourceId, @PathVariable("token") String token, @PathVariable("userName") String userName, ModelMap modelMap,
         HttpServletRequest request, HttpServletResponse response) {
      return handle(resourceId, userName, request, token, modelMap);
   }
}

注意:这是在 Glassfish 3.1.2 上,起初是 Grizzly/Glassfish 不接受斜线,但是

Note: This is on Glassfish 3.1.2, and at first it was Grizzly/Glassfish not accepting the slash, but

-Dcom.sun.grizzly.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

解决了这个问题.

asadmin 设置 configs.config.server-config.network-config.protocols.protocol.http-listener-2.http.encoded-slash-enabled=true

似乎没有帮助.

推荐答案

这可能就是你的答案:urlencoded 正斜杠正在破坏 URL

我建议不要将其放在路径中,而是将其移至请求参数.

I would suggest not putting that in the path, move it to a request param instead.

解决办法:

您可以将 RequestMapping 更改为

You could change the RequestMapping to

@RequestMapping(value = "/ws/stuff/lookup/resourceId/**", method = RequestMethod.GET) 

然后从请求对象中手动解析路径变量.

and then parse the path variables manually from the request object.

这篇关于带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 给出 HTTP 400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆