我正在尝试使用 spring 的 UriComponentsBuilder 为 oauth 交互生成一些 url。查询参数包括回调 url 和参数值等实体,其中包含空格。
尝试使用 UriComponentBuilder(因为 UriUtils 现在已弃用)
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(oauthURL);
urlBuilder.queryParam("client_id", clientId);
urlBuilder.queryParam("redirect_uri", redirectURI);
urlBuilder.queryParam("scope", "test1 test2");
String url = urlBuilder.build(false).encode().toUriString();
不幸的是,虽然 scope 参数中的空格已成功替换为“+”,但 redirect_uri 参数根本没有进行 url 编码。
例如,
redirect_uri=https://oauth2-login-demo.appspot.com/code
应该结束了
redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Fcode
但没有受到影响。深入研究代码,特别是 org.springframework.web.util.HierarchicalUriComponents.Type.QUERY_PARAM.isAllowed(c) :
if ('=' == c || '+' == c || '&' == c) {
return false;
}
else {
return isPchar(c) || '/' == c || '?' == c;
}
显然允许 ':' 和 '/' 字符,而口香糖不应该。它一定是在做一些其他类型的编码,虽然对于我的生活,我无法想象是什么。我在这里吠叫错误的树吗?
谢谢