Android的SSL问题 [英] Android SSL issues
本文介绍了Android的SSL问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有在使用SSL从Android电子emulater连接服务器的问题。我一直在使用portecle(BKS)。当我用来连接服务器创建公共密钥,认证不采取place.Logcat没有显示任何错误,但SSL连接不工作。
我的资料来源$ C $ C:
进口android.content.Context;
进口org.apache.http.conn.ClientConnectionManager;
进口org.apache.http.conn.scheme.PlainSocketFactory;
进口org.apache.http.conn.scheme.Scheme;
进口org.apache.http.conn.scheme.SchemeRegistry;
进口org.apache.http.conn.ssl.SSLSocketFactory;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.impl.conn.SingleClientConnManager;
导入com.myclinicmyway *。
进口的java.io.InputStream;
进口java.security.KeyStore中; 公共类MyHttpClient扩展DefaultHttpClient { 公众最终上下文的背景下;
公共MyHttpClient(上下文的背景下){
this.context =背景;
}
//HTTP,PlainSocketFactory.getSocketFactory(),80 @覆盖保护ClientConnectionManager createClientConnectionManager(){ SchemeRegistry注册表=新SchemeRegistry();
registry.register(
新的计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
registry.register(新计划(https开头,newSslSocketFactory(),443));
返回新SingleClientConnManager(getParams()方法,注册表); } //客户端连接的结束
私人的SSLSocketFactory newSslSocketFactory(){ 尝试{
密钥仓库中可信任= KeyStore.getInstance(BKS);
InputStream的时间= context.getResources()openRawResource(R.raw.docinbangalorefinals)。 尝试{
trusted.load(在docinbangalore.toCharArray());
} {最后
附寄();
}
返回新的SSLSocketFactory(信任); }
赶上(例外五){
抛出新的AssertionError(E);
} //捕捞结束 } // ssl的插座端} // CLAS结束
解决方案
我得到了solution.Following code为我工作。
公共类MyHttpClient扩展DefaultHttpClient {
最后上下文的背景下;
的TrustManager easyTrustManager =新X509TrustManager(){
@覆盖
公共无效checkClientTrusted(
X509证书[]链,
字符串的authType)抛出CertificateException {
} @覆盖
公共无效checkServerTrusted(
X509证书[]链,
字符串的authType)抛出CertificateException {
} @覆盖
公共x509证书[] getAcceptedIssuers()为{
返回null;
}
};
公共MyHttpClient(上下文的背景下){
this.context =背景;
} @覆盖保护ClientConnectionManager createClientConnectionManager(){
SchemeRegistry注册表=新SchemeRegistry();
registry.register(
新的计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
registry.register(新计划(https开头,newSslSocketFactory(),443));
返回新SingleClientConnManager(getParams()方法,注册表);
}
私人MySSLSocketFactory newSslSocketFactory(){
尝试{
密钥仓库中可信任= KeyStore.getInstance(BKS);
尝试{
trusted.load(NULL,NULL); } {最后
} MySSLSocketFactory sslfactory =新MySSLSocketFactory(信任);
sslfactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
返回sslfactory;
}赶上(例外五){
抛出新的AssertionError(E);
} }
公共类MySSLSocketFactory扩展的SSLSocketFactory {
的SSLContext的SSLContext = SSLContext.getInstance(TLS); 公共MySSLSocketFactory(密钥库信任)抛出抛出:NoSuchAlgorithmException,KeyManagementException,KeyStoreException,UnrecoverableKeyException {
超(信任); 的TrustManager TM =新X509TrustManager(){
公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
} 公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
} 公共x509证书[] getAcceptedIssuers()为{
返回null;
}
}; sslContext.init(空,新的TrustManager [] {} TM,NULL);
} @覆盖
公共插座的createSocket(Socket套接字,字符串主机,端口INT,布尔自动关闭)抛出IOException异常,{UnknownHostException异常
返回sslContext.getSocketFactory()的createSocket(插座,主机,端口自动关闭)。
} @覆盖
公共插座的createSocket()抛出IOException
返回sslContext.getSocketFactory()的createSocket()。
}
}
}
I am having problems in connecting server using ssl from android emulater. I have created public key using portecle(bks).When i used to connect server,authentication is not taking place.Logcat is not showing any error but ssl connection is not working.
My Source Code:
import android.content.Context;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import com.myclinicmyway.*;
import java.io.InputStream;
import java.security.KeyStore;
public class MyHttpClient extends DefaultHttpClient {
public final Context context;
public MyHttpClient(Context context) {
this.context = context;
}
//"http", PlainSocketFactory.getSocketFactory(), 80
@Override protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}// end of client connection
private SSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = context.getResources().openRawResource(R.raw.docinbangalorefinals);
try {
trusted.load(in, "docinbangalore".toCharArray());
} finally {
in.close();
}
return new SSLSocketFactory(trusted);
}
catch (Exception e) {
throw new AssertionError(e);
}// end of catch
}// end of ssl socket
}// end of clas
解决方案
I got the solution.Following code worked for me.
public class MyHttpClient extends DefaultHttpClient {
final Context context;
TrustManager easyTrustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(
X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
public MyHttpClient(Context context) {
this.context = context;
}
@Override protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private MySSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS");
try {
trusted.load(null, null);
} finally {
}
MySSLSocketFactory sslfactory = new MySSLSocketFactory(trusted);
sslfactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return sslfactory;
} catch (Exception e) {
throw new AssertionError(e);
}
}
public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
}
这篇关于Android的SSL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文