以编程方式修改worklight适配器中的凭据

时间:2013-03-13 17:22:42

标签: javascript ibm-mobilefirst

我在寻找一种以编程方式设置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。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

首先,您应该使用"授权"而不是"身份验证"如下所述: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"));

}