-1

我试图结合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响应

4

1 回答 1

0

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

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

function listUsers(){
  busyIndicator.show();

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

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

更新

您可以使用@OAuthSecurity注解来保护您的 Java 适配器方法。

用户适配器.java

@GET
@Path("/protectePath")
@OAuthSecurity(scope="YourRealm")
public String myProtectedMethod() {
    // your code here
    return "your-response";
}
于 2015-04-17T20:10:20.793 回答