Programmatically modify credential in worklight adapter

痴心易碎 提交于 2019-12-12 02:22:52

问题


I looking for a way to programmatically set the credential of an HTTP adapter? Is somebody has an example?

Is it possible to modify the adapter implementation js to overwrite the credentials?

with something like:

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);
}

But it failed as it doesn't find the Base64Encoder.

Any idea?


回答1:


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:

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"));
    }
}

In the adapter's .js file, declare globally:

var idan = new org.Idan();

And the procedure:

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

}



来源:https://stackoverflow.com/questions/15392206/programmatically-modify-credential-in-worklight-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!