第三方平台认证,简单实现OAuth2.0授权码协议

spring cloud zuul 如何转发 websocket 请求

    如果登录一家网站注册账号需要填写个人信息、邮箱地址、手机号,我肯定是放弃了。人手一部智能机,移动互联网的几大社交巨头APP成了我们特殊的身份证明。各种网站移动APP越来越倾向于通过第三方平台认证来简化申请和登录帐号的流程,增加用户量。下面是百度网盘使用微信扫码登录页面:

百度网盘登录页面.png

原理解析-开放授权

    开放授权(OAuth)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用。
    OAuth 2.0 是目前比较流行的做法,为了支持这些不同类型的第三方应用,提出了多种授权类型,如:
    1.授权码 (Authorization Code Grant)
    2.隐式授权 (Implicit Grant)
    3.RO凭证授权 (Resource Owner Password Credentials Grant)
    4.Client凭证授权 (Client Credentials Grant)
而授权码模式是应用最广泛、最核心、也是最复杂的,接下来的内容就是按照OAUTH 2.0 授权码模式原理,自己实现简单的第三方授权案例。
授权码模式逻辑如下图:

授权码模式.png

    1.web客户端传入它的客户端标识符、请求作用域、本地状态和一个重定向URI,然后重定向到授权服务器来发起这个流程,在访问被许可(或被拒绝)后授权服务器会重新将终端用户引导回这个URI。
    2.授权服务器验证终端用户,并确定终端用户是否许可客户端的访问请求。
    3.授权服务器重定向到之前提供的重定向URI上去。授权服务器为客户端传回一个授权码做获取访问令牌之用。
    4.客户端通过验证并传入上一步取得的授权码从授权服务器请求一个访问令牌。(需要带上ClientId和Secret,ClientId和Secret是通过平台授予)。
    5.授权服务器验证客户端私有证书和授权码的有效性并返回访问令牌。

代码实现

基于sping boot 框架开发,使用maven创建两个代码工程,授权服务和客户端服务(授权应用)。
1.客户端服务器带上请求参数,重定向到授权服务器发起流程。代码如下:

    /**
   * 重定向到 授权服务 授权页
   * @param request
   * @return redirect:
   */
  @GetMapping("/redirectauthpage")
  public String clientLogin(HttpServletRequest request) {
    HttpSession session = request.getSession();
    String state = "state01";
    //将state 放入session 中,用户回调时的安全校验
    session.setAttribute("state", state);
    // 获取授权码成功后回调的地址
    String redirectUrl = "http://127.0.0.1:8081/ms-consumer/callbackcode";
    // 采用了code的响应方式
    String response_type = "code";
    // 拼接请求路径
    String loginurl = asUrl + "/oauth2/authorize";
    
    StringBuffer params = new StringBuffer();
    params.append("client_id=").append(clientId);
    params.append("&redirect_uri=").append(redirectUrl);
    params.append("&response_type=").append(response_type);
    params.append("&state=").append(state);
    return "redirect:" + loginurl + "?" + params.toString();
  }

