我正在尝试从OAuth2授权代码流的请求中生成重定向URI。该请求包含主机名以及范围和授权代码。例如:
redirect_uri=myapp2://oauth2redirect&scope=email+profile
所以,这里的主机名是:myapp2://oauth2redirect
现在,当我执行以下代码为应用程序生成重定向uri时,它会添加额外的" /" (斜杠)最后而不是继续查询参数,即:
myapp2://oauth2redirect/?code=abcdefghijkl
额外/不需要" /" myapp2://oauth2redirect/?
中的重定向失败。
理想情况下应该是:myapp2://oauth2redirect?code=abcdefghijkl&scope=
public Response getResponse() {
String uri = oAuthrequest.getRedirectURI();
UriBuilder uriBuilder = UriBuilder.fromUri(uri)
.queryParam("code", code);
if (oAuthrequest.getState() != null) {
uriBuilder.queryParam("state", oAuthrequest.getState());
}
if(scopeNames != null && scopeNames.size() > 0) {
uriBuilder.queryParam("scope", StringUtil.toSingleString(scopeNames, " "));
}
logger.info("OAuth2 Authorization response success");
return Response.status(302).cookie(cookies).location(uriBuilder.build()).build();
}
我认为UriBuilder.fromUri(uri)方法会增加额外的" /"在uri中我调试并检查了String字段的值" uri"是正确的。但是,一旦这条线被执行,它就会增加额外的" /"在uri之后,然后通过附加查询参数继续。
答案 0 :(得分:1)
好吧,我提出了一个hacky解决方案:假设getRedirectURI()
将返回类似"myapp2://oauth2redirect"
// builds an URI object from the URI string
java.net.URI uri = java.net.URI.create(getRedirectURI());
// uses the authory(oauth2redirect) as the path to build the uri
UriBuilder uriBuilder = UriBuilder.fromPath(
// forcefully adds the double slashes (//), without this,
// at this point, the uri would be: myapp2:oauth2redirect
"//" + uri.getAuthority())
.scheme(uri.getScheme());
uriBuilder.queryParam("code", "myCode");
uriBuilder.build(); // produces the uri: myapp2://oauth2redirect?code=myCode