Browse Source

1.增加http工具类,使RestTemplate支持发送https
2.修改地址为正式地址

will.chen 7 years ago
parent
commit
9bdb976b9c

+ 126 - 0
src/main/java/com/uas/erp/schedular/finance/mq/HttpsClientRequestFactory.java

@@ -0,0 +1,126 @@
+package com.uas.erp.schedular.finance.mq;
+
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+
+import javax.net.ssl.*;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.security.cert.X509Certificate;
+
+public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
+
+    @Override
+    protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
+        try {
+            if (!(connection instanceof HttpsURLConnection)) {
+                throw new RuntimeException("An instance of HttpsURLConnection is expected");
+            }
+
+            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
+
+            TrustManager[] trustAllCerts = new TrustManager[]{
+                    new X509TrustManager() {
+                        @Override
+                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                            return null;
+                        }
+                        @Override
+                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
+                        }
+                        @Override
+                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
+                        }
+
+                    }
+            };
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
+            httpsConnection.setSSLSocketFactory(new MyCustomSSLSocketFactory(sslContext.getSocketFactory()));
+
+            httpsConnection.setHostnameVerifier(new HostnameVerifier() {
+                @Override
+                public boolean verify(String s, SSLSession sslSession) {
+                    return true;
+                }
+            });
+
+            super.prepareConnection(httpsConnection, httpMethod);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * We need to invoke sslSocket.setEnabledProtocols(new String[] {"SSLv3"});
+     * see http://www.oracle.com/technetwork/java/javase/documentation/cve-2014-3566-2342133.html (Java 8 section)
+     */
+    // SSLSocketFactory用于创建 SSLSockets
+    private static class MyCustomSSLSocketFactory extends SSLSocketFactory {
+
+        private final SSLSocketFactory delegate;
+
+        public MyCustomSSLSocketFactory(SSLSocketFactory delegate) {
+            this.delegate = delegate;
+        }
+
+        // 返回默认启用的密码套件。除非一个列表启用,对SSL连接的握手会使用这些密码套件。
+        // 这些默认的服务的最低质量要求保密保护和服务器身份验证
+        @Override
+        public String[] getDefaultCipherSuites() {
+            return delegate.getDefaultCipherSuites();
+        }
+
+        // 返回的密码套件可用于SSL连接启用的名字
+        @Override
+        public String[] getSupportedCipherSuites() {
+            return delegate.getSupportedCipherSuites();
+        }
+
+
+        @Override
+        public Socket createSocket(final Socket socket, final String host, final int port,
+                                   final boolean autoClose) throws IOException {
+            final Socket underlyingSocket = delegate.createSocket(socket, host, port, autoClose);
+            return overrideProtocol(underlyingSocket);
+        }
+
+
+        @Override
+        public Socket createSocket(final String host, final int port) throws IOException {
+            final Socket underlyingSocket = delegate.createSocket(host, port);
+            return overrideProtocol(underlyingSocket);
+        }
+
+        @Override
+        public Socket createSocket(final String host, final int port, final InetAddress localAddress,
+                                   final int localPort) throws
+                IOException {
+            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
+            return overrideProtocol(underlyingSocket);
+        }
+
+        @Override
+        public Socket createSocket(final InetAddress host, final int port) throws IOException {
+            final Socket underlyingSocket = delegate.createSocket(host, port);
+            return overrideProtocol(underlyingSocket);
+        }
+
+        @Override
+        public Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress,
+                                   final int localPort) throws
+                IOException {
+            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
+            return overrideProtocol(underlyingSocket);
+        }
+
+        private Socket overrideProtocol(final Socket socket) {
+            if (!(socket instanceof SSLSocket)) {
+                throw new RuntimeException("An instance of SSLSocket is expected");
+            }
+            ((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.2"});
+            return socket;
+        }
+    }
+}

+ 8 - 15
src/main/java/com/uas/erp/schedular/finance/mq/RabbitReceiveServiceImpl.java

