Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support for untrusted website in jspringbot-http
- Loading branch information
badong2210
committed
Mar 5, 2013
1 parent
961b75b
commit c840962
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
jspringbot-http/src/main/java/org/jspringbot/keyword/http/SchemeRegistryBean.java
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
package org.jspringbot.keyword.http; | |||
|
|||
import org.apache.http.conn.scheme.PlainSocketFactory; | |||
import org.apache.http.conn.scheme.Scheme; | |||
import org.apache.http.conn.scheme.SchemeRegistry; | |||
import org.apache.http.conn.ssl.AllowAllHostnameVerifier; | |||
import org.apache.http.conn.ssl.SSLSocketFactory; | |||
import org.apache.http.conn.ssl.TrustStrategy; | |||
|
|||
import java.security.KeyManagementException; | |||
import java.security.KeyStoreException; | |||
import java.security.NoSuchAlgorithmException; | |||
import java.security.UnrecoverableKeyException; | |||
import java.security.cert.CertificateException; | |||
import java.security.cert.X509Certificate; | |||
|
|||
public class SchemeRegistryBean { | |||
|
|||
private SchemeRegistry registry; | |||
|
|||
public SchemeRegistryBean(SchemeRegistry registry) { | |||
this.registry = registry; | |||
} | |||
|
|||
public void setAllowUntrusted(boolean allowUntrusted) throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { | |||
registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); | |||
|
|||
if(allowUntrusted) { | |||
SSLSocketFactory sf = new SSLSocketFactory(new TrustAllStrategy(), new AllowAllHostnameVerifier()); | |||
registry.register(new Scheme("https", 443, sf)); | |||
} else { | |||
registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); | |||
} | |||
} | |||
|
|||
public static class TrustAllStrategy implements TrustStrategy { | |||
@Override | |||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { | |||
return true; | |||
} | |||
} | |||
} |
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