分享

安卓WebSocket WSS/SSL对证书受信任

发布于2017-09-11 作者shen100 104次浏览

WebSocket库下载地址

Java-WebSocket

Demo代码

package com.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.security.SecureRandom;
import javax.security.cert.X509Certificate;


class WebSocketChatClient extends WebSocketClient {

    public WebSocketChatClient( URI serverUri ) {
        super( serverUri );
    }

    @Override
    public void onOpen( ServerHandshake handshakedata ) {
        System.out.println( "Connected" );
        this.send("{\"age\": 22}");
    }

    @Override
    public void onMessage( String message ) {
        System.out.println( "got: " + message );

    }

    @Override
    public void onClose( int code, String reason, boolean remote ) {
        System.out.println( "Disconnected" + code + reason + remote);
        System.exit( 0 );
    }

    @Override
    public void onError( Exception ex ) {
        ex.printStackTrace();
    }
}

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                WebSocketImpl.DEBUG = true;

                WebSocketChatClient chatclient = null;
                try {
                    chatclient = new WebSocketChatClient( new URI( "wss://test.com/socket" ) );
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }

                SSLContext sslContext = null;
                try {
                    sslContext = SSLContext.getInstance( "TLS" );
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                try {
                    sslContext.init(null, new TrustManager[] {
                            new X509TrustManager() {

                                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                                    System.out.println("checkClientTrusted1");
                                }

                                @Override
                                public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                                        throws CertificateException {
                                    System.out.println("checkClientTrusted2");
                                }

                                public void checkServerTrusted(X509Certificate[] certs,
                                                               String authType) {
                                    System.out.println("checkServerTrusted1");
                                }

                                @Override
                                public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                                        throws CertificateException {
                                    System.out.println("checkServerTrusted2");
                                }
                                @Override
                                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                    return null;
                                }
                            }
                    }, new SecureRandom());
                } catch (KeyManagementException e) {
                    e.printStackTrace();
                }
                SSLSocketFactory factory = sslContext.getSocketFactory();

                try {
                    chatclient.setSocket( factory.createSocket() );
                } catch (IOException e) {
                    e.printStackTrace();
                }
                chatclient.connect();
            }
        }).start();
    }
}
收藏
分享
暂无回复