Commit 9f588e72 authored by Quxl's avatar Quxl

x

parent 02b6810c
package com.egolm.sso.clients; package com.egolm.sso.clients;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.security.KeyStore;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.callback.Callback; import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler; import javax.security.auth.callback.CallbackHandler;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
...@@ -13,7 +20,10 @@ import javax.xml.ws.Service; ...@@ -13,7 +20,10 @@ import javax.xml.ws.Service;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor; import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.handler.WSHandlerConstants; import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.wss4j.common.ext.WSPasswordCallback; import org.apache.wss4j.common.ext.WSPasswordCallback;
...@@ -25,23 +35,37 @@ import org.springframework.stereotype.Component; ...@@ -25,23 +35,37 @@ import org.springframework.stereotype.Component;
public class SAPServiceFactory { public class SAPServiceFactory {
Log logger = LogFactory.getLog(SAPServiceFactory.class); Log logger = LogFactory.getLog(SAPServiceFactory.class);
@Value("${schneider.username}") @Value("${schneider.username}")
private String username; private String username;
@Value("${schneider.password}") @Value("${schneider.password}")
private String password; private String password;
@Value("CLIENT_KEYSTORE.FILEPATH")
private String keyStoreFile;
@Value("CLIENT_KEYSTORE.PASSWORD")
private String keyStorePassword;
public <T> T create(Class<T> requiredType, URL wsdlLocation, QName serviceQName) { public <T> T create(Class<T> requiredType, URL wsdlLocation, QName serviceQName) {
assert requiredType != null : "WebService requiredType cannot be null"; assert requiredType != null : "WebService requiredType cannot be null";
assert wsdlLocation != null : "WebService wsdlLocation cannot be null"; assert wsdlLocation != null : "WebService wsdlLocation cannot be null";
assert serviceQName != null : "WebService serviceQName cannot be null"; assert serviceQName != null : "WebService serviceQName cannot be null";
Service dyService = Service.create(wsdlLocation, serviceQName); Service dyService = Service.create(wsdlLocation, serviceQName);
T service = dyService.getPort(requiredType); T service = dyService.getPort(requiredType);
ClientProxy.getClient(service).getOutInterceptors().add(getWSS4JOutInterceptor()); Client client = ClientProxy.getClient(service);
client.getOutInterceptors().add(this.getWSS4JOutInterceptor());
HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
TLSClientParameters tlsParams = httpConduit.getTlsClientParameters();
tlsParams = tlsParams == null ? new TLSClientParameters() : tlsParams;
tlsParams.setSecureSocketProtocol("SSL");
tlsParams.setKeyManagers(getKeyManagers());
tlsParams.setTrustManagers(getTrustManagers());
httpConduit.setTlsClientParameters(tlsParams);
return service; return service;
} }
private static String absolutePath = null; private static String absolutePath = null;
public URL getAbsoluteURL(String WSDLPATH) throws MalformedURLException { public URL getAbsoluteURL(String WSDLPATH) throws MalformedURLException {
...@@ -78,5 +102,61 @@ public class SAPServiceFactory { ...@@ -78,5 +102,61 @@ public class SAPServiceFactory {
public enum PasswordType { public enum PasswordType {
PasswordText, PasswordNone, PasswordDigest PasswordText, PasswordNone, PasswordDigest
} }
private static TrustManager[] trustManagers = null;
private static KeyManager[] keyManagers = null;
private TrustManager[] getTrustManagers() {
if(trustManagers == null) {
FileInputStream fis = null;
try {
String alg = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory factory = TrustManagerFactory.getInstance(alg);
fis = new FileInputStream(new File(keyStoreFile));
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(fis, keyStorePassword.toCharArray());
fis.close();
factory.init(ks);
trustManagers = factory.getTrustManagers();
} catch (Exception e) {
logger.error("Load TrustManager fail [" + keyStoreFile + "]", e);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
logger.error(keyStoreFile, e);
}
}
}
}
return trustManagers;
}
private KeyManager[] getKeyManagers() {
if(keyManagers == null) {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(keyStoreFile));
String alg = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory factory = KeyManagerFactory.getInstance(alg);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(fis, keyStorePassword.toCharArray());
factory.init(ks, keyStorePassword.toCharArray());
keyManagers = factory.getKeyManagers();
} catch (Exception e) {
logger.error("Load KeyManager fail [" + keyStoreFile + "]", e);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
logger.error(keyStoreFile, e);
}
}
}
}
return keyManagers;
}
} }
...@@ -9,7 +9,6 @@ import java.util.Map; ...@@ -9,7 +9,6 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.frontend.ClientProxy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -66,8 +65,7 @@ public class SI004INVENTORYSyncOutServiceTask { ...@@ -66,8 +65,7 @@ public class SI004INVENTORYSyncOutServiceTask {
} }
public void runTask() throws MalformedURLException { public void runTask() throws MalformedURLException {
SI004INVENTORYSyncOut service = new SI004INVENTORYSyncOutService(factory.getAbsoluteURL(WSDLPATH)).getPort(SI004INVENTORYSyncOut.class); SI004INVENTORYSyncOut service = factory.create(SI004INVENTORYSyncOut.class, factory.getAbsoluteURL(WSDLPATH), SI004INVENTORYSyncOutService.SERVICE);
ClientProxy.getClient(service).getOutInterceptors().add(factory.getWSS4JOutInterceptor());
this.sendData(service); this.sendData(service);
} }
......
...@@ -9,7 +9,6 @@ import java.util.Map; ...@@ -9,7 +9,6 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.frontend.ClientProxy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -65,8 +64,7 @@ public class SI009SOCREATIONAsynOutServiceTask { ...@@ -65,8 +64,7 @@ public class SI009SOCREATIONAsynOutServiceTask {
} }
public void runTask() throws MalformedURLException { public void runTask() throws MalformedURLException {
SI009SOCREATIONAsynOut service = new SI009SOCREATIONAsynOutService(factory.getAbsoluteURL(WSDLPATH)).getPort(SI009SOCREATIONAsynOut.class); SI009SOCREATIONAsynOut service = factory.create(SI009SOCREATIONAsynOut.class, factory.getAbsoluteURL(WSDLPATH), SI009SOCREATIONAsynOutService.SERVICE);
ClientProxy.getClient(service).getOutInterceptors().add(factory.getWSS4JOutInterceptor());
this.sendData(service); this.sendData(service);
} }
......
...@@ -9,7 +9,6 @@ import java.util.Map; ...@@ -9,7 +9,6 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.frontend.ClientProxy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
...@@ -66,8 +65,7 @@ public class SI011ACTUALSALESSyncOutServiceTask { ...@@ -66,8 +65,7 @@ public class SI011ACTUALSALESSyncOutServiceTask {
} }
public void runTask() throws MalformedURLException { public void runTask() throws MalformedURLException {
SI011ACTUALSALESSyncOut service = new SI011ACTUALSALESSyncOutService(factory.getAbsoluteURL(WSDLPATH)).getPort(SI011ACTUALSALESSyncOut.class); SI011ACTUALSALESSyncOut service = factory.create(SI011ACTUALSALESSyncOut.class, factory.getAbsoluteURL(WSDLPATH), SI011ACTUALSALESSyncOutService.SERVICE);
ClientProxy.getClient(service).getOutInterceptors().add(factory.getWSS4JOutInterceptor());
this.sendData(service); this.sendData(service);
} }
......
CLIENT_KEYSTORE:
FILEPATH: SSL/sap.keystore
PASSWORD: 123456
isUsedTestController: true isUsedTestController: true
xmlRoot: xml xmlRoot: xml
systemId: system systemId: system
...@@ -34,6 +37,4 @@ spring: ...@@ -34,6 +37,4 @@ spring:
cxf: cxf:
path: /api/services path: /api/services
servlet: servlet:
load-on-startup: -1 load-on-startup: -1
logging: \ No newline at end of file
file: logs/spring.log
\ No newline at end of file
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