Commit fd3ad44b authored by Quxl's avatar Quxl

x

parent 384c3271
......@@ -3,14 +3,21 @@ package com.egolm.sso.clients;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.handler.WSHandlerConstants;
......@@ -24,12 +31,18 @@ import com.egolm.sso.config.XRException;
@Component
public class SAPServiceFactory {
Log logger = LogFactory.getLog(SAPServiceFactory.class);
@Value("${schneider.username}")
private String username;
@Value("${schneider.password}")
private String password;
public SAPServiceFactory() throws KeyManagementException, NoSuchAlgorithmException {
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
public <T> T create(Class<T> requiredType, String WSDLPATH, QName serviceQName, QName portQName) {
assert requiredType != null : "WebService requiredType cannot be null";
......@@ -38,18 +51,19 @@ public class SAPServiceFactory {
try {
URL WSDL_URL = new URL(this.getAbsolutePath(WSDLPATH));
Service dyService = Service.create(WSDL_URL, serviceQName);
T service = portQName == null ? dyService.getPort(requiredType) : dyService.getPort(portQName, requiredType);
T service = portQName == null ? dyService.getPort(requiredType)
: dyService.getPort(portQName, requiredType);
ClientProxy.getClient(service).getOutInterceptors().add(getWSS4JOutInterceptor());
return service;
} catch (MalformedURLException e) {
throw new XRException(e);
}
}
private static String absolutePath = null;
private String getAbsolutePath(String WSDLPATH) {
if(absolutePath == null) {
if (absolutePath == null) {
ApplicationHome home = new ApplicationHome(getClass());
File jarFile = home.getSource();
File folder = jarFile.getParentFile();
......@@ -57,11 +71,11 @@ public class SAPServiceFactory {
}
return "file:///" + absolutePath + "/" + WSDLPATH;
}
private WSS4JOutInterceptor wss4JOutInterceptor = null;
public WSS4JOutInterceptor getWSS4JOutInterceptor() {
if(wss4JOutInterceptor == null) {
if (wss4JOutInterceptor == null) {
Map<String, Object> pro = new HashMap<String, Object>();
pro.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
pro.put(WSHandlerConstants.USER, username);
......@@ -78,8 +92,48 @@ public class SAPServiceFactory {
}
return wss4JOutInterceptor;
}
public enum PasswordType {
PasswordText, PasswordNone, PasswordDigest
}
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
logger.warn("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
return true;
}
};
private void trustAllHttpsCertificates() throws KeyManagementException, NoSuchAlgorithmException {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment