[英]How to check if user is already login in google auth. in android
[英]android webview with https connection and basic auth. How to get this working?
我已經研究和研究過這個問題,直到我變得灰白禿頂。 我如何得到一個webview,適用於需要通過api級別8+上的https連接進行http基本身份驗證的站點
我有以下代碼
String email = Util.getEmail(this);
String pwd = Util.getPassword(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
// webview.setWebViewClient(new MobileWebViewClient(this));
webview.loadUrl(url);
正如您所看到的,我確實有一個Web視圖客戶端(現已注釋掉)覆蓋了onReceivedHttpAuthRequest方法,該方法看起來像這樣
@Override
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm){
String email = Util.getEmail(wvContext);
String pwd = Util.getPassword(wvContext);
if(!pwd.equalsIgnoreCase("-1") && !pwd.equalsIgnoreCase("-1")){
handler.proceed(email, pwd);
}
}
這是在沒有webview.setHttpAuthUsernamePassword的情況下使用並且工作正常,除了它意味着向網站發出了2個請求 - 第一個獲得401然后客戶端使用授權內容啟動這對於少量網站流量來說很好但是減半流量(目前平均49個請求p / m)是現在的游戲名稱!
我讀到我可以通過使用先發制人地提供憑據
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
然而,這只會導致Http Basic:訪問被拒絕錯誤服務器基本URL常量是站點的域名,即https://example.com (沒有頁面)實際URL是https://example.com/some_pages 。 無論是使用完整網址還是域名都沒有區別。 我檢查了領域,我有正確的,我只使用空字符串只提供電子郵件和密碼。 這可能與網站使用https的事實有關嗎? 我的代碼似乎在沒有https的開發框中正常工作,但這可能是一個紅色的鯡魚。
唯一似乎涵蓋我的要求的堆棧溢出問題沒有得到接受的答案,文檔也沒有我能看到的幫助。
我現在腦袋里有這么大的凹痕,撞在一堵磚牆上,我正想着拿一頂方帽子。
如果有人能給我解決方案,我會永遠欠你的債! 我甚至可能通過電子郵件發送給你一個KitKat
這個簡單的例子濫用了HttpWatch上的一個頁面,因為它對於一個有效的公共示例更有趣。
有問題的資源https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage
使用HTTPS上的基本身份驗證,可以在沒有身份驗證失敗的情況下加載(使用Android 2.3.7測試):
WebView v = ...; // Your webview goes here.
try {
HashMap<String, String> map = new HashMap<String, String>();
// This test service takes the username "httpwatch" and a random
// password. Repeating a password can lead to failure, so we create
// a decently random one using UUID.
String usernameRandomPassword = "httpwatch:" + UUID.randomUUID().toString();
String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP);
map.put("Authorization", authorization);
v.loadUrl("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?" + System.currentTimeMillis(), map);
} catch (UnsupportedEncodingException e) {}
這適用於ICS和Gingerbread。 不能訪問早於那個的東西,但是在API級別8中引入了loadUrl(String, Map<String,String>)
,所以我不明白它為什么不能用於它。
Nappy的澄清:
要支持對后續請求的身份驗證,請提供WebViewClient
並執行以下操作:
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, <your map containing the Authorization header>);
return true;
}
});
它適用於https URL。如果我們收到Untrusted_cer,那么我們將忽略它
webview.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
@Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
if(error.getPrimaryError()==SslError.SSL_UNTRUSTED){
handler.proceed();
}else{
handler.proceed();
}
}
});
我不知道第二個問題
要在您的網站上處理授權,只需在您的webViewClient中覆蓋以下方法,並在其處理程序中添加用戶名和密碼。
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed(StringUtils.AUTH_NAME,StringUtils.AUTH_PASS);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.