这不是重复的 referenced question ,因为它是 Spring 特定的。谁添加了这个(事实发生后 3 年!)并没有费心阅读问题或评论线程来查看真正的答案是什么。接受的答案并不完全是答案,但答案的作者从未像我要求的那样回来并对其进行编辑。
鉴于下面的 restful 方法,Spring 3.1 给出了 400 错误“客户端发送的请求在语法上不正确 ()”。当 token
参数包含 URL 编码的斜杠 (%2F) 时,例如“https://somewhere.com/ws/stuff/lookup/resourceId/287559/token/R4o6lI%2FbBx43/userName/jim” 没有 %2F 一切正常。第 3 方已经在调用此服务(当然!)所以我无法更改他们发送的内容,至少在短期内是这样。有关如何在服务器端解决此问题的任何想法?
这个问题在这里描述得很好https://jira.springsource.org/browse/SPR-8662虽然这个问题与我没有使用的 UriTemplate 有关。
@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 不接受斜线,但是
-Dcom.sun.grizzly.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
解决了这个问题。
asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-2.http.encoded-slash-enabled=true
似乎没有帮助。
最佳答案
对于spring-boot,下面的技巧
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) throws Exception {
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
SpringApplication.run(Application.class, args);
}
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
关于spring - 带有 Spring RequestMapping 路径参数的编码斜杠 (%2F) 提供 HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13482020/