使用Spring UriComponentsBuilder进行URL编码

我正在尝试使用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;

}

清楚地允许使用’:’和’/’字符,而口香糖则不允许。它一定在做其他类型的编码,尽管对于我一生来说,我无法想象。我在这里树错树了吗?

谢谢

回答:

UriComponentsBuilder按照RFC 3986对URI进行编码,其中关于URI的“查询”部分的第3.4节要特别注意。

在“查询”组件中,允许使用字符“ /”和“:”,并且不需要转义。

以’/’字符为例:’query’组件(明显由未转义的’?’和(可选)’#’字符分隔)不是分层的,’/’字符没有特殊含义。因此,它不需要编码。

以上是 使用Spring UriComponentsBuilder进行URL编码 的全部内容, 来源链接: utcz.com/qa/436059.html

回到顶部