Search in sources :

Example 91 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project Gargoyle by callakrsos.

the class GoogleTrend method test.

/**
	 * 1. SSL 통신으로 데이터를 주고 받아야함.
	 * 		1- a Proxy 설정이 추가적으로 필요한경우 설정
	 *
	 * 2. URL Encoding으로 한글 데이터를 바꿔야함. << 여기서 많이 고생했다. 특정글자 부분에 한글이 깨져서
	 *
	 * 3. conn.connect(); 먼저 호출하고 inputStream객체를 리턴받아 핸들링하자
	 *
	 * 4. response code가 203이 리턴되는 경우가 있다.
	 * 		주로 프록시 서버관계인경우 발생되는것으로 보임 . 통신은 정상적으로 이루어 졌으나
	 * 		프록시 서버가 데이터를 리턴하는경우 발생하는것같다.
	 *
	 *5. 데이터중 일부가 불량임. 데이터 변환 기능을 추가함.
	 *		ex) "v" :new Date(2011,1,1) 형태로 되어있어 json 포멧을 만드는데 어려움
	 * @작성자 : KYJ
	 * @작성일 : 2016. 11. 1.
	 * @throws Exception
	 */
@Test
public void test() throws Exception {
    String geo = "KR";
    String date = "all";
    //		String keywords = "%EB%B0%95%EA%B7%BC%ED%98%9C,%EC%B5%9C%EC%88%9C%EC%8B%A4";
    String origin_keywords = "박근혜,최순실";
    String converted_keywords = URLEncoder.encode(origin_keywords, "UTF-8");
    //핫 트랜드 https://www.google.com/trends/hottrends/atom
    String URL_FORMAT = "https://www.google.com/trends/fetchComponent?cid=TIMESERIES_GRAPH_0&export=3&q=:keywords&geo=:geo";
    Map<String, Object> hashMap = new HashMap<>();
    hashMap.put("geo", geo);
    hashMap.put("date", date);
    hashMap.put("keywords", converted_keywords);
    String formattedURL = ValueUtil.getVelocityToText(URL_FORMAT, hashMap, true, null, str -> str);
    System.out.println(formattedURL);
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
    SSLContext.setDefault(ctx);
    URL url = new URL(formattedURL);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    try {
        //			conn.setDoInput(true);
        //			conn.setDoOutput(true);
        conn.setDefaultUseCaches(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0");
        //			conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        //			conn.setRequestProperty("Accept-Language", "ko-KR,kr;q=0.5");
        conn.setRequestProperty("Accept-Encoding", "UTF-8");
        conn.setRequestProperty("Connection", "keep-alive");
        //		conn.setIfModifiedSince(100L);
        conn.setRequestProperty("Accept", "text/html");
        //			conn.setRequestProperty("Accept-Charset", "UTF-8");
        //					conn.setRequestProperty("Accept-Encoding", "UTF-8");
        //					conn.setRequestProperty("Accept-Language", "KR");
        //		conn.setRequestProperty("Cache-Control", "no-store");
        //					conn.setRequestProperty("Pragma", "no-cache");
        conn.setHostnameVerifier(hostnameVerifier);
        //		System.out.println(conn.getContentEncoding());
        conn.getHeaderFields().forEach((str, li) -> {
            System.out.printf("%s : %s \n", str, li);
        });
        conn.setConnectTimeout(6000);
        conn.connect();
        //Charset
        //
        //Description
        //
        //US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
        //ISO-8859-1   ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
        //UTF-8 Eight-bit UCS Transformation Format
        //UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
        //UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
        //UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
        //필터링해야할 텍스트가 모두 포함된 원본 데이터
        String dirtyConent = null;
        //버퍼로 그냥 읽어봐도 되지만 인코딩 변환을 추후 쉽게 처리하기 위해 ByteArrayOutputStream을 사용
        try (InputStream is = conn.getInputStream()) {
            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                int read = -1;
                while ((read = is.read()) != -1) {
                    out.write(read);
                }
                out.flush();
                dirtyConent = out.toString();
            }
        }
        //		print_https_cert(conn);
        if (203 == conn.getResponseCode()) {
            //
            System.out.println(conn.getResponseMessage());
        //			File file = new File("sample.html");
        //			FileUtil.writeFile(file, dirtyConent, Charset.forName("UTF-8"));
        //			FileUtil.openFile(file);
        } else if (200 == conn.getResponseCode()) {
            System.out.printf("dirty content %s \n", dirtyConent);
            //원본 데이터에서 불필요한 부분 삭제
            String regexMatch = ValueUtil.regexMatch("\\(.*\\)", dirtyConent, str -> {
                return str.substring(1, str.length() - 1);
            });
            //데이터중 new Date(xxx,x,x) 라고 JSON이 인식못하는 텍스트를 제거함.
            regexMatch = ValueUtil.regexReplaceMatchs("new Date\\([0-9,]+\\)", regexMatch, str -> {
                int startIdx = str.indexOf('(');
                int endIdx = str.indexOf(')', startIdx);
                if (startIdx == -1 || endIdx == -1)
                    return str;
                startIdx++;
                return String.format("\"%s\"", str.substring(startIdx, endIdx).replaceAll(",", "-"));
            });
            //결과출력
            System.out.printf("result string : %s \n", regexMatch);
        }
    } finally {
        conn.disconnect();
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) ProxyInitializable(com.kyj.fx.voeditor.visual.main.initalize.ProxyInitializable) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLContext(javax.net.ssl.SSLContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) IOException(java.io.IOException) HashMap(java.util.HashMap) Test(org.junit.Test) ValueUtil(com.kyj.fx.voeditor.visual.util.ValueUtil) CertificateException(java.security.cert.CertificateException) KeyManager(javax.net.ssl.KeyManager) SecureRandom(java.security.SecureRandom) URLEncoder(java.net.URLEncoder) Certificate(java.security.cert.Certificate) Stream(java.util.stream.Stream) X509TrustManager(javax.net.ssl.X509TrustManager) Map(java.util.Map) HostnameVerifier(javax.net.ssl.HostnameVerifier) InputStream(java.io.InputStream) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Before(org.junit.Before) HashMap(java.util.HashMap) InputStream(java.io.InputStream) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 92 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project bnd by bndtools.

the class HttpsVerification method init.

/**
	 * Initialize the SSL Context, factory and verifier.
	 * 
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 * @throws IOException
	 * @throws CertificateException
	 * @throws FileNotFoundException
	 */
private synchronized void init() throws NoSuchAlgorithmException, KeyManagementException, FileNotFoundException, CertificateException, IOException {
    if (factory == null) {
        List<X509Certificate> certificates = createCertificates(certificatesPath);
        final X509Certificate[] trusted = certificates.toArray(new X509Certificate[0]);
        TrustManager[] trustAllCerts = new TrustManager[] { getTrustManager(trusted) };
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, trustAllCerts, new SecureRandom());
        factory = context.getSocketFactory();
        //
        // We can already verify the name through the matchers
        // of the URL so we can ignore that for our connections
        //
        verifier = new HostnameVerifier() {

            public boolean verify(String string, SSLSession session) {
                return verify;
            }
        };
    }
}
Also used : SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 93 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project ddf by codice.

