Permalink
Browse files

Support for untrusted website in jspringbot-http

  • Loading branch information...
1 parent 961b75b commit c84096204cb0e63877d130f416a99203e0345a6b @badong2210 badong2210 committed Mar 5, 2013
@@ -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;
+ }
+ }
+}
@@ -34,7 +34,15 @@
</property>
</bean>
+ <bean name="schemeRegistry" class="org.apache.http.conn.scheme.SchemeRegistry"/>
+
+ <bean class="org.jspringbot.keyword.http.SchemeRegistryBean">
+ <constructor-arg ref="schemeRegistry"/>
+ <property name="allowUntrusted" value="${http.allow.untrusted:true}"/>
+ </bean>
+
<bean name="connectionManager" class="org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager">
+ <constructor-arg ref="schemeRegistry"/>
<property name="defaultMaxPerRoute" value="${http.max.connection.per.route:5}"/>
<property name="maxTotal" value="${http.max.connection.total:10}"/>
</bean>

0 comments on commit c840962

Please sign in to comment.