2.授权服务器校验请求参数,返回授权登录界面。代码如下:

   /**
   * 返回用户授权页面
   * @param request
   * @param session
   * @param modelAndView
   * @return
   */
  @GetMapping("/oauth2/authorize")
  public String loginPage(HttpServletRequest request,HttpSession session,ModelAndView modelAndView) {
    try {
      String clientId =  request.getParameter("client_id");
      String redirectUri =  request.getParameter("redirect_uri");
      String responseType =  request.getParameter("response_type");
      String state =  request.getParameter("state");
      //校验参数 client_id redirect_uri,
      //这里不贴校验代码,根据自身情况去缓存数据再校验       
      //validateParams() 
      
      //将客户端信息保存到session 中
      session.setAttribute("state", state);
      session.setAttribute("clientId", clientId);
      session.setAttribute("redirectUri", redirectUri);
      session.setAttribute("sessionId", session.getId());
    } catch (Exception e) {
      return "error";   //返回error 页面
    }
    return "loginpage";  //返回用户授权页面
  }

  2.1 用户授权页面代码如下:

 <label>authorization server</label>
  <form id="formId" method = "post">
    用户:<input type="text" name ="userName" id="userId"/> </br>
    密码:<input type="password" name = "pwd" id = "pwdid"/> </br>
    <input type="button" value="提交数据" onclick="login()"/> 
  </form>
  <input id="stateid" type="hidden" value="" />
  <script type="text/javascript">
    _initPage();
    function _initPage() {
      $.ajax({
        type : "GET",
        //async: false,
        url : "/ms-producter/loginpage/state",
        //data :, // 请求参数
        contentType: "application/json;charset=UTF-8",
        dataType : "text", // 服务器响应的数据类型
        success : function(data) {
          document.getElementById("stateid").value = data;
        }
      });
    }
    function login() {
      var username = document.getElementById("userId").value;
      var pwd = document.getElementById("pwdid").value;
      if (username == "" || pwd == "") {
        alert("用户名或密码不为空");
        return false;
      }formId
      var form = document.getElementById("formId");
      form.action = "http://127.0.0.1:8083/ms-producter/login?" +"state=" + document.getElementById("stateid").value;
      form.submit();
    }

3.验证用户授权成功后,授权服务器重定向到之前提供的重定向URI,并在URI上传回一个授权码。代码如下:

/**
   * 验证用户授权信息,并重定向到客户端提供的重定向URI
   * @param request
   * @param httpSession
   * @param response
   * @return redirectURI:http://127.0.0.1:8081/ms-consumer/callbackcode
   */
  @PostMapping("/login")
  public Object login(HttpServletRequest request,HttpSession httpSession,HttpServletResponse response) {
    try {
      //验证state 
      String state =  request.getParameter("state");
      if(!state.equals(httpSession.getAttribute("state"))) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        return modelAndView;
      }
      //验证用户名密码的有效性,验证方法根据项目实际情况编码,这里就不贴代码了
      String userName =  request.getParameter("userName");
      String pwd =  request.getParameter("pwd");
      // validateUserInfo() 
      
      //生成授权码重定向到指定地址
      StringBuffer redirectURI = new StringBuffer();
      redirectURI.append("redirect:").append(httpSession.getAttribute("redirectUri"));
      redirectURI.append("?code=").append(System.currentTimeMillis());
      redirectURI.append("&state=").append(state);
      return redirectURI.toString();
    } catch (Exception e) {
      ModelAndView modelAndView = new ModelAndView();
      modelAndView.setViewName("error");
      return modelAndView;
    }
  }

4.客户端服务器取得上一步传入的授权码,缓存从授权服务器请求的访问令牌后,向前端返回等待页面并驱动首页自动更新(等待页面代码在后面),代码如下:

/**
   * 客户端服务器重定向方法
   * @param request
   * @param session
   * @param modelAndView
   * @return
   */
  @GetMapping("/callbackcode")
  public Object getToken(HttpServletRequest request,HttpSession session,ModelAndView modelAndView) {
    String state = request.getParameter("state");
    String code = request.getParameter("code");
    String redirectUrl = "";
    //判断state
    if(!state.equals(session.getAttribute("state"))) {
      modelAndView.setViewName("error");
      return modelAndView;
    }
    /**
     * 根据授权码获取token 
     */
    String accessToken = null;
    String refreshToken;
    int expiresIn;
    try {
      HttpHeaders headers = new HttpHeaders();
      MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
      // 填入请求参数
      params.add("client_id", clientId);
      params.add("redirect_uri", redirectUrl);
      params.add("grant_type", "authorization_code");
      params.add("client_secret", clientSecret);  //第三方平台获取
      params.add("code", code);
      //发出获取token的请求
      String accessTokenUrl = asUrl + "/oauth2/accessToken";
      String result = restTemplate.postForEntity(accessTokenUrl, new HttpEntity<>(params, headers), String.class).getBody();
      JSONObject jsonObject = JSONObject.parseObject(result);
      accessToken = jsonObject.get("access_token").toString();
      expiresIn = jsonObject.getInteger("expires_in");
      refreshToken = jsonObject.get("refresh_token").toString();
      //updateTokenCache() 客户端服务器缓存token,在发起请请求时使用,不贴代码
    } catch (Exception e) {
      modelAndView.setViewName("error");
      return modelAndView;
    }
    //带上accessToken获取用户信息
    String userInfo;
    try {
      String userInfoUrl = asUrl + "/userinfo";
      HttpHeaders httpheader = new HttpHeaders();
      httpheader.add("token", accessToken);
      httpheader.setContentType(MediaType.APPLICATION_JSON);
      userInfo = restTemplate.postForEntity(userInfoUrl, new HttpEntity<>(httpheader), String.class).getBody();
    } catch (Exception e) {
      modelAndView.addObject("error", "request userinfo by token exception !");
      modelAndView.setViewName("error");
      return modelAndView;
    }
    modelAndView.setViewName("wait");
    return modelAndView;
  }

