2014-01-13 23 views
0

我正在使用WorkLightAuthenticator来验证用户登录。 但我需要返回一些参数,如userName,organozationID等等。什么是正确的方式来返回这些数据? 我相信这将是createIdentity方法的例子。并检索客户端上的数据?Worklight安全WorkLightAuthLoginModule

/*Worklight Adapter*/  
public class LoginModule implements WorkLightAuthLoginModule { 

private String USERNAME; 
private String PASSWORD; 

public void init(Map<String, String> options) throws MissingConfigurationOptionException { 
} 

@SuppressWarnings("unchecked") 
public boolean login(Map<String, Object> authenticationData) { 
    USERNAME = (String) authenticationData.get("username"); 
    PASSWORD = (String) authenticationData.get("password"); 

    InvocationResult invocationResult = callLoginAdapter(USERNAME, PASSWORD); 
    JSONObject jsonObject = invocationResult.toJSON(); 

    HashMap<String, String> result = (HashMap<String, String>) jsonObject.get("result"); 

    if(result != null){ 
     return true; 
    } else { 
     HashMap<String, String> fault = (HashMap<String, String>) jsonObject.get("Fault"); 
     String detail = fault.get("Detail");       
     throw new RuntimeException(detail); 
    } 
} 



public UserIdentity createIdentity(String loginModule) { 
    HashMap<String, Object> customAttributes = new HashMap<String, Object>(); 
    customAttributes.put("AuthenticationDate", new Date()); 
    customAttributes.put("userName", userExample); 
    customAttributes.put("organizationID", 1); 

    UserIdentity identity = new UserIdentity(loginModule, USERNAME, null, null, customAttributes, PASSWORD); 
    return identity; 
} 

public void logout() { 
    USERNAME = null; 
    PASSWORD = null; 
} 

public void abort() { 
    USERNAME = null; 
    PASSWORD = null; 
} 

@Override 
public LoginModule clone() throws CloneNotSupportedException { 
    return (LoginModule) super.clone(); 
} 

public InvocationResult callLoginAdapter(String user, String password){ 
    DataAccessService service = WorklightBundles.getInstance().getDataAccessService(); 
    String parameters = "['"+user+"','"+password+"']"; 
    ProcedureQName procedureQName = new ProcedureQName("LoginAdapter", "getUserByLogin"); 
    return service.invokeProcedure(procedureQName, parameters);  
} 
} 

而且AuthenticatorRealmChallengeHandler在客户端

/*AuthenticatorRealmChallengeHandler.js*/ 
var customAuthenticatorRealmChallengeHandler = WL.Client.createChallengeHandler("AuthenticatorRealm"); 

customAuthenticatorRealmChallengeHandler.isCustomResponse = function(response) { 
console.log("**********************"); 
console.log(response.responseJSON); 

if (!response || !response.responseJSON) { 
    return false; 
} 

if (response.responseJSON.authStatus) 
    return true; 
else 
    return false; 
}; 

customAuthenticatorRealmChallengeHandler.handleChallenge = function(response){ 
var authStatus = response.responseJSON.authStatus; 

if (authStatus == "required"){  
    if (response.responseJSON.errorMessage){ 
     currentPage.loginFail(response.responseJSON.errorMessage); 
    } 
} else if (authStatus == "complete"){  
    currentPage.loadMaster(); 
    customAuthenticatorRealmChallengeHandler.submitSuccess(); 

} 
}; 

customAuthenticatorRealmChallengeHandler.submitLoginFormCallback = function(response) { 
var isLoginFormResponse = customAuthenticatorRealmChallengeHandler.isCustomResponse(response); 
if (isLoginFormResponse){ 
    customAuthenticatorRealmChallengeHandler.handleChallenge(response); 
} 
}; 

$('#loginButton').bind('click', function() { 
var reqURL = '/auth_request_url'; 
var options = {}; 
options.parameters = { 
    username : $('#userTextField').val(), 
    password : $('#passwordTextField').val() 
}; 
options.headers = {}; 
customAuthenticatorRealmChallengeHandler.submitLoginForm(reqURL, options, customAuthenticatorRealmChallengeHandler.submitLoginFormCallback); 
}); 

回答

0

变种属性= WL.Client.getUserInfo( “AuthenticatorRealm”, “属性”);