zoukankan      html  css  js  c++  java
  • [PHP] 实现oauth下的单点登陆

    整体流程是 ,参照腾讯企业邮的对外开放API , 传递client_id + client_secret就可以获取到access_token , 拿着access_token+ 用户的标识 , 去请求一个登陆地址 , 获取到登陆地址直接跳转过去就能登陆成功 

    1. 首先在管理后台 , 申请生成自己的client_id  +  client_secret

    重置秘钥 , 停用秘钥都会使当前企业的旧秘钥和access_token失效

    2. 通过自定义规则生成签名串

    获取所有get传递的参数值 , 使用字典序排序 , 拼接在一起 ,拼接上自己的秘钥进行sha1加密 , 作为GET参数signature

    <?php
    //从企管后台获取到以下参数
    $client_secret="af0c7c2ac7b5687d6c4ed5e6a2a7e058780c1aac";
    $client_id="appdev.sinanet.com";
    //生成签名
    $_GET['grant_type']='client_credentials';
    $_GET['client_id']=$client_id;
    $_GET['ts']=time();//秒数时间戳
    $args = array_values($_GET);
    sort($args, SORT_STRING);
    $args = implode($args);
    $sign = sha1($args . $client_secret);
    $_GET['signature']=$sign;
    
    //最终传递的GET参数
    var_dump($_GET);
    

    3. 获取access_token接口

    重新获取token会使旧的token失效

    GET /openapi/token?

    grant_type=client_credentials&

    client_id=appdev.sinanet.com&

    ts=1587628159&

    signature=ad90ca4f8395c679243c4264bd2159dd59a0f82b  

    {
        "result": true,
        "errno": 0,
        "msg": "",
        "data": {
            "access_token": "e3ea1a05893f5906893a37c51e4458bdb7361794",
            "expire_in": 7200
        }
    }
    

    4. 通过access_token + 用户标识 获取本企业内的员工登陆地址 , 登陆地址是一次性的 , 过期时间300秒

    GET /openapi/get_login_url?

    access_token=e3ea1a05893f5906893a37c51e4458bdb7361794&

    email=shihan2@xxxxx.com 

    {
        "result": true,
        "errno": 0,
        "msg": "",
        "data": {
            "login_url": "http://xxx/cgi/openapi_login.php?ts=1587628586&authKey=7883171fa07d5a149ef5f8e4fd401c925286c2cf&email=shihan2@xxx.com",
            "expire_in": 300
        }
    }
    

    5.直接往登陆地址上跳 , 记录cookie session等身份信息 , 再跳到真正的系统  

     

  • 相关阅读:
    将vue文件script代码抽取到单独的js文件
    git pull 提示错误:Your local changes to the following files would be overwritten by merge
    vue和uniapp 配置项目基础路径
    XAMPP Access forbidden! Access to the requested directory is only available from the local network.
    postman与newman集成
    postman生成代码段
    Curl命令
    POST方法的Content-type类型
    Selenium Grid 并行的Web测试
    pytorch转ONNX以及TnesorRT的坑
  • 原文地址:https://www.cnblogs.com/taoshihan/p/12761479.html
Copyright © 2011-2022 走看看