<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Using a self signed certificate with SSL"]]></title>
		<link>http://forums.hotjoe.com/posts/list/27.page</link>
		<description><![CDATA[Latest messages posted in the topic "Using a self signed certificate with SSL"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Using a self signed certificate with SSL</title>
				<description><![CDATA[ One problem people have with Java and SSL is that the certificate manager will reject any self signed certificates.  This is common when you are doing development or are using an internal certificate that you don't want to pay money for from a commercial certificate authority.<br /> <br /> One way to handle this is to simply accept any certificate, regardless of what it has in it.  Below is a simple program to do just that - it reads a URL and, regardless of what the certificate says, prints out the contents of the URL.  Note that this is a bit dangerous to use in a production environment so be careful.<br /> <br /> I'll also attach a zip file with a build.xml so that you can build the program with ant.<br /> <br /> [code]<br /> package com.xigole.util.ssl;<br /> <br /> import java.io.IOException;<br /> import java.io.InputStream;<br /> import java.net.MalformedURLException;<br /> import java.net.URL;<br /> import java.net.URLConnection;<br /> <br /> import javax.net.ssl.HttpsURLConnection;<br /> import javax.net.ssl.SSLContext;<br /> import javax.net.ssl.TrustManager;<br /> import javax.net.ssl.X509TrustManager;<br /> <br /> public class SelfSignedCertTest {<br /> 	public static void main(String[] argv) {<br /> 		if (argv.length != 1) {<br /> 			System.err.println("usage: SelfSignedCertTest hostname");<br /> 			System.exit(1);<br /> 		}<br /> <br /> 		TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {<br /> 			public java.security.cert.X509Certificate[] getAcceptedIssuers() {<br /> 				return null;<br /> 			}<br /> <br /> 			public void checkClientTrusted(<br /> 					java.security.cert.X509Certificate[] certs, String authType) {<br /> 			}<br /> <br /> 			public void checkServerTrusted(<br /> 					java.security.cert.X509Certificate[] certs, String authType) {<br /> 				System.out.println("authType is " + authType);<br /> 				System.out.println("cert issuers");<br /> 				for (int i = 0; i &lt; certs.length; i++) {<br /> 					System.out.println("\t" + certs[i].getIssuerX500Principal().getName());<br /> 					System.out.println("\t" + certs[i].getIssuerDN().getName());<br /> 		  	   }<br /> 			}<br /> 		} };<br /> <br /> 		try {<br /> 			SSLContext sc = SSLContext.getInstance("SSL");<br /> 			sc.init(null, trustAllCerts, new java.security.SecureRandom());<br /> 			HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());<br /> 		} catch (Exception e) {<br /> 			e.printStackTrace();<br /> 			System.exit(1);<br /> 		}<br /> <br /> 		URL url = null;<br /> 		try {<br /> 			url = new URL("https", argv[0], 443, "/");<br /> 		} catch (MalformedURLException mue) {<br /> 			mue.printStackTrace();<br /> 			System.exit(1);<br /> 		}<br /> <br /> 		try {<br /> 			URLConnection connection = url.openConnection();<br /> <br /> 			InputStream is = connection.getInputStream();<br /> 			byte[] buffer = new byte[1024];<br /> 			int bytesRead = 0;<br /> <br /> 			while ((bytesRead = is.read(buffer)) != -1)<br /> 				System.out.println(new String(buffer, 0, bytesRead));<br /> 		} catch (IOException ioe) {<br /> 			ioe.printStackTrace();<br /> 			System.exit(1);<br /> 		}<br /> 	}<br /> }<br /> [/code]<br /> <br /> Note that this is almost the exact same code I posted in [url=http://forums.devshed.com/java-help-9/login-and-download-file-over-https-360178.html]a Devshed thread[/url] but the attachment has a complete build environment too.<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://forums.hotjoe.com/posts/preList/290/1235.page</guid>
				<link>http://forums.hotjoe.com/posts/preList/290/1235.page</link>
				<pubDate><![CDATA[Thu, 15 Jun 2006 11:47:01]]> GMT</pubDate>
				<author><![CDATA[ stdunbar]]></author>
			</item>
	</channel>
</rss>
