2017-06-28 13 views
0

OAuth2認証コードフローのリクエストからリダイレクトURIを生成しようとしています。要求には、スコープと認証コードと共にホスト名が含まれます。例は:javax.ws.rs.core.UriBuilder、fromUri()メソッドがホスト名URIの最後に余分な "/"を追加するのはなぜですか?

redirect_uri=myapp2://oauth2redirect&scope=email+profile 

ので、ホスト名はここにある:myapp2://oauth2redirect

さて、私は、アプリケーションのリダイレクトURIを生成するために、次のコードを実行したときに、それが最後に余分な「/」(スラッシュ)を追加します代わりに、クエリパラメータを続けるのは、IE:

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(); 
} 

私は文字列フィールド「URI」の値は、デバッグと確認してきたようにUriBuilder.fromUri(URI)メソッドは、URIに「/」の余分を追加することを考えます正しい。しかし、この行が実行されると、uriの後ろに余分な "/"が追加され、クエリパラメータを追加することによって処理が進められます。

+0

のようなものを返すことを与えられましたか? –

+0

@LeonardoPinaこれはJerseyのUriBuilder(javax.ws.rs.core.UriBuilder) –

+0

です。問題はURI:myapp2:// oauth2redirect&scope = email + profile'内にあると私は考えています。 'oauth2redirect&scope = email + profile'はURIの権限として解釈されます –

答えて

1

まあ、私はハック解決策を考え出した:getRedirectURI()"myapp2://oauth2redirect"使用しているUriBuilder

// 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 
+0

おかげでね!それは魅力のように働いた:) –

+0

@MayankSinhaこれはあなたの問題を解決した場合は、それを受け入れたとしてマークしてください。 :) –

+0

uriが= myapp2:// oauth2redirect /の場合、uriが "/"で終わるテストケースはどうでしょうか?この場合、実際には "/"が最後に削除されます。 uri –

関連する問題