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既能够实现客户端又能够实现服务端,本次我们只需要构建客户端即可

依赖

        <dependency>
            <groupId>org.apache.oltu.oauth2</groupId>
            <artifactId>org.apache.oltu.oauth2.client</artifactId>
            <version>1.0.2</version>
        </dependency>

测试代码

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

/**
 * 获取授权的控制器
 */
@Controller
@RequestMapping("/client")
@Slf4j
public class GetAuthorizationController {
 
    private static String CLIENT_ID = "20210903";
    private static String CLIENT_SECRET = "cas123456";
    private static String CODE_URL = "oauth2.0/authorize";
    private static String RESPONSE_TYPE = "code";
    /*认证服务器地址*/
    private static String AUTH_SERVER_URL = "https://cas.test.com:8443/cas/oauth2.0/accessToken";
    /*资源拥有者地址*/
    private static String RESOURCE_OWNER_URL = "https://cas.test.com:8443/cas/";
    /*资源服务器地址*/
    private static String RESOURCE_SERVER_URL = "https://cas.test.com:8443/cas/oauth2.0/profile";
    /*授权码回调地址*/
    private static String CALLBACKCODE = "https://localhost:8082/client/callbackCode";
    /*获取资源地址*/
    private static String GETRESOURCE = "https://localhost:8082/client/getResource";
 
    /**
     * 客户向资源所有者获取授权码
     */
    @GetMapping("/getCode")
    public String getCode() throws OAuthSystemException {
        OAuthClientRequest oAuthClientRequest = OAuthClientRequest
                .authorizationLocation(CODE_URL)
                .setClientId(CLIENT_ID)
                .setResponseType(RESPONSE_TYPE)
                .setRedirectURI(CALLBACKCODE)
                .buildQueryMessage();
        String uriString = oAuthClientRequest.getLocationUri();
        //重定向到资源所有者,获取验证码
        return "redirect:" + RESOURCE_OWNER_URL + uriString;
    }
 
    /**
     * 1. 资源所有者在生成授权码之后,会回调该接口将授权码传回给客户
     * 2. 客户获取授权码之后,使用该接口向认证服务器申请令牌
     */
    @GetMapping("/callbackCode")
    public String callbackCode(HttpServletRequest request) throws OAuthSystemException, OAuthProblemException {
        String code = request.getParameter("code");
        log.info(" --- 从资源拥有者获取code: {} -----------", code);
        //code需要非对称加密
        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
        OAuthClientRequest tokenRequest = OAuthClientRequest
                .tokenLocation(AUTH_SERVER_URL)
                .setClientId(CLIENT_ID)
                .setClientSecret(CLIENT_SECRET)
                .setGrantType(GrantType.AUTHORIZATION_CODE)
                .setCode(code)
                .setRedirectURI(GETRESOURCE)
                .buildQueryMessage();
        //通过Code,向认证服务器申请令牌
        OAuthResourceResponse resourceResponse = oAuthClient.resource(tokenRequest, OAuth.HttpMethod.GET,OAuthResourceResponse.class);
        String accessToken = resourceResponse.getBody();
        //获取令牌
        log.info("客户获取的访问令牌:" + accessToken);
        return "redirect:" + GETRESOURCE + "?" + accessToken;
    }
 
    /**
     * 使用令牌获取资源服务器中的数据
     */
    @GetMapping("/getResource")
    @ResponseBody
    public String getResource(String access_token) throws OAuthSystemException, OAuthProblemException {
        log.info("客户使用令牌向资源服务器获取数据 token = " + access_token);
        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
        OAuthClientRequest userInfoRequest =
                new OAuthBearerClientRequest(RESOURCE_SERVER_URL)
                        .setAccessToken(access_token)
                        .buildQueryMessage();
        OAuthResourceResponse resourceResponse = oAuthClient.resource(userInfoRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
        String userInfo = resourceResponse.getBody();
        log.info("客户获取的资源服务器的数据: " + userInfo);
        return userInfo;
    }
 
}

测试

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值