我正在编写一个具有服务器连接的Android应用程序.我想在Android设备上验证应用程序的用户,让服务器知道这已经完成.
假设用户有:
AccountManager成功我现在想要正确安全地让服务器知道设备上的应用程序已经对用户进行了身份验证.服务器如何验证这不是假的?有没有办法在服务器上使用Google和Facebook验证令牌而无需用户交互?
谢谢.
我已经研究和研究过这个问题,直到我变得灰白秃顶.我如何得到一个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);
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我确实有一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是在没有webview.setHttpAuthUsernamePassword的情况下使用并且工作正常,除了它意味着向网站发出了2个请求 - 第一个获得401然后客户端使用授权内容启动这对于少量网站流量很好但是减半流量(目前平均49个请求p/m)是现在的游戏名称!
我读到我可以通过使用先发制人地提供凭据
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
Run Code Online (Sandbox Code Playgroud)
然而,这只会导致Http Basic:访问被拒绝错误服务器基本URL常量是站点的域名,即https://example.com(没有页面)实际URL是https://example.com/some_pages.无论是使用完整网址还是域名都没有区别.我检查了领域,我有正确的,我只使用空字符串只提供电子邮件和密码.这可能与网站使用https的事实有关吗?我的代码似乎在没有https的开发框中正常工作,但这可能是一个红色的鲱鱼.
唯一似乎涵盖我的要求的堆栈溢出问题没有得到接受的答案,文档也没有我能看到的帮助.
我现在脑袋里有这么大的凹痕,撞在一堵砖墙上,我正想着拿一顶方帽子.
如果有人能给我解决方案,我会永远欠你的债!我甚至可能通过电子邮件发送给你一个KitKat
我正在尝试将一个简单的JSON帖子实现到接受JSON身体基本身份验证的URL ANDROID.
我试过HttpUrlConnection但我得到了"unauthorized"我的数据发送到服务器.
我尝试的另一种方法是使用HttpClient,但现在我遇到了另一个问题.身份验证工作正常,但数据不会发送到服务器...
可以肯定的是,我在一个简单的Java项目(不是Android环境)中设置了一个小测试.
这是我POST用于服务器的代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://localhost/api/v1/purchase/");
postMethod.setEntity(new StringEntity("{\"amount_adult\" : 1, \"object_id\" : 13}"));
postMethod.setHeader( "Content-Type", "application/json");
String authorizationString = "Basic " + Base64.encodeToString(("travelbuddy" + ":" + "travelbuddy").getBytes(), Base64.DEFAULT); //this line is diffe
postMethod.setHeader("Authorization", authorizationString);
String response = httpClient.execute(postMethod,resonseHandler);
System.out.println("response :" + response);
Run Code Online (Sandbox Code Playgroud)
Java项目中的代码非常完美.
当我在Android中尝试完全相同的代码时,我internal server error从服务器获取,这意味着尚未收到JSON数据.
我真的不明白为什么这在JAVA中工作但在Android中不起作用.
我想要实现的效果是以下命令:
curl --dump-header …Run Code Online (Sandbox Code Playgroud) 正如问题所说,我正在尝试在android中进行摘要式身份验证.
到目前为止,我已经使用了DefaultHttpClient它和它的身份验证方法(使用UsernamePasswordCredentials等等),但是自Android 5以来它已经被弃用,并且将在Android 6中删除.
所以我即将切换DefaultHttpClient到HttpUrlConnection.
我现在想实现摘要式身份验证,这应该工作作为解释非常简单的在这里:
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Run Code Online (Sandbox Code Playgroud)
但是getPasswordAuthentication从来没有因为某些原因而被召唤.
在我搜索这个问题的过程中,我找到了不同的帖子,说HttpUrlConnection在android中不支持摘要认证,但这些帖子是从2010年到2012年,所以我不确定这是否仍然是真的.我们HttpUrlConnection还在桌面java应用程序中使用摘要式身份验证,它可以在其中运行.
我还发现了一些帖子,谈论着OkHttp.OkHttp似乎Android被引擎使用(更具体地说HttpUrlConnectionImpl).但这HttpUrlConnectionImpl有点奇怪,它甚至没有在Eclipse类型层次结构中显示,我无法调试它.它也应该是一个com.squareup.okhttp.internal.huc.HttpUrlConnectionImpl,而在android中它是一个com.android.okhttp.internal.http.HttpUrlConnectionImpl.
所以我只是无法HttpUrlConnection在android中进行摘要认证.
没有外部库,谁能告诉我如何做到这一点?
编辑:
服务器要求摘要验证:
WWW-Authenticate: Digest realm="Realm Name",domain="/domain",nonce="nonce",algorithm=MD5,qop="auth"
Run Code Online (Sandbox Code Playgroud)
所以基本身份验证不应该工作,因为服务器要求摘要.