是的,这个 JS 函数就是你所需要的 (
basic-auth.js
):
function(creds) {
var temp = creds.username + ':' + creds.password;
var Base64 = Java.type('java.util.Base64');
var encoded = Base64.getEncoder().encodeToString(temp.getBytes());
return 'Basic ' + encoded;
}
然后使用此函数构建
Authorization
标头的值:
* header Authorization = call read('basic-auth.js') { username: 'john', password: 'secret' }
请参阅此处的文档:https://github.com/intuit/karate#http-basic-authentication-example
有关 OAuth 或“登录表单”类型的流程,请参阅:https://stackoverflow.com/a/58643689/143475 和 https://stackoverflow.com/a/46333729/143475