如何从Android进行安全HTTPS调用

时间:2014-08-26 23:35:30

标签: java android apache ssl https

连接到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;

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

我还怀疑如果证书被撤销,他们会进行适当的检查,但在这种情况下,它们并不比浏览器差得多:(