• OAuth2.0客户端基于oltu搭建


    之前提到了cas-server这个项目其本身是支持OAuth2.0协议的,因此我们可以简单搭建一个OAuth客户端,本次使用到的是Apache的oltu

    oltu介绍

    Apache Oltu 是一个开源的 OAuth 库,用于帮助开发者实现 OAuth 1.0 和 OAuth 2.0 协议。OAuth 是一种用于授权和认证的协议,常用于保护和控制对 Web 资源的访问。Oltu 项目最初由 Apache 基金会发起,目的是提供一个易于使用的 Java 库,帮助开发者构建 OAuth 认证和授权的功能。简单来讲它能够帮助我们更快捷的实现OAuth协议

    oltu既能够实现客户端又能够实现服务端,本次我们只需要构建客户端即可

    依赖

    1. <dependency>
    2. <groupId>org.apache.oltu.oauth2groupId>
    3. <artifactId>org.apache.oltu.oauth2.clientartifactId>
    4. <version>1.0.2version>
    5. dependency>

    测试代码

    oltu说起来其实也不复杂,他内部封装了一个httpclient,简单来说我们完全可以通过httpclient来实现OAuth2.0客户端,不过oltu已经帮我们将该有的调用格式都封装好了,此处测试使用了url重定向,这么做是为了方便测试,需要将其加入到拦截其中

    1. /**
    2. * 获取授权的控制器
    3. */
    4. @Controller
    5. @RequestMapping("/client")
    6. @Slf4j
    7. public class GetAuthorizationController {
    8. private static String CLIENT_ID = "20210903";
    9. private static String CLIENT_SECRET = "cas123456";
    10. private static String CODE_URL = "oauth2.0/authorize";
    11. private static String RESPONSE_TYPE = "code";
    12. /*认证服务器地址*/
    13. private static String AUTH_SERVER_URL = "https://cas.test.com:8443/cas/oauth2.0/accessToken";
    14. /*资源拥有者地址*/
    15. private static String RESOURCE_OWNER_URL = "https://cas.test.com:8443/cas/";
    16. /*资源服务器地址*/
    17. private static String RESOURCE_SERVER_URL = "https://cas.test.com:8443/cas/oauth2.0/profile";
    18. /*授权码回调地址*/
    19. private static String CALLBACKCODE = "https://localhost:8082/client/callbackCode";
    20. /*获取资源地址*/
    21. private static String GETRESOURCE = "https://localhost:8082/client/getResource";
    22. /**
    23. * 客户向资源所有者获取授权码
    24. */
    25. @GetMapping("/getCode")
    26. public String getCode() throws OAuthSystemException {
    27. OAuthClientRequest oAuthClientRequest = OAuthClientRequest
    28. .authorizationLocation(CODE_URL)
    29. .setClientId(CLIENT_ID)
    30. .setResponseType(RESPONSE_TYPE)
    31. .setRedirectURI(CALLBACKCODE)
    32. .buildQueryMessage();
    33. String uriString = oAuthClientRequest.getLocationUri();
    34. //重定向到资源所有者,获取验证码
    35. return "redirect:" + RESOURCE_OWNER_URL + uriString;
    36. }
    37. /**
    38. * 1. 资源所有者在生成授权码之后,会回调该接口将授权码传回给客户
    39. * 2. 客户获取授权码之后,使用该接口向认证服务器申请令牌
    40. */
    41. @GetMapping("/callbackCode")
    42. public String callbackCode(HttpServletRequest request) throws OAuthSystemException, OAuthProblemException {
    43. String code = request.getParameter("code");
    44. log.info(" --- 从资源拥有者获取code: {} -----------", code);
    45. //code需要非对称加密
    46. OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    47. OAuthClientRequest tokenRequest = OAuthClientRequest
    48. .tokenLocation(AUTH_SERVER_URL)
    49. .setClientId(CLIENT_ID)
    50. .setClientSecret(CLIENT_SECRET)
    51. .setGrantType(GrantType.AUTHORIZATION_CODE)
    52. .setCode(code)
    53. .setRedirectURI(GETRESOURCE)
    54. .buildQueryMessage();
    55. //通过Code,向认证服务器申请令牌
    56. OAuthResourceResponse resourceResponse = oAuthClient.resource(tokenRequest, OAuth.HttpMethod.GET,OAuthResourceResponse.class);
    57. String accessToken = resourceResponse.getBody();
    58. //获取令牌
    59. log.info("客户获取的访问令牌:" + accessToken);
    60. return "redirect:" + GETRESOURCE + "?" + accessToken;
    61. }
    62. /**
    63. * 使用令牌获取资源服务器中的数据
    64. */
    65. @GetMapping("/getResource")
    66. @ResponseBody
    67. public String getResource(String access_token) throws OAuthSystemException, OAuthProblemException {
    68. log.info("客户使用令牌向资源服务器获取数据 token = " + access_token);
    69. OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    70. OAuthClientRequest userInfoRequest =
    71. new OAuthBearerClientRequest(RESOURCE_SERVER_URL)
    72. .setAccessToken(access_token)
    73. .buildQueryMessage();
    74. OAuthResourceResponse resourceResponse = oAuthClient.resource(userInfoRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
    75. String userInfo = resourceResponse.getBody();
    76. log.info("客户获取的资源服务器的数据: " + userInfo);
    77. return userInfo;
    78. }
    79. }

    测试

    首先请求https://localhost:8082/client/getCode

     此时我们并没有登录,所以会跳转到我们之前文章中CAS的登录页面

     输入用户名密码后,会进行两次重定向,最终返回所有的用户信息

  • 相关阅读:
    海豚调度器集群安装DolphinScheduler(3.1.8)
    steam搬砖项目月入过万靠谱吗
    数据结构-树进阶刷题
    基于PTP的同步时钟同步
    DPD(Digital Pre-Distortion,数字预失真)
    JAVA获取30天或某段范围日期的方法
    【系统学习】WRF-DA资料同化
    从零学算法289
    逆向-beginners之C++继承
    开源让这位 00 后逆袭成为各类大奖收割者
  • 原文地址:https://blog.csdn.net/winerpro/article/details/133266414