Last active
August 29, 2015 14:00
-
-
Save andi-bigswitch/11070102 to your computer and use it in GitHub Desktop.
Restlet client without certificate validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.cert.CertificateException; | |
import java.security.cert.X509Certificate; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.TrustManager; | |
import javax.net.ssl.X509TrustManager; | |
import org.restlet.Context; | |
import org.restlet.ext.ssl.DefaultSslContextFactory; | |
import org.restlet.ext.ssl.SslContextFactory; | |
import org.restlet.representation.Representation; | |
import org.restlet.representation.StringRepresentation; | |
import org.restlet.resource.ClientResource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
class TrustEveryOneManager implements X509TrustManager { | |
static final TrustEveryOneManager INSTANCE = new TrustEveryOneManager(); | |
private final static X509Certificate[] noCerts = new X509Certificate[0]; | |
@Override | |
public void checkClientTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
// we trust it - return | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] chain, String authType) | |
throws CertificateException { | |
// we trust it - return | |
} | |
@Override | |
public X509Certificate[] getAcceptedIssuers() { | |
return noCerts; | |
} | |
} | |
class TrustingSslContextFactory extends DefaultSslContextFactory { | |
@Override | |
public SSLContext createSslContext() throws Exception { | |
final SSLContext sslContext = SSLContext.getInstance("TLS"); | |
sslContext.init(null, new TrustManager[] { TrustEveryOneManager.INSTANCE }, null); | |
return createWrapper(sslContext); | |
} | |
} | |
public class TestRestletSSL { | |
private static final Logger logger = | |
LoggerFactory.getLogger(TestRestletSSL.class); | |
final static SslContextFactory sslContextFactory = new TrustingSslContextFactory(); | |
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException { | |
Context clientContext = new Context(); | |
clientContext.getAttributes().put("sslContextFactory", sslContextFactory); | |
ClientResource resource = new ClientResource(clientContext, "https://1.2.3.4/reboot"); | |
// ClientResource resource = new ClientResource("https://10.192.75.242/api/switch/reboot"); | |
Representation representation = resource.post(new StringRepresentation("delay")); | |
String text = representation.getText(); | |
logger.info("got text: {} ", text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment