【问题标题】:java rest jersey redirection for oauth2 not workingoauth2的java rest jersey重定向不起作用
【发布时间】:2014-07-16 15:22:50
【问题描述】:

我正在编写一个带有 jersey 2 的 java rest 模块,其第一步是从外部 Oauth2 服务器获取用户凭据。当我尝试将用户重定向到身份验证服务时,我看到 url 已正确生成,但它仍然存在。这是我正在执行的代码:

  @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 ...
  }

如果我将生成的 url 写入屏幕它可以完美运行,但是temporaryRedirect 命令什么也没做(显然)。

我做错了什么?我是不是忘记了什么?这段代码几乎是直接从这个论坛中提出的类似问题中复制而来的,但我无法让它工作。

【问题讨论】:

  • 你确定你的 if 条件匹配吗?我怀疑。
  • redurl 被构造(我尝试返回值),所以我认为它确实如此。

标签: java rest oauth-2.0 jersey jersey-2.0


【解决方案1】:

这次很简单,但花了很长时间才弄清楚。

函数的输出不应该是String,而是Response:

 public String inOk(

重定向命令应如下所示:

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

现在它可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2014-05-11
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多