jordi
jordi

Reputation: 1187

java rest jersey redirection for oauth2 not working

I am writing a java rest module, with jersey 2, whose first step is to get user credentials from an external Oauth2 server. When I try to redirect the user to the authentications service I see that url is correctly generated but It stays in the place. This is the code i am executing:

  @GET
  public String inOk(
          @DefaultValue("") @QueryParam("code") String inoauthcode)
  {
      String oauthserv = "https://xxx";
      String oauthclientid = "xxx";
      String oauthsecret = "xxxx";
      String oauthredirect = "http://myapp/url";
      String oauthurl = "xxxx"; 
      String redurl = "http";
      String authurl = "http";

        if (inoauthcode.equals("")) {
            redurl = oauthserv + "?response_type=code&client_id=" + oauthclientid + "&redirect_uri=" + oauthredirect;
            URI uri = UriBuilder.fromUri(redurl).build();
            Response.temporaryRedirect(uri);
      } 
        authurl = oauthurl + inoauthcode +  "&redirect_uri=" + oauthredirect + "&client_id=" + oauthclientid + "&client_secret=" + oauthsecret;
     ...REST OF CODE ...
  }

If I write the generated url to the screen It works perfectly but the temporaryRedirect command is doing nothing (aparently).

What am I doing wrong? Did I forget something? This code is almost directly copied from similar questions asked in this forum, but I can't get it to work.

Upvotes: 0

Views: 244

Answers (1)

jordi
jordi

Reputation: 1187

It was an easy one this time, but took long to figure out.

The output of the function should not be a String but a Response:

 public String inOk(

The redirection command should look like this:

return Response.seeOther(uri).build();

And now it works.

Upvotes: 1

Related Questions