Skip to content

Commit

Permalink
Updated to support SelfSigned Certs
Browse files Browse the repository at this point in the history
  • Loading branch information
sa-rrowcliffe committed May 5, 2016
1 parent d804210 commit eacaea7
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions src/main/java/org/secureauth/sarestapi/resources/SAExecuter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public class SAExecuter {
private ClientConfig config = null;
private Client client=null;
private static Logger logger=LoggerFactory.getLogger(SAExecuter.class);

//Set up our Connection
private void createConnection() throws Exception{

config = new ClientConfig();
SSLContext ctx = null;
ctx = SSLContext.getInstance("TLS");


TrustManager[] certs = new TrustManager[]{
new X509TrustManager(){
Expand All @@ -73,44 +76,24 @@ public void checkClientTrusted(X509Certificate[] chain, String authType) throws
}
};

SSLContext ctx = null;
ctx.init(null, certs, new SecureRandom());

try{
ctx = SSLContext.getInstance("TLS");
ctx.init(null, certs, new SecureRandom());
}catch(java.security.GeneralSecurityException ex){
logger.error(new StringBuilder().append("Exception occurred while attempting to setup SSL security. ").toString(), ex);
}


HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

try{
client = ClientBuilder.newBuilder()
client = ClientBuilder.newBuilder()
.withConfig(config)
.sslContext(ctx)
.hostnameVerifier(
new HostnameVerifier(){
@Override
public boolean verify(String hostname, SSLSession session){return true;}
}
)
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
})
.build();

}catch(Exception e){
logger.error(new StringBuilder().append("Exception occurred while attempting to associating our SSL cert to the session.").toString(), e);
}

try{
client = ClientBuilder.newClient(config);
}catch(Exception e){
StringBuilder bud = new StringBuilder();
for(StackTraceElement st: e.getStackTrace()){
bud.append(st.toString()).append("\n");
}
throw new Exception(new StringBuilder().append("Exception occurred while attempting to create connection object. Exception: ")
.append(e.getMessage()).append("\nStackTraceElements:\n").append(bud.toString()).toString());
}

if(client == null) throw new Exception(new StringBuilder().append("Unable to create connection object, creation attempt returned NULL.").toString());
}

Expand Down

0 comments on commit eacaea7

Please sign in to comment.