【问题标题】:Java - Token flow OAuth 2 E2E with codeJava - 带有代码的令牌流 OAuth 2 E2E
【发布时间】:2017-11-18 10:08:58
【问题描述】:

我是安全和 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

【问题讨论】:

    标签: java spring-security oauth-2.0 jwt cloud-foundry


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        有关 OAuth 2.0 流程的详细说明,请访问RFC 6749 Specification。关于一步一步的解决方案,你应该看一些教程,比如this article explaining how to create a Spring REST API using OAuth 2.0。本文介绍了代码以及创建 Postman 请求。关于模拟/测试,我之前使用 TestNG 和 Mockito 为 OAuth 2.0 创建了一个测试套件。

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

        【讨论】:

          【解决方案4】:

          我建议您将 Spring 作为用 Java 构建 Web 应用程序的最流行的框架。它具有 Spring Security 模块,可以方便地开发 OAuth 2.0 客户端以及资源服务器,如 here 或 here 所示。

          【讨论】:

          • 感谢您的回答,我需要的是一步一步的代码解决方案,以及如何测试它?这就是我放赏金的原因:)
          • OAuth 2.0 中有 4 种不同的流程。您需要哪种流量? UAA 附带支持授权代码流的示例应用程序 - 您是否需要指导如何设置它?
          • 我需要支持这个流程 tutorials.jenkov.com/oauth2/… ,使用 uaa ....谢谢
          • 那么我应该如何编写代码来实现它,你能提供你提供的链接的例子吗?
          猜你喜欢
          • 2014-08-25
          • 2017-11-03
          • 2020-12-11
          • 2014-10-05
          • 2013-06-08
          • 2013-08-17
          • 2011-08-05
          • 2019-11-02
          • 2021-09-19
          相关资源
          最近更新 更多