@@ -1,13 +1,10 @@
 package com.uas.erp.schedular.finance.mq;
 
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.TypeReference;
 import com.uas.erp.schedular.database.RestJdbcTemplate;
 import com.uas.erp.schedular.entity.Master;
 import com.uas.erp.schedular.entity.MqConfig;
-import com.uas.erp.schedular.service.MasterService;
-import com.uas.erp.schedular.service.SettingService;
 import com.uas.erp.schedular.util.ContextHolder;
 import com.uas.erp.schedular.util.HmacUtils;
 import com.uas.erp.schedular.web.ResultWrap;
@@ -15,41 +12,34 @@ import com.usoft.mq.utils.BaseRabbitReceiveService;
 import com.usoft.mq.utils.MessageInfo;
 import com.usoft.security.utils.OpenApiSignUtil;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.annotation.Order;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
-import org.springframework.util.Assert;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.util.DefaultUriTemplateHandler;
 
 import javax.annotation.PostConstruct;
-import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
 @Service
 public class RabbitReceiveServiceImpl extends BaseRabbitReceiveService {
 
-
     @Autowired
     private RestTemplate restTemplate;
     @Autowired
     private RestJdbcTemplate jdbcTemplate;
-    @Autowired
-    private SettingService settingService;
 
     private final static String SECRETKEY = "600d3f07955ba67fe050007f01002db2";
-    private final static String BASEPATH = "http://192.168.253.3:26600";
+    private final static String BASEPATH = "https://finrest.usofchina.com";
     private final static String UASURL = "http://218.18.115.198:8888/ERP";
+//    private final static String UASURL = "http://192.168.253.41:8080/ERP";
     private final static String MASTERNAME = "YITOA_BL";
+//    private final static String MASTERNAME = "YITOA_BL_BAKCUP";
 
     @PostConstruct
     public void init() throws InterruptedException {
@@ -127,8 +117,11 @@ public class RabbitReceiveServiceImpl extends BaseRabbitReceiveService {
 
         DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
         uriTemplateHandler.setStrictEncoding(true);
-        restTemplate.setUriTemplateHandler(uriTemplateHandler);
-        String result = restTemplate.getForObject(requestURL, String.class);
+
+        RestTemplate restTemp = new RestTemplate(new HttpsClientRequestFactory());
+        restTemp.setUriTemplateHandler(uriTemplateHandler);
+        String result = restTemp.getForObject(requestURL, String.class);
+
         String UASurl = UASURL + uasURL;
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("data", result);

+ 5 - 2
src/main/java/com/uas/erp/schedular/finance/task/AccountApplyTask.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.uas.api.crypto.util.FlexJsonUtils;
 import com.uas.erp.schedular.finance.domain.*;
+import com.uas.erp.schedular.finance.mq.HttpsClientRequestFactory;
 import com.uas.erp.schedular.task.support.Method;
 import com.uas.erp.schedular.task.support.Role;
 import com.uas.erp.schedular.task.support.TaskMapping;
@@ -57,11 +58,13 @@ public class AccountApplyTask extends AbstractTask {
                 String signatureValue = OpenApiSignUtil.sign(JSON.toJSONString(json),String.valueOf(secretkey));
                 json.put("signature",signatureValue);
 
+                RestTemplate restTemp = new RestTemplate(new HttpsClientRequestFactory());
+
                 DefaultUriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
                 uriTemplateHandler.setStrictEncoding(true);
-                restTemplate.setUriTemplateHandler(uriTemplateHandler);
+                restTemp.setUriTemplateHandler(uriTemplateHandler);
 
-                String result = restTemplate.postForObject("http://192.168.253.3:26600/api/draw", json.toString(), String.class);
+                String result = restTemp.postForObject("https://finrest.usofchina.com/api/draw", json.toString(), String.class);
                 JSONObject response = (JSONObject) JSONObject.parse(result);
                 if("0".equals(String.valueOf(((JSONObject)response.get("respHeader")).getString("code")))){
                     jdbcTemplate.execute("update AccountApply set aa_sendstatus='已上传' where aa_code='"+ map.get("AA_CODE") +"'");