项目中涉及到一个关于调用华为接口实现破损预警的项目
项目是基于Springboot搭建的,主要controller有
禁用SSL
DisableSSLCertificateCheckUtil
package XXX.socket.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.*; import java.io.IOException; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public final class DisableSSLCertificateCheckUtil { private static final Logger LOGGER = LoggerFactory.getLogger(DisableSSLCertificateCheckUtil.class); private DisableSSLCertificateCheckUtil() { } public static void disableChecks() { try { new URL("https://192.168.2.3/").getContent(); } catch (IOException e) { // This invocation will always fail, but it will register the // default SSL provider to the URL class. } try { SSLContext sslc; sslc = SSLContext.getInstance("TLS"); TrustManager[] trustManagerArray = {new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}; sslc.init(null, trustManagerArray, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); } catch (Exception e) { LOGGER.error("error msg:{}", e); throw new IllegalArgumentException("证书校验异常!"); } } }
HttpsClientRequestFactory
package xxx.socket.controller; import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.conn.ssl.TrustStrategy; import org.springframework.http.client.SimpleClientHttpRequestFactory; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import java.net.HttpURLConnection; import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; /** * 兼容调Https接口 * @Author mazq * @Date 2020/06/04 17:16 * @Param * @return */ public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory { @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) { try { if (!(connection instanceof HttpsURLConnection)) {// http协议 //throw new RuntimeException("An instance of HttpsURLConnection is expected"); super.prepareConnection(connection, httpMethod); } if (connection instanceof HttpsURLConnection) {// https协议,修改协议版本 KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); // 信任任何链接 TrustStrategy anyTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }; SSLContext ctx = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build(); ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory()); HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; super.prepareConnection(httpsConnection, httpMethod); } } catch (Exception e) { e.printStackTrace(); } } }
RestTemplateUtils
package xxx.socket.controller; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.Map; /** * <pre> * RestTemplate 远程调用工具类 * </pre> * * <pre> * </pre> */ @Component public class RestTemplateUtils { public static RestTemplate geTemplate(){ return new RestTemplate(new HttpsClientRequestFactory()); } /** * GET请求调用方式 * @Author mazq * @Date 2020/06/01 13:47 * @Param [url, responseType, uriVariables] * @return org.springframework.http.ResponseEntity<T> */ public static <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) { return geTemplate().getForEntity(url, responseType, uriVariables); } /** * POST请求调用方式 * @Author mazq * @Date 2020/06/01 13:47 * @Param [url, headers, body, responseType] * @return org.springframework.http.ResponseEntity<T> */ public static <T> ResponseEntity<T> postForEntity(String url, HttpHeaders headers, Object requestBody , Class<T> responseType ){ HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return geTemplate().postForEntity(url, requestEntity, responseType); } /** * PUT请求调用方式 * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return geTemplate().exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables); } /** * DELETE请求调用方式 * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return geTemplate().exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables); } /** * 通用调用方式 * @Author mazq * @Date 2020/06/01 13:37 * @Param [url, method, requestEntity, responseType, uriVariables] * @return org.springframework.http.ResponseEntity<T> */ public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) { return geTemplate().exchange(url, method, requestEntity, responseType, uriVariables); } }
WebSocketController(controller)
package xxx.socket.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.net.URI; import java.net.URISyntaxException; @Controller @RequestMapping(value = "/socket") public class WebSocketController { private String prefix = "system/socket"; /** * historyalarmpubu */ @GetMapping("/historyalarmpubu") public String historyalarmpubu() { return prefix + "/historyalarmpubu"; } @RequestMapping(value = "/testPostApi") @ResponseBody public Object testPostApi() throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory()); //https ssl证书忽略 DisableSSLCertificateCheckUtil.disableChecks(); URI uri = new URI("https://192.168.888.999:26335/rest/plat/smapp/v1/sessions"); HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.add("Content-Type", "application/json;charset=UTF-8 "); //添加参数,因为HttpEntity里面的参数是MultiValueMap类型的,所以使用这个map集合 /*MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("grantType","password"); map.add("userName", "userName"); map.add("value", "passWord");*/ String s = "{\"grantType\":\"password\",\"userName\": \"userName\",\"value\": \"passWord\"}"; //添加请求的实体类,这里第一个参数是要发送的参数,第二个参数是请求头里的数据 HttpEntity<Object> requestEntity = new HttpEntity<>(s, headers); //跟下面使用交换机的方法结果一样 ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.PUT, requestEntity, String.class); JSONObject jsonObject = JSON.parseObject(exchange.getBody()); System.out.println(jsonObject); return jsonObject; } @RequestMapping(value = "/testPostApi2") @ResponseBody public Object testPostApi2(String token) throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory()); //https ssl证书忽略 DisableSSLCertificateCheckUtil.disableChecks(); /* URI uri1 = new URI("https://192.168.888.999:26335/restconf/v1/data/tim-rtn-alarms:alarms/alarm-list?alarm-type-qualifier[]=14593&alarm-type-qualifier[]=14594&alarm-type-qualifier[]=14586"); URI uri2 = new URI("https://192.168.888.999:26335/restconf/v1/data/ietf-alarms:alarms/alarm-list?limit=2000&start-time=2021-07-25T06:35:38Z&end-time=2021-7-30T14:35:38Z"); URI uri3 = new URI("https://192.168.888.999:26335/restconf/v1/data/ietf-alarms:alarms/alarm-list?limit=2000&is-cleared=false&start-time=2021-07-25T06:35:38Z&end-time=2021-07-27T14:35:38Z"); */ URI uri = new URI("https://192.168.888.999:26335/restconf/v1/data/ietf-alarms:alarms/alarm-list?alarm-type-qualifier=268374017-14593&alarm-type-qualifier=268374017-14594"); HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.add("Content-Type", "application/json;charset=UTF-8 "); headers.add("X-Auth-Token", token); //添加参数,因为HttpEntity里面的参数是MultiValueMap类型的,所以使用这个map集合 /*MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("grantType","password"); map.add("userName", "superuser"); map.add("value", "Kcm_123456");*/ String s1 = "{\"is-cleared\":false}"; String s2 = "{\"is-cleared\":true,\"alarm-type-qualifier\": [\"14593\",\"14594\",\"14586\"]}"; String s = ""; //添加请求的实体类,这里第一个参数是要发送的参数,第二个参数是请求头里的数据 HttpEntity<Object> requestEntity = new HttpEntity<>(s, headers); //跟下面使用交换机的方法结果一样 System.out.println(uri); ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class); JSONObject jsonObject = JSON.parseObject(exchange.getBody()); System.out.println(jsonObject); return jsonObject; } @RequestMapping(value = "/testPostApi3") @ResponseBody public Object testPostApi3(String token) throws URISyntaxException { RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory()); //https ssl证书忽略 DisableSSLCertificateCheckUtil.disableChecks(); URI uri = new URI("https://192.168.888.999:26335/restconf/v1/data/ietf-alarms:alarms/alarm-list?alarm-type-qualifier=268374017-14586"); HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.add("Content-Type", "application/json;charset=UTF-8 "); headers.add("X-Auth-Token", token); String s = ""; //添加请求的实体类,这里第一个参数是要发送的参数,第二个参数是请求头里的数据 HttpEntity<Object> requestEntity = new HttpEntity<>(s, headers); //跟下面使用交换机的方法结果一样 System.out.println(uri); ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class); JSONObject jsonObject = JSON.parseObject(exchange.getBody()); System.out.println(jsonObject); return jsonObject; } }
如遇到问题请联系博主第一时间修正删除