Java - 使用授权码 OAuth 2 完整的令牌流程

5

我是新手,对安全和JAVA不熟悉,需要实现OAuth2的令牌跟踪,请参考下列流程(如果有可以帮助的库就更好了):

http://tutorials.jenkov.com/oauth2/authorization-code-request-response.html

我想用JAVA实现它,想使用一些提供此功能的库。该令牌流应该针对UAA,但任何其他类似的示例都将非常有帮助。

我找到了这个示例,但不确定如何使用/测试它与UAA E2E。Postman将有助于模拟它......

https://developers.google.com/api-client-library/java/google-oauth-java-client/oauth2

UAA上下文

https://github.com/cloudfoundry/uaa

4个回答

4

我建议你使用Spring作为在Java中构建Web应用程序的最受欢迎的框架。它有Spring Security模块,可以方便地开发OAuth 2.0客户端和资源服务器,如此处此处所示。


感谢您的回答,我需要的是逐步的代码解决方案,以及如何进行测试?这就是为什么我设置了奖励的原因 :) - John Jerrby
OAuth 2.0有4种不同的流程。你需要哪一种流程?UAA附带支持授权码流程的示例应用程序 - 你需要设置指南吗? - Danylo Zatorsky
我需要使用uaa支持这个流程http://tutorials.jenkov.com/oauth2/authorization-code-request-response.html...谢谢 - John Jerrby
那么我应该如何编写代码来实现它呢?您能否提供一些示例和您提供的链接? - John Jerrby

1
这是Google API客户端库的示例。如果有帮助,请尝试。
    public class ServletSample extends AbstractAuthorizationCodeServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    // do stuff
  }

  @Override
  protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
    GenericUrl url = new GenericUrl(req.getRequestURL().toString());
    url.setRawPath("/oauth2callback");
    return url.build();
  }

  @Override
  protected AuthorizationCodeFlow initializeFlow() throws IOException {
    return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
        new NetHttpTransport(),
        new JacksonFactory(),
        new GenericUrl("https://server.example.com/token"),
        new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"),
        "s6BhdRkqt3",
        "https://server.example.com/authorize").setCredentialDataStore(
            StoredCredential.getDefaultDataStore(
                new FileDataStoreFactory(new File("datastoredir"))))
        .build();
  }

  @Override
  protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
    // return user ID
  }
}

public class ServletCallbackSample extends AbstractAuthorizationCodeCallbackServlet {

  @Override
  protected void onSuccess(HttpServletRequest req, HttpServletResponse resp, Credential credential)
      throws ServletException, IOException {
    resp.sendRedirect("/");
  }

  @Override
  protected void onError(
      HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse)
      throws ServletException, IOException {
    // handle error
  }

  @Override
  protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
    GenericUrl url = new GenericUrl(req.getRequestURL().toString());
    url.setRawPath("/oauth2callback");
    return url.build();
  }

  @Override
  protected AuthorizationCodeFlow initializeFlow() throws IOException {
    return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
        new NetHttpTransport(),
        new JacksonFactory(),
        new GenericUrl("https://server.example.com/token"),
        new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"),
        "s6BhdRkqt3",
        "https://server.example.com/authorize").setCredentialDataStore(
            StoredCredential.getDefaultDataStore(
                new FileDataStoreFactory(new File("datastoredir"))))
        .build();
  }

  @Override
  protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
    // return user ID
  }
}

1

1

如果您想详细了解OAuth 2.0流程,请访问 RFC 6749规范。 如果您想了解逐步解决方案,可以查看一些教程,例如此文章介绍如何使用OAuth 2.0创建Spring REST API。 本文将介绍代码和创建Postman请求。 关于模拟/测试,我以前使用TestNG和Mockito为OAuth 2.0创建了一个测试套件。

您越开发和研究,就会找到改进或更改设计代码的方法。 也就是说,如果您真的想遵守OAuth 2.0流程,您应该正确理解RFC 6749链接中有时可能相对模糊的流程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接