the class AsyncClient method doTrustAllCertificates.

// Trust All Certifications
private void doTrustAllCertificates() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            return;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            return;
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    // Set HttpsURLConnection settings
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    HostnameVerifier hostnameVerifier = (s, sslSession) -> s.equalsIgnoreCase(sslSession.getPeerHost());
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
Also used : X509Certificate(java.security.cert.X509Certificate) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLContext(javax.net.ssl.SSLContext) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) LongPollingTransport(org.cometd.client.transport.LongPollingTransport) LoggerFactory(org.slf4j.LoggerFactory) TrustManager(javax.net.ssl.TrustManager) HashMap(java.util.HashMap) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) SecureRandom(java.security.SecureRandom) HttpClient(org.eclipse.jetty.client.HttpClient) MessageListener(org.cometd.bayeux.client.ClientSessionChannel.MessageListener) X509TrustManager(javax.net.ssl.X509TrustManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Map(java.util.Map) ClientSessionChannel(org.cometd.bayeux.client.ClientSessionChannel) ClientTransport(org.cometd.client.transport.ClientTransport) HostnameVerifier(javax.net.ssl.HostnameVerifier) Message(org.cometd.bayeux.Message) BayeuxClient(org.cometd.client.BayeuxClient) X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 94 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project iTest by e-government-ua.

the class DeleteTask method createHttpClient_AcceptsUntrustedCerts.

public HttpClient createHttpClient_AcceptsUntrustedCerts() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpClientBuilder b = HttpClientBuilder.create();
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

        public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            return true;
        }
    }).build();
    b.setSslcontext(sslContext);
    HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (X509HostnameVerifier) hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    b.setConnectionManager(connMgr);
    HttpClient client = b.build();
    return client;
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpClient(org.apache.http.client.HttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

HostnameVerifier (javax.net.ssl.HostnameVerifier)94 SSLSession (javax.net.ssl.SSLSession)41 SSLContext (javax.net.ssl.SSLContext)30 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)27 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)24 TrustManager (javax.net.ssl.TrustManager)19 IOException (java.io.IOException)18 URL (java.net.URL)18 X509Certificate (java.security.cert.X509Certificate)17 X509TrustManager (javax.net.ssl.X509TrustManager)17 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)14 SecureRandom (java.security.SecureRandom)14 InputStream (java.io.InputStream)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 CertificateException (java.security.cert.CertificateException)10 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)10 KeyManagementException (java.security.KeyManagementException)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8