以编程方式修改worklight适配器中的凭据 [英] Programmatically modify credential in worklight adapter
问题描述
我正在寻找一种以编程方式设置HTTP适配器凭据的方法吗?
有人有例子吗?
I looking for a way to programmatically set the credential of an HTTP adapter? Is somebody has an example?
是否可以修改适配器实现js来覆盖凭证?
Is it possible to modify the adapter implementation js to overwrite the credentials?
类似于:
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。
But it failed as it doesn't find the Base64Encoder.
任何想法?
推荐答案
首先,你应该使用授权,而不是身份验证如此处所述: http://en.wikipedia.org/wiki/Basic_access_authentication
Firstly, you should use "Authorization" and not "Authentication" as described here: http://en.wikipedia.org/wiki/Basic_access_authentication
此外,您应该执行以下操作:
In addition, you should do the following:
创建一个Java类,就像这样(你需要当然要清理和调整它):
Create a Java class, something like this (you'll need to clean and adjust it of course):
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文件中,全局声明:
In the adapter's .js file, declare globally:
var idan = new org.Idan();
程序:
function test(){
WL.Logger.debug(idan.basicHash("username_test", "secret_password"));
}
这篇关于以编程方式修改worklight适配器中的凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!