try to handle SSL stuff.
This commit is contained in:
@@ -85,8 +85,8 @@ dependencies {
|
|||||||
[group: 'joda-time', name: 'joda-time', version: '2.1'],
|
[group: 'joda-time', name: 'joda-time', version: '2.1'],
|
||||||
[group: 'commons-lang', name: 'commons-lang', version: '2.4'],
|
[group: 'commons-lang', name: 'commons-lang', version: '2.4'],
|
||||||
[group: 'commons-io', name: 'commons-io', version: '1.4'],
|
[group: 'commons-io', name: 'commons-io', version: '1.4'],
|
||||||
[group: 'commons-httpclient', name: 'commons-httpclient', version: '3.1'],
|
[group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.1.3'],
|
||||||
|
|
||||||
[group: 'org.slf4j', name: 'slf4j-api', version: '1.6.4'],
|
[group: 'org.slf4j', name: 'slf4j-api', version: '1.6.4'],
|
||||||
[group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.6.4'],
|
[group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.6.4'],
|
||||||
[group: 'ch.qos.logback', name: 'logback-core', version: '1.0.1'],
|
[group: 'ch.qos.logback', name: 'logback-core', version: '1.0.1'],
|
||||||
|
|||||||
+3
-2
@@ -20,6 +20,7 @@ import org.springframework.security.core.context.SecurityContext;
|
|||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link AbstractUserDetailsAuthenticationProvider} which is responsible for delegating authentication to the mifos platform API.
|
* A {@link AbstractUserDetailsAuthenticationProvider} which is responsible for delegating authentication to the mifos platform API.
|
||||||
@@ -29,11 +30,11 @@ import org.springframework.web.client.HttpClientErrorException;
|
|||||||
*/
|
*/
|
||||||
public class ApiDelegatingAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
|
public class ApiDelegatingAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
|
||||||
|
|
||||||
private final SslRestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final ApplicationConfigurationService applicationConfigurationService;
|
private final ApplicationConfigurationService applicationConfigurationService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ApiDelegatingAuthenticationProvider(final SslRestTemplate restTemplate, final ApplicationConfigurationService applicationConfigurationService) {
|
public ApiDelegatingAuthenticationProvider(final RestTemplate restTemplate, final ApplicationConfigurationService applicationConfigurationService) {
|
||||||
this.restTemplate = restTemplate;
|
this.restTemplate = restTemplate;
|
||||||
this.applicationConfigurationService = applicationConfigurationService;
|
this.applicationConfigurationService = applicationConfigurationService;
|
||||||
}
|
}
|
||||||
|
|||||||
-71
@@ -1,71 +0,0 @@
|
|||||||
package org.mifosng.ui.infrastructure;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
|
|
||||||
import javax.net.ssl.HostnameVerifier;
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
|
||||||
import javax.net.ssl.SSLContext;
|
|
||||||
import javax.net.ssl.SSLSession;
|
|
||||||
import javax.net.ssl.TrustManager;
|
|
||||||
import javax.net.ssl.X509TrustManager;
|
|
||||||
|
|
||||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class CustomClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void prepareConnection(HttpURLConnection connection,
|
|
||||||
String httpMethod) throws IOException {
|
|
||||||
|
|
||||||
if (connection instanceof HttpsURLConnection) {
|
|
||||||
disableCertificateValidation();
|
|
||||||
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
|
|
||||||
httpsConnection.setHostnameVerifier(new NullHostnameVerifier());
|
|
||||||
}
|
|
||||||
super.prepareConnection(connection, httpMethod);
|
|
||||||
}
|
|
||||||
|
|
||||||
private class NullHostnameVerifier implements HostnameVerifier {
|
|
||||||
@Override
|
|
||||||
public boolean verify(String hostname, SSLSession session) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void disableCertificateValidation() {
|
|
||||||
|
|
||||||
X509TrustManager trustManager = new X509TrustManager() {
|
|
||||||
@Override
|
|
||||||
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
TrustManager[] trustAllCerts = new TrustManager[1];
|
|
||||||
trustAllCerts[0] = trustManager;
|
|
||||||
|
|
||||||
try {
|
|
||||||
SSLContext sc = SSLContext.getInstance("SSL");
|
|
||||||
sc.init(null, trustAllCerts, new SecureRandom());
|
|
||||||
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
|
|
||||||
} catch (Exception e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+62
@@ -0,0 +1,62 @@
|
|||||||
|
package org.mifosng.ui.infrastructure;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.UnrecoverableKeyException;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
|
||||||
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
|
|
||||||
|
public class MySSLSocketFactory extends SSLSocketFactory {
|
||||||
|
|
||||||
|
private SSLContext sslContext;
|
||||||
|
|
||||||
|
public MySSLSocketFactory(SSLContext sslContext, KeyStore truststore)
|
||||||
|
throws NoSuchAlgorithmException, KeyManagementException,
|
||||||
|
KeyStoreException, UnrecoverableKeyException {
|
||||||
|
super(truststore);
|
||||||
|
this.sslContext = sslContext;
|
||||||
|
|
||||||
|
TrustManager tm = new X509TrustManager() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkServerTrusted(
|
||||||
|
java.security.cert.X509Certificate[] chain, String authType)
|
||||||
|
throws java.security.cert.CertificateException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkClientTrusted(
|
||||||
|
java.security.cert.X509Certificate[] chain, String authType)
|
||||||
|
throws java.security.cert.CertificateException {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
sslContext.init(null, new TrustManager[] { tm }, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(Socket socket, String host, int port,
|
||||||
|
boolean autoClose) throws IOException, UnknownHostException {
|
||||||
|
return sslContext.getSocketFactory().createSocket(socket, host, port,
|
||||||
|
autoClose);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket() throws IOException {
|
||||||
|
return sslContext.getSocketFactory().createSocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
+109
@@ -0,0 +1,109 @@
|
|||||||
|
package org.mifosng.ui.infrastructure;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStore;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.UnrecoverableKeyException;
|
||||||
|
import java.security.cert.CertificateException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
|
import javax.net.ssl.SSLSession;
|
||||||
|
import javax.net.ssl.SSLSocket;
|
||||||
|
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
|
import org.apache.http.conn.scheme.Scheme;
|
||||||
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
|
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RestTemplateFactory implements FactoryBean<RestTemplate>,
|
||||||
|
InitializingBean {
|
||||||
|
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RestTemplate getObject() {
|
||||||
|
return restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<RestTemplate> getObjectType() {
|
||||||
|
return RestTemplate.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
||||||
|
HttpClient client = requestFactory.getHttpClient();
|
||||||
|
ClientConnectionManager clientConnectionManager = client.getConnectionManager();
|
||||||
|
SchemeRegistry registry = clientConnectionManager.getSchemeRegistry();
|
||||||
|
|
||||||
|
Scheme oldScheme = registry.unregister("https");
|
||||||
|
|
||||||
|
KeyStore trustStore = KeyStore.getInstance(KeyStore
|
||||||
|
.getDefaultType());
|
||||||
|
|
||||||
|
trustStore.load(null, null);
|
||||||
|
|
||||||
|
X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {
|
||||||
|
@Override
|
||||||
|
public boolean verify(String arg0, SSLSession arg1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(String host, String[] cns, String[] subjectAlts)
|
||||||
|
throws SSLException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(String host, X509Certificate cert) throws SSLException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void verify(String host, SSLSocket ssl) throws IOException {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SSLContext context = SSLContext.getInstance("TLS");
|
||||||
|
SSLSocketFactory factory = new MySSLSocketFactory(context, trustStore);
|
||||||
|
factory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
|
factory.setHostnameVerifier(hostnameVerifier);
|
||||||
|
|
||||||
|
Scheme newHttpsScheme = new Scheme("https", 8443, factory);
|
||||||
|
registry.register(newHttpsScheme);
|
||||||
|
|
||||||
|
restTemplate = new RestTemplate(requestFactory);
|
||||||
|
} catch (KeyStoreException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (CertificateException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (KeyManagementException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (UnrecoverableKeyException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
-17
@@ -1,17 +0,0 @@
|
|||||||
package org.mifosng.ui.infrastructure;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class SslRestTemplate extends RestTemplate {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public SslRestTemplate(final CustomClientHttpRequestFactory simpleClientHttpRequestFactory) {
|
|
||||||
super(simpleClientHttpRequestFactory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user