调用 setActiveUser 会抛出错误
Calling setActiveUser throws error
我正在尝试为用户分配用户标识,但出现错误。我按照以下步骤操作。
服务器详情。
<realm loginModule="AuthLoginModule" name="AuthRealm">
<className>com.worklight.integration.auth.AdapterAuthenticator</className>
<parameter name="login-function" value="AuthAdapter.onAuthRequired"/>
<parameter name="logout-function" value="AuthAdapter.onLogout"/>
</realm>
</realms>
<loginModule name="AuthLoginModule">
<className>com.worklight.core.auth.ext.NonValidatingLoginModule</className>
</loginModule>
<customSecurityTest name="AuthSecurityTest">
<test isInternalUserID="true" realm="AuthRealm"/>
</customSecurityTest>
适配器
function onAuthRequired(headers, errorMessage){
errorMessage = errorMessage ? errorMessage : null;
return {
authStatus: "credentialsRequired",
errorMessage: errorMessage
};
}
function submitAuthentication(username, password){
// if (username==="user" && password === "password"){
var userIdentity = {
userId: username,
displayName: username,
attributes: {
foo: "bar"
}
};
WL.Server.setActiveUser("AuthRealm", userIdentity);
return {
authStatus: "complete"
};
// }
// return onAuthRequired(null, "Invalid login credentials");
}
function getSecretData(){
return {
secretData: "Very very very very secret data"
};
}
function onLogout(){
WL.Logger.debug("Logged out");
}
适配器xml
<procedure name="submitAuthentication" securityTest="wl_unprotected" />
<procedure name="getSecretData" securityTest="AuthSecurityTest"/>
Android代码:
public class MFPInitializer {
Context mContext;
public AndroidChallengeHandler challengeHandler;
private String realm = "AuthRealm";
WLClient client = null;
private static MFPInitializer mfpInitializer = new MFPInitializer();
/* A private Constructor prevents any other
* class from instantiating.
*/
private MFPInitializer() {
}
/* Static 'instance' method */
public static MFPInitializer getInstance() {
return mfpInitializer;
}
void mfpConnector(WLClient client) {
challengeHandler = new AndroidChallengeHandler(realm);
client.registerChallengeHandler(challengeHandler);
client.connect(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
Log.d("Success--- MFP_connect", wlResponse.getResponseText());
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.d("fail-- MFP_connect", wlFailResponse.getErrorMsg());
}
});
}
public void logout() {
if (client != null) {
client.logout(realm, new WLRequestListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
}
});
}
}
public WLClient mfpInit(Context context) {
mContext = context;
try {
client = WLClient.createInstance(context);
if (client != null) {
mfpConnector(client);
}
} catch (Exception e) {
e.printStackTrace();
}
return client;
}
public static void registerUser() {
try {
URI adapterPath = new URI("/adapters/AuthAdapter/getSecretData");
WLResourceRequest request = new WLResourceRequest(adapterPath, WLResourceRequest.GET);
request.setQueryParameter("params", "[]");
request.send(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
Log.d("onsuccess--------", wlResponse.getResponseText());
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.d("onfail--------", wlFailResponse.getErrorMsg());
}
});
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
Android 挑战处理程序
public class AndroidChallengeHandler extends ChallengeHandler {
// private Activity parentActivity;
private WLResponse cachedResponse;
public AndroidChallengeHandler( String realm) {
super(realm);
// parentActivity = activity;
}
@Override
public void onFailure(WLFailResponse response) {
try {
submitFailure(response);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSuccess(WLResponse response) {
submitSuccess(response);
}
@Override
public boolean isCustomResponse(WLResponse response) {
try {
if(response!= null &&
response.getResponseJSON()!=null &&
!response.getResponseJSON().isNull("authStatus") &&
response.getResponseJSON().getString("authStatus") != ""){
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
public void handleChallenge(WLResponse response){
cachedResponse = response;
Log.d( "handleChallenge: ---- ",response.getResponseText());
try {
if(response.getResponseJSON().getString("authStatus").equals("credentialsRequired")){
// MainAdapterBasedAuth.setMainText("handleChallenge->credentialsRequired");
// Intent login = new Intent(parentActivity, LoginAdapterBasedAuth.class);
// parentActivity.startActivityForResult(login, 1);
}
else if(response.getResponseJSON().getString("authStatus").equals("complete")){
Log.d( "handleChallenge: ---- ",response.getResponseText());
submitSuccess(cachedResponse);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void submitLogin( String userName, String password){
Object[] parameters = new Object[]{userName, password};
WLProcedureInvocationData invocationData = new WLProcedureInvocationData("AuthAdapter", "submitAuthentication");
invocationData.setParameters(parameters);
WLRequestOptions options = new WLRequestOptions();
options.setTimeout(30000);
submitAdapterAuthentication(invocationData, options);
}
}
Intally in activity 1 a 我正在调用,它将初始化 wlclient 并添加领域。
MFPInitializer.getInstance().mfpInit(getApplicationContext());
但是当我尝试将登录信息提交到移动优先平台的设备时,我的应用程序崩溃了
MFPInitializer mfpInitializer= MFPInitializer.getInstance();
mfpInitializer.challengeHandler.submitLogin("user","Passcode");
我收到以下错误
在 android 监视器上。
ava.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.worklight.wlclient.WLRequest.shouldFailOnChallengeCancel()' on a null object reference
日食 (MFP)
An error occurred while invoking procedure [project _MFP]AuthAdapter/submitAuthenticationFWLSE0100E: parameters: [project _MFP]
Cannot change identity of an already logged in user in realm 'AuthRealm'. The application must logout first.
FWLSE0101E: Caused by: [pro
上述错误已解决,但新用户 ID 未在 mobilefirst 控制台中更新。
你能帮我看看我哪里做错了吗
如评论中所述,已通过从 IBM Fix Central 网站更新到最新可用的 iFix 版本解决了此问题。
我正在尝试为用户分配用户标识,但出现错误。我按照以下步骤操作。 服务器详情。
<realm loginModule="AuthLoginModule" name="AuthRealm">
<className>com.worklight.integration.auth.AdapterAuthenticator</className>
<parameter name="login-function" value="AuthAdapter.onAuthRequired"/>
<parameter name="logout-function" value="AuthAdapter.onLogout"/>
</realm>
</realms>
<loginModule name="AuthLoginModule">
<className>com.worklight.core.auth.ext.NonValidatingLoginModule</className>
</loginModule>
<customSecurityTest name="AuthSecurityTest">
<test isInternalUserID="true" realm="AuthRealm"/>
</customSecurityTest>
适配器
function onAuthRequired(headers, errorMessage){
errorMessage = errorMessage ? errorMessage : null;
return {
authStatus: "credentialsRequired",
errorMessage: errorMessage
};
}
function submitAuthentication(username, password){
// if (username==="user" && password === "password"){
var userIdentity = {
userId: username,
displayName: username,
attributes: {
foo: "bar"
}
};
WL.Server.setActiveUser("AuthRealm", userIdentity);
return {
authStatus: "complete"
};
// }
// return onAuthRequired(null, "Invalid login credentials");
}
function getSecretData(){
return {
secretData: "Very very very very secret data"
};
}
function onLogout(){
WL.Logger.debug("Logged out");
}
适配器xml
<procedure name="submitAuthentication" securityTest="wl_unprotected" />
<procedure name="getSecretData" securityTest="AuthSecurityTest"/>
Android代码:
public class MFPInitializer {
Context mContext;
public AndroidChallengeHandler challengeHandler;
private String realm = "AuthRealm";
WLClient client = null;
private static MFPInitializer mfpInitializer = new MFPInitializer();
/* A private Constructor prevents any other
* class from instantiating.
*/
private MFPInitializer() {
}
/* Static 'instance' method */
public static MFPInitializer getInstance() {
return mfpInitializer;
}
void mfpConnector(WLClient client) {
challengeHandler = new AndroidChallengeHandler(realm);
client.registerChallengeHandler(challengeHandler);
client.connect(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
Log.d("Success--- MFP_connect", wlResponse.getResponseText());
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.d("fail-- MFP_connect", wlFailResponse.getErrorMsg());
}
});
}
public void logout() {
if (client != null) {
client.logout(realm, new WLRequestListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
}
});
}
}
public WLClient mfpInit(Context context) {
mContext = context;
try {
client = WLClient.createInstance(context);
if (client != null) {
mfpConnector(client);
}
} catch (Exception e) {
e.printStackTrace();
}
return client;
}
public static void registerUser() {
try {
URI adapterPath = new URI("/adapters/AuthAdapter/getSecretData");
WLResourceRequest request = new WLResourceRequest(adapterPath, WLResourceRequest.GET);
request.setQueryParameter("params", "[]");
request.send(new WLResponseListener() {
@Override
public void onSuccess(WLResponse wlResponse) {
Log.d("onsuccess--------", wlResponse.getResponseText());
}
@Override
public void onFailure(WLFailResponse wlFailResponse) {
Log.d("onfail--------", wlFailResponse.getErrorMsg());
}
});
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
Android 挑战处理程序
public class AndroidChallengeHandler extends ChallengeHandler {
// private Activity parentActivity;
private WLResponse cachedResponse;
public AndroidChallengeHandler( String realm) {
super(realm);
// parentActivity = activity;
}
@Override
public void onFailure(WLFailResponse response) {
try {
submitFailure(response);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSuccess(WLResponse response) {
submitSuccess(response);
}
@Override
public boolean isCustomResponse(WLResponse response) {
try {
if(response!= null &&
response.getResponseJSON()!=null &&
!response.getResponseJSON().isNull("authStatus") &&
response.getResponseJSON().getString("authStatus") != ""){
return true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
public void handleChallenge(WLResponse response){
cachedResponse = response;
Log.d( "handleChallenge: ---- ",response.getResponseText());
try {
if(response.getResponseJSON().getString("authStatus").equals("credentialsRequired")){
// MainAdapterBasedAuth.setMainText("handleChallenge->credentialsRequired");
// Intent login = new Intent(parentActivity, LoginAdapterBasedAuth.class);
// parentActivity.startActivityForResult(login, 1);
}
else if(response.getResponseJSON().getString("authStatus").equals("complete")){
Log.d( "handleChallenge: ---- ",response.getResponseText());
submitSuccess(cachedResponse);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void submitLogin( String userName, String password){
Object[] parameters = new Object[]{userName, password};
WLProcedureInvocationData invocationData = new WLProcedureInvocationData("AuthAdapter", "submitAuthentication");
invocationData.setParameters(parameters);
WLRequestOptions options = new WLRequestOptions();
options.setTimeout(30000);
submitAdapterAuthentication(invocationData, options);
}
}
Intally in activity 1 a 我正在调用,它将初始化 wlclient 并添加领域。
MFPInitializer.getInstance().mfpInit(getApplicationContext());
但是当我尝试将登录信息提交到移动优先平台的设备时,我的应用程序崩溃了
MFPInitializer mfpInitializer= MFPInitializer.getInstance();
mfpInitializer.challengeHandler.submitLogin("user","Passcode");
我收到以下错误
在 android 监视器上。
ava.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.worklight.wlclient.WLRequest.shouldFailOnChallengeCancel()' on a null object reference
日食 (MFP)
An error occurred while invoking procedure [project _MFP]AuthAdapter/submitAuthenticationFWLSE0100E: parameters: [project _MFP]
Cannot change identity of an already logged in user in realm 'AuthRealm'. The application must logout first.
FWLSE0101E: Caused by: [pro
上述错误已解决,但新用户 ID 未在 mobilefirst 控制台中更新。
你能帮我看看我哪里做错了吗
如评论中所述,已通过从 IBM Fix Central 网站更新到最新可用的 iFix 版本解决了此问题。