【问题标题】:How do I make a secure HTTPS call from Android如何从 Android 进行安全的 HTTPS 调用
【发布时间】:2014-10-20 10:12:56
【问题描述】:

当连接到 HTTPS 后端时,来自 Android 中可用的 apache 库的常规 HTTP 调用是否会产生安全通信?

因为端点是 HTTPS,并且假设后端是安全的,这是从 Android 客户端发送密码的有效方式吗?请注意,在下面的代码中,密码被插入到 POST 请求的正文中,并且没有加密。

我的代码如下:

// Create post 
String url = "https://example.endpoint.com/token";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
HttpResponse response = null;
HttpEntity entity = null;

// Populate the post request            
JSONObject json = new JSONObject();
json.put("username", user);
json.put("password", pass);
StringEntity se = new StringEntity( json.toString() );
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);

// Execute the post request
response = client.execute(post);

使用的库有:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

【问题讨论】:

    标签: java android apache ssl https


    【解决方案1】:

    默认设置大多是安全的(即正确的证书验证)。但是,至少 Android 上可用的旧版本 apache HTTP 库不支持 SNI(服务器名称指示),因此无法与在同一 IP 地址上具有不同证书的服务器一起使用。

    我也怀疑他们是否会正确检查证书是否被吊销,但在这种情况下,它们并不比浏览器差多少:(

    【讨论】:

    • 如果您可以控制应用程序及其应连接的所有服务器,那么证书固定是一件好事。但是,如果应用程序应该能够与您无法控制的服务器进行通信,并且您永远不知道它们何时替换其证书,那么固定将不起作用。
    【解决方案2】:

    HTTPS 使用 SSL,所以它是安全的,但不要在每个请求中发送密码(仅在必要时),也不要将其存储在手机上。

    如果您想提高安全性,也可以添加 SSL pinning:https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning

    【讨论】:

      猜你喜欢
      • 2012-08-10
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多