Java - 带有代码的令牌流 OAuth 2 E2E



我是安全和JAVA的新手,我需要实现OAuth2的令牌跟随,这是我需要实现的确切流程(如果有一些库可以帮助它很棒(

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

我如何使用 JAVA 实现它,我想使用一些提供此功能的库。 令牌流应该针对 UAA,但任何其他类似示例都将非常有帮助。我找到了这个例子,但不确定如何使用/测试它 E2E 与 UAA邮递员将非常有助于模拟它...

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

UAA 上下文

https://github.com/cloudfoundry/uaa

我建议你把Spring作为在Java中构建Web应用程序的最流行的框架。它具有 Spring 安全模块,可以促进开发 OAuth 2.0 客户端和资源服务器,如下所示或此处。

有关 OAuth 2.0 流程的详细说明,请访问 RFC 6749 规范。 关于分步解决方案,您应该看到一些教程,例如本文,介绍如何使用 OAuth 2.0 创建 Spring REST API。 本文将介绍代码以及创建邮递员请求。关于模拟/测试,我之前使用 TestNG 和 Mockito 为 OAuth 2.0 创建了一个测试套件。

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

下面是 Google API clinet 库示例。如果有帮助,请尝试此操作

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

https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2 包含使用 Spring 安全性执行 OAuth2 的示例代码。

最新更新