{"id":1776,"date":"2012-11-23T02:13:59","date_gmt":"2012-11-23T02:13:59","guid":{"rendered":"http:\/\/www.msinterdev.org\/wp\/2012\/11\/23\/ssl-client-for-java\/"},"modified":"2012-11-23T02:13:59","modified_gmt":"2012-11-23T02:13:59","slug":"ssl-client-for-java","status":"publish","type":"post","link":"http:\/\/www.msinterdev.org\/wp\/2012\/11\/23\/ssl-client-for-java\/","title":{"rendered":"SSL Client for JAVA"},"content":{"rendered":"<p>JAVA\uc5d0\uc11c SSL Client\ub97c \uad6c\ud604\ud558\uace0 \uc778\uc99d\uc11c \uc5c6\uc774 \uc811\uadfc \ud560 \uc218 \uc788\ub3c4\ub85d \uad6c\ud604\ud55c \uc608\uc81c \uc785\ub2c8\ub2e4. <\/p>\n<p>public class URLUtils {<br \/>&nbsp;<br \/>&nbsp;private static boolean trusted = false;<\/p>\n<p>&nbsp;public static HttpURLConnection getConnection(URL url) throws IOException {<br \/>&nbsp; <br \/>&nbsp; HttpURLConnection con = null;<br \/>&nbsp; boolean https = url.getProtocol().equalsIgnoreCase(&#8220;https&#8221;) ? true : false;<br \/>&nbsp; <br \/>&nbsp; if (https) {<br \/>&nbsp;&nbsp; if (!trusted) trustAllHosts();<br \/>&nbsp;&nbsp; con = (HttpsURLConnection) url.openConnection();<br \/>&nbsp;&nbsp; ((HttpsURLConnection) con).setHostnameVerifier(DO_NOT_VERIFY);<br \/>&nbsp; &nbsp;<br \/>&nbsp; } else {<br \/>&nbsp;&nbsp; con = (HttpURLConnection) url.openConnection();<br \/>&nbsp; }<br \/>&nbsp; <br \/>&nbsp; return con;<br \/>&nbsp;}<br \/>&nbsp;<br \/>&nbsp;private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {<br \/>&nbsp; @Override<br \/>&nbsp; public boolean verify(String hostname, SSLSession session) {<br \/>&nbsp;&nbsp; return true;<br \/>&nbsp; }<br \/>&nbsp;};<br \/>&nbsp; &nbsp; &nbsp;<br \/>&nbsp;private static void trustAllHosts() {<br \/>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<br \/>&nbsp; trusted = true;<br \/>&nbsp; <br \/>&nbsp; \/\/ Create a trust manager that does not validate certificate chains<br \/>&nbsp; &nbsp; &nbsp; &nbsp; TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; public java.security.cert.X509Certificate[] getAcceptedIssuers() {<br \/>&nbsp; &nbsp; &nbsp;&nbsp; return new java.security.cert.X509Certificate[] {};<br \/>&nbsp; &nbsp; &nbsp; }<br \/>&nbsp; <br \/>&nbsp; &nbsp; &nbsp; @Override<br \/>&nbsp; &nbsp; &nbsp; public void checkClientTrusted (java.security.cert.X509Certificate[] chain, String authType)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws java.security.cert.CertificateException { }<br \/>&nbsp; <br \/>&nbsp; &nbsp; &nbsp; @Override<br \/>&nbsp; &nbsp; &nbsp; public void checkServerTrusted (java.security.cert.X509Certificate[] chain, String authType)<br \/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws java.security.cert.CertificateException { }<br \/>&nbsp; &nbsp;&nbsp; &nbsp;<br \/>&nbsp; &nbsp; &nbsp; &nbsp; } };<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Install the all-trusting trust manager<br \/>&nbsp; &nbsp; &nbsp; &nbsp; try {<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; SSLContext sc = SSLContext.getInstance(&#8220;TLS&#8221;);<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; sc.init(null, trustAllCerts, new java.security.SecureRandom());<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());<br \/>&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {<br \/>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; e.printStackTrace();<br \/>&nbsp; &nbsp; &nbsp; &nbsp; }<br \/>&nbsp; &nbsp; }<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JAVA\uc5d0\uc11c SSL Client\ub97c \uad6c\ud604\ud558\uace0 \uc778\uc99d\uc11c \uc5c6\uc774 \uc811\uadfc \ud560 \uc218 \uc788\ub3c4\ub85d \uad6c\ud604\ud55c \uc608\uc81c \uc785\ub2c8\ub2e4. public class URLUtils {&nbsp;&nbsp;private static boolean trusted = false; &nbsp;public static HttpURLConnection getConnection(URL url) throws IOException {&nbsp; &nbsp; HttpURLConnection con = null;&nbsp; boolean https = url.getProtocol().equalsIgnoreCase(&#8220;https&#8221;) ? true : false;&nbsp; &nbsp; if (https) {&nbsp;&nbsp; if (!trusted) trustAllHosts();&nbsp;&nbsp; con = (HttpsURLConnection) url.openConnection();&nbsp;&nbsp; ((HttpsURLConnection) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[3309,3310,3311,162,3312],"class_list":["post-1776","post","type-post","status-publish","format-standard","hentry","category-about-knowledge-programs_java","tag-allow_all","tag-faketrusted","tag-htts","tag-java","tag-ssl"],"_links":{"self":[{"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/posts\/1776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/comments?post=1776"}],"version-history":[{"count":0,"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/posts\/1776\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/media?parent=1776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/categories?post=1776"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.msinterdev.org\/wp\/wp-json\/wp\/v2\/tags?post=1776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}