andeoid4.x强制使用TLSv1.2

最近做安全性比较高的项目,上级最近出台新规要求必须Https+TLSv1.2

由于android20才开始默认使用TLS1.2,4..0-5.0一下默认的都是TLS1.0,

遇到一些坑费了点时间,网上查了好多资料,没有比较完整清晰的资料,

所以解决后记录一下,方便自己方便大家,好了下边正文开始。



首先明确一点要限制TLS版本需要设置的是什么,是SSLSocket

第二在哪设置,需要重写SSLSocketFactory(注:javax.net.ssl.SSLSocketFactory;而不是org.apache.http.conn.ssl.SSLSocketFactory;

我选择的不一定是绝对但一定是最简单省事的,当然后者你要是能实现请联系我,我会补充上去);

第三就是重写的.SSLSocketFactory的源码

public class MySSLSocketFactory extends SSLSocketFactory{  
    
	   private SSLSocketFactory internalSSLSocketFactory;

	   private SSLContext context;
	    public MySSLSocketFactory(KeyManager[] km, TrustManager[] tm, SecureRandom sr) throws KeyManagementException, NoSuchAlgorithmException {
	    	context = SSLContext.getInstance("TLSv1.2");
	    	context.init(km, tm, sr);
	        internalSSLSocketFactory = context.getSocketFactory();
	    }

	    @Override
	    public String[] getDefaultCipherSuites() {
	        return internalSSLSocketFactory.getDefaultCipherSuites();
	    }

	    @Override
	    public String[] getSupportedCipherSuites() {
	        return internalSSLSocketFactory.getSupportedCipherSuites();
	    }

	    @Override
	    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
	    	 SSLSocket sslSocket=(SSLSocket) context.getSocketFactory().createSocket(s, host, port, autoClose);  
	         sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});  
	         return sslSocket;  
	    }

	    @Override
	    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
	    	 SSLSocket sslSocket=(SSLSocket) context.getSocketFactory().createSocket(host,port);  
	         sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});  
	         return sslSocket;  
	    }

	    @Override
	    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
	    	 SSLSocket sslSocket=(SSLSocket) context.getSocketFactory().createSocket(host, port, localHost, localPort);  
	         sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});  
	         return sslSocket;  
	    }

	    @Override
	    public Socket createSocket(InetAddress host, int port) throws IOException {
	    	 SSLSocket sslSocket=(SSLSocket) context.getSocketFactory().createSocket(host, port);  
	         sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});  
	         return sslSocket;  
	    }

	    @Override
	    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
	    	 SSLSocket sslSocket=(SSLSocket) context.getSocketFactory().createSocket(address, port, localAddress, localPort);  
	         sslSocket.setEnabledProtocols(new String[]{"TLSv1.2"});  
	         return sslSocket;  
	    }
}

由于我用的volley框架KeyManager,TrustManager

这两个参数在外面已经设置好了所以我选择直接传过来;

在下一步就是把你项目中所有SSLSocketFactory

换成我们自己的MySSLSocketFactory就好了;

这样就实现的限制android4.x强制使用TLSv1.2了;

我用的是volley,但是okhttp等也都是可以的,原理一样;

项目保密只能贴这些了,如果有不了解的请给我发邮件询问;



感谢观看,如果你在使用过程中遇到什么问题欢迎与我交流,

邮箱是[email protected],如果有什么好的建议也请联系我

你可能感兴趣的:(Android)