MobileFirst 7.0结合身份验证和Java SQL适配器失败

时间:2015-04-17 19:26:18

标签: ibm-mobilefirst mobilefirst-adapters

我尝试将Java UserAdapter和基于适配器的身份验证结合起来

我希望在经过身份验证后获取用户列表,但它失败了

在服务器端,基于适配器的身份验证,AuthenticationAdapter-impl.js

function onAuthRequired(headers, errorMessage) {
  errorMessage = errorMessage ? errorMessage : null;

  return {
    authRequired: true,
    errorMessage: errorMessage
  };
}

function submitAuthentication(username, password){

  if (username==="admin" && password === "admin"){

    var userIdentity = {
      userId: username,
      displayName: username, 
      attributes: {
        foo: "bar"
      }
    };

    WL.Server.setActiveUser("AdapterAuthRealm", userIdentity);

    return { 
      authRequired: false 
    };
  }

  return onAuthRequired(null, "Invalid login credentials");
}


function listUsers() {
  var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);
  return resourceRequest.send();
}

function onLogout() {
  WL.Server.setActiveUser("AdapterAuthRealm", null);
  WL.Logger.debug("Logged out");
}

在客户端,

function listUsers(){
  busyIndicator.show();
  var invocationData = {
    adapter : "AuthenticationAdapter",
    procedure: "listUsers",
    parameters: []
  };

  WL.Client.invokeProcedure(invocationData, {
    onSuccess: listUsersSuccess, 
    onFailure: listUsersFailure
  });
}

function listUsersSuccess(result){
  WL.Logger.debug("Feed retrieve success");
  busyIndicator.hide();
  WL.Logger.debug(JSON.stringify(result));
  if (result.responseJSON.length > 0){ 
    displayUsers(result.responseJSON);
  } else {
    listUsersFailure();
  }
}

function listUsersFailure(result){
  WL.Logger.error("Feed retrieve failure");
  busyIndicator.hide();
  WL.SimpleDialog.show("Banking Application", "Service not available. Try again later.", [
    {
      text : 'Reload',
      handler : WL.Client.reloadApp 
    },
    {
      text: 'Close',
      handler : function() {}
    }
  ]);
}

返回onFailure回复

1 个答案:

答案 0 :(得分:0)

WLResourceRequest是客户端API,因此您不能在适配器上使用它,因为适配器在服务器上运行。

您应该按如下方式更新listUsers功能(客户端):

function listUsers(){
  busyIndicator.show();

  var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);

  resourceRequest.send().then(listUsersSuccess).fail(listUsersFailure);
}

<强>更新

您可以使用@OAuthSecurity注释保护Java Adapter方法。

<强> UserAdapter.java

@GET
@Path("/protectePath")
@OAuthSecurity(scope="YourRealm")
public String myProtectedMethod() {
    // your code here
    return "your-response";
}