以编程方式修改工作灯适配器中的凭据

Programmatically modify credential in worklight adapter

本文关键字:适配器 编程 方式 修改 工作      更新时间:2023-09-26

我正在寻找一种方法以编程方式设置HTTP适配器的凭据?有人能举个例子吗?

是否可以修改适配器实现js来覆盖凭据?

,例如:

function getMyAdapters(path) {
var tok = "myuser:mypw";
var hash = Base64Encoder.encode(tok);
var headers="{'User-Agent':'Mozilla'"+"Authentication: Basic }"+hash;
var input = {
        method : 'get',
        returnedContentType : 'json',
        headers: headers,
        path : path
    };

return WL.Server.invokeHttp(input);
}

但是它失败了,因为它没有找到Base64Encoder。

任何想法?

首先,您应该使用"Authorization"而不是"Authentication",如下所示:http://en.wikipedia.org/wiki/Basic_access_authentication

此外,你应该做以下事情:

创建一个Java类,就像这样(当然,您需要对它进行清理和调整):

public class Idan {
        public String basicHash(String user, String password) {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String authorization = user + ":" + password;
            return base64Encoder.encode(authorization.getBytes());
        }
        // to test:
        public static void main(String[] args) {
        Idan i = new Idan();
        System.out.println(i.basicHash("idan", "somepassword"));
    }
}

在适配器的.js文件中全局声明:

var idan = new org.Idan();

和程序:

function test(){
WL.Logger.debug(idan.basicHash("username_test", "secret_password"));

}