Skip to content

Commit

Permalink
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
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
@@ -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;
}
}
}
Expand Up @@ -34,7 +34,15 @@
</property> </property>
</bean> </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"> <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="defaultMaxPerRoute" value="${http.max.connection.per.route:5}"/>
<property name="maxTotal" value="${http.max.connection.total:10}"/> <property name="maxTotal" value="${http.max.connection.total:10}"/>
</bean> </bean>
Expand Down

0 comments on commit c840962

Please sign in to comment.