【问题标题】:Programmatically modify credential in worklight adapter以编程方式修改工作灯适配器中的凭据
【发布时间】:2013-03-01 19:04:18
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: javascript ibm-mobilefirst


    【解决方案1】:

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

    }

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    相关资源
    最近更新 更多