首页 > 其他分享 >Apifox-Postman 请求前登录

Apifox-Postman 请求前登录

时间:2023-04-25 21:44:43浏览次数:53  
标签:const Postman 登录 publicKey Cookie result Apifox pm

请求后端接口进行测试时,往往需要先登录,在 Apifox 中可以用“前置脚本”来完成登录操作,每次发请求测试接口前,都先调用“前置脚本”完成登录。

下面是一个例子(更多信息可参考登录态(Auth)如何处理),代码流程:

  1. 环境变量中获取LOGIN_USERNAMELOGIN_PASSWORD变量的值(用户名和密码——需要自己先设置好)
  2. 请求后端接口获取到公钥
  3. 使用公钥加密用户名和密码
  4. 使用加密后的用户名和密码请求后端登录接口,获得 Cookie 值
  5. 将 Cookie 值设置到请求头中
  6. 之后发起请求时就会带上 Cookie 值,后端就不会拦截请求
const username = pm.environment.get("LOGIN_USERNAME");
const password = pm.environment.get("LOGIN_PASSWORD");
login('session.id', 'ACCESS_TOKEN', username, password);

/**
 * 登录并设置 Cookie 值
 * @param {String} cookieKey Cookie 名
 * @param {String} envName 用来保存 Cookie 值的全局变量名
 * @param {String} username 用户名
 * @param {String} password 用户密码
 */
function login(cookieKey = 'token', envName = 'ACCESS_TOKEN', username, password) {
    const baseUrl = pm.request.getBaseUrl();
    const publicKeyRequest = {
        url: baseUrl + "/a/loginV2",
        method: "POST"
    };
    // pm.sendrequest 参考文档:https://www.apifox.cn/help/app/scripts/api-references/pm-reference/#pm-sendrequest
    pm.sendRequest(publicKeyRequest, function(err, res) {
        if (err) {
            console.log(err);
        } else {
            const result = res.json();
            console.log(result);
            if (result.code == 200 && result.msg == '已登录'){
                console.log('已登录');
                pm.request.headers.upsert({ key: 'Cookie', value: cookieKey + '=' + pm.environment.get(envName)})
                return
            }
            if (result.code == 100) {
                const publicKey = result.data;

                const loginRequest = {
                    url: baseUrl + "/a/loginV2",
                    method: "POST",
                    body: {
                        mode: "urlencoded",
                        urlencoded: [
                            { key: "keyA", value: encrypt(username, publicKey) },
                            { key: "keyB", value: encrypt(password, publicKey) },
                        ],
                    }
                };

                pm.sendRequest(loginRequest, function(err, res) {
                    if (err) {
                        console.log(err)
                        return
                    }
                    // Cookies 参考文档:https://www.apifox.cn/help/app/scripts/api-references/pm-reference/#pm-cookies
                    const result = res.json();
                    console.log(result);
                    if (result.code == 200 && result.msg == '已登录'){
                        console.log('已登录');
                        pm.request.headers.upsert({ key: 'Cookie', value: cookieKey + '=' + pm.environment.get(envName)})
                    } else {
                        console.log(envName, result.data);
                        pm.environment.set(envName, result.data);
                        // 参考:https://blog.csdn.net/m0_67401660/article/details/123419962
                        pm.request.headers.upsert({ key: 'Cookie', value: cookieKey + '=' + pm.environment.get(envName)})
                    }
                });
            }
        }
    });
}

/**
 * RSA 加密,参考:https://blog.csdn.net/m0_67401660/article/details/123419962
 * @param {String} encryptData 待加密值 
 * @param {String} publicKey 公钥
 * @returns 加密后的值
 */
function encrypt(encryptData, publicKey) {
    const jsrsasign = require("jsrsasign");
    publicKey = "-----BEGIN PUBLIC KEY-----" + publicKey + "-----END PUBLIC KEY-----";
    const pub = jsrsasign.KEYUTIL.getKey(publicKey);
    const enc = jsrsasign.KJUR.crypto.Cipher.encrypt(encryptData, pub);
    return enc;
}

标签:const,Postman,登录,publicKey,Cookie,result,Apifox,pm
From: https://www.cnblogs.com/Higurashi-kagome/p/17353996.html

相关文章

  • 【Azure 应用服务】启用 Managed Identity 登录 SQL Server 报错 Managed Identity au
    问题描述在AppService中启用Identity后,使用系统自动生成Identity。使用如下代码连接数据库SQLServer:SQLServerDataSourcedataSource=newSQLServerDataSource();dataSource.setServerName("yoursqlservername.database.chinacloudapi.cn");//Replacewit......
  • vue2项目中调取登录接口登录以后获取个人信息以后,储存在哪里,怎么在不同的页面展示想
    在Vue2项目中,可以将个人信息存储在Vuex状态管理中或者浏览器的本地存储中,具体取决于项目的需求和规模。1.Vuex状态管理在Vuex中定义一个user模块,用于存储用户信息,可以在登录成功后将用户信息存储到该模块中。```javascript//store/user.jsconststate={userInfo:null}......
  • P.16-登录接口代码实现、P.17-测试接口
    P.16-登录接口代码实现自定义登陆接口,然后让SpringSecurity对这个接口放行,让用户访问这个接口的时候不用登录也能访问。在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,所以需要在SecurityConfig中配置把AuthenticationManager注入容器。......
  • P.13-用户密码加密存储密码、P.14-铺垫知识jwt工具类使用、P.15-登录接口实现细节分析
    P.13-用户密码加密存储密码实际项目中我们不会把密码明文存储在数据库中。默认使用的PasswordEncoder要求数据库中的密码格式为:{id}password。它会根据id去判断密码的加密方式。但是我们一般不会采用这种方式。所以就需要替换PasswordEncoder。我们一般......
  • 服务器之间实现免密登录的简易教程
    今天这篇文章主要是教会大家如何实现服务器之间的免密登录。1、先在所有服务器上执行命令:ssh-keygen-tdsa-P''-f~/.ssh/id_dsamaster服务器slave1服务器slave2服务器2、而后在所有服务器上执行命令:cat/.ssh/id_dsa.pub>>/.ssh/authorized_keysmaster服务器slave1服务器......
  • 直播平台软件开发,一个简单的Android登录实现demo
    直播平台软件开发,一个简单的Android登录实现demo一、登录活动 packagecom.example.login; importandroid.content.Intent;importandroid.os.Bundle;importandroid.text.TextUtils;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText......
  • 接口测试工具-Postman使用详解
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • 微信公众号使用隐藏页判断登录
    <scripttype="text/javascript">   $(document).ready(function(){      document.getElementById("over").style.display="block";      document.getElementById("layout").style.display="block";      //判断......
  • 答题积分小程序云开发实战-界面交互篇:注册登录页布局样式与逻辑交互开发
    微信小程序云开发实战-答题积分赛小程序界面交互篇:注册登录页布局样式与逻辑交互开发写在前面-开发调试小技巧模拟器通常默认展示的页面是首页,那么如果我们想切换到其他页面呢,那怎么办?我这里教给初学者三种方式,方便大家在搭建页面过程中,进行开发调试。点击事件跳转给页面按钮添加一......
  • 登录接口实现细节分析与登录接口代码实现
    登录接口实现细节分析登陆接口接下我们需要自定义登陆接口,然后让SpringSecurity对这个接口放行,让用户访问这个接口的时候不用登录也能访问。​在接口中我们通过AuthenticationManager的authenticate方法来进行用户认证,所以需要在SecurityConfig中配置把Authe......