我正在开发一个MobileFirst 7.0应用程序,并且我已经实现了一个自定义身份验证器(来自入门示例)和LoginModule。成功登录后,我想使用我的LtpaToken对Web Experience Factory服务上的操作进行身份验证,例如,我想使用WCM API为已登录用户呈现内容,或者使用Puma API为当前登录用户更改一些数据。所有服务都具有相同域名的网址:test.sample.com。 2台服务器之间的SSO配置正确,一切都很好iOs设备,但我有Android 5.0及以上的问题(我正在测试我的代码只有5.0和5.1 Android版本)。我读了很多关于cookies和Android问题的事情> 5.0,但没有解决方案帮助我。 1.在使用域test.sample.com在MobileFirst中获取后,我得到了jsessionId和LtpaToken。我在登录后使用此代码,一切都很好,因为我的cookie存在:
getSecureCookie: function() {
_that = this;
console.log("get secure cookie in EWD method!");
WL.Client.getCookies().then(function(cookies) {
for (var i in cookies) {
console.log("test: " + i);
}
var ltpaToken = cookies[0];
var test = 'LtpaToken2=' + ltpaToken.value + ' ; path=/';
console.log("cookie_test" + test);
_that.storage.set("LtpaToken2", test);
console.log("ltpatokenJson: " + JSON.stringify(_that.storage.get("LtpaToken2")));
});
}
然后我尝试使用ajax异步请求调用需要ltpaToken的WEF服务,因为我的用户未经过身份验证。当我使用嗅探器时,我发现我的cookie没有附加到请求中。我的ajax代码如下所示:
$.ajax({
url: url,
type: type,
async: false,
dataType: 'json',
beforeSend: function(xhr) {
xhr.withCredentials = true;
},
contentType: "application/json; charset=utf-8",
data: data,
success: success,
error: error
});
当我尝试使用document.cookie包含此cookie时,它无法正常工作。
我的onCreate功能如下所示:
public class MyCompany extends CordovaActivity implements WLInitWebFrameworkListener {
WebView wv;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//super.init();
/*setContentView(R.layout.main);
wv = (WebView)findViewById(R.layout.main);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setDisplayZoomControls(false);
HashMap <String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Cookie", "cookieName=cookieValue;domain=test.sample.com;path=/;");*/
WL.createInstance(this);
WL.getInstance().showSplashScreen(this);
WL.getInstance().initializeWebFramework(getApplicationContext(), this);
setContentView(R.layout.main);
wv = (WebView)findViewById(R.id.test);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setDisplayZoomControls(false);
HashMap <String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Cookie", "cookieName=cookieValue;domain=test.sample.com;path=/;");
// Allow third party cookies for Android Lollipop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setAcceptThirdPartyCookies(wv,true);
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.setCookie("test.sample.com","test=cookieValueTest;domain=test.sampme.com;path=/");
}
wv.loadUrl(Config.getStartUrl(), extraHeaders);
}
/**
* The IBM MobileFirst Platform calls this method after its initialization is complete and web resources are ready to be used.
*/
public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){
if (result.getStatusCode() == WLInitWebFrameworkResult.SUCCESS) {
super.loadUrl(WL.getInstance().getMainHtmlFilePath());
} else {
handleWebFrameworkInitFailure(result);
}
}
private void handleWebFrameworkInitFailure(WLInitWebFrameworkResult result){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setNegativeButton(R.string.close, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
finish();
}
});
alertDialogBuilder.setTitle(R.string.error);
alertDialogBuilder.setMessage(result.getMessage());
alertDialogBuilder.setCancelable(false).create().show();
}
}
我在我的混合应用程序中使用本机WebView组件,我尝试使用CordovaActivity的WebView我得到例外:
java.lang.NullPointerException: Attempt to invoke virtual method android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5274)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
sed by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings
android.webkit.WebView.getSettings()' on a null object reference
at com.Omantel.Omantel.onCreate(Omantel.java:46)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
更新: 我解决了这个问题,并创建了Cordova Plugin,它将令牌添加到WebView上:
@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
if ("INJECT-COOKIES-TO-WEBVIEW".equals(action)){
l.debug("Started injecting cookies");
CookieStore cookieStore = HttpClientManager.getInstance().getHttpClient().getCookieStore();
List<Cookie> list = cookieStore.getCookies();
for (Cookie cookie : list){
String cookieName = cookie.getName();
l.debug("Found cookie :: " + cookieName);
if ("LtpaToken".equals(cookieName)){
l.debug("Found LtpaToken cookie");
CookieManager cookieManager = CookieManager.getInstance();
String cookieValue = cookie.getName() + "=" + cookie.getValue();
cookieManager.setCookie(cookie.getDomain(), cookieValue);
break;
}
l.debug("LtpaToken cookie not found");
}
l.debug("Done injecting cookies");
callbackContext.success();
return true;
}
return false;
}
}