5.授权服务器验证平台发放的客户端密钥和授权码的有效性,校验成功后返回访问令牌。代码如下:

/**
   * 生成token
   * @param request
   * @return
   */
  @RequestMapping("/oauth2/accessToken")
  @ResponseBody
  public Object getAccessToken(HttpServletRequest request) {
    String clientId = request.getParameter("client_id");
    String redirectURI = request.getParameter("redirect_uri");
    String grantType = request.getParameter("grant_type");
    String clientSecret = request.getParameter("client_secret");
    String code = request.getParameter("code");
    JSONObject jsonObject = new JSONObject();
    if(grantType.equals("authorization_code")) {//授权码格式
      //校验重定向地址 redirect_uri
      //validateRedirectURI() 平台授权时可以缓存客户端的申请信息
      //校验 授权码
      //validateAuthCode() 这里不贴校验代码了,按照你项目组加密算法来
      
      //返回token
      //生成token建议使用JWT的方式,这里就不讨论加密算法了,不是本章的重点
      jsonObject.put("access_token",System.currentTimeMillis());
      jsonObject.put("expires_in",System.currentTimeMillis());
      jsonObject.put("refresh_token",System.currentTimeMillis());
      return jsonObject;
    }else { 
      //这里可扩展其它的授权类型
    }
    return jsonObject;
  }

后台代码到此结束,简单吧。

前端代码

     客户端服务器取得上一步传入的授权码,缓存从授权服务器请求的访问令牌后,向前端返回等待页面并驱动首页自动更新。 前端如何自动更新,返回的等待页面和首页的监听是重点,
    等待页面会在加载完成之后等待设定的时间自动关闭,代码如下:

<body>
  <label>正在验证中......</label>
  <!-- 定时关闭窗口 -->
  <script type="text/javascript">
    window.onload = function() {
      setInterval(function() {
        window.close()
      }, 1000);
    }
</script>
</body>

    首页发起授权流程页面,代码如下:

<script type="text/javascript">
  function _btnCode() {
    //创建弹出窗页面,发起授权请求
    var winObj = window.open("http://127.0.0.1:8081/ms-consumer/redirectauthpage","top", 'height=400, width=400, top=100, left=400, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=false')
    //监听弹出窗口页面关闭事件,
    var loop = setInterval(function() {
          if(winObj.closed) {
             location.href = "http://127.0.0.1:8081/ms-consumer/portal.html";
          }
    }, 1000); 
  }
</script>

代码工程展示

image.png

    提示,spring boot 使用ModelAndView 返回HTML页面需要添加如下配置文件:

spring:
  mvc: 
    view:
      prefix: /
      suffix: .html

演示效果
简书的编辑器不知道怎么放入录好的视频,如何你想看结果演示,找我要链接。

最后感谢参考的以下博客
https://www.jianshu.com/p/a047176d9d65?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容