From 798fbdc154bb9254a7dd0ba36f4afe941910eb59 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 07 十月 2023 09:22:07 +0800 Subject: [PATCH] 解决https访问失败 --- src/main/java/com/smartearth/poiexcel/service/EntService.java | 74 ++++++++++++++++++++++++++++++++---- src/main/resources/application.properties | 4 + 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/smartearth/poiexcel/service/EntService.java b/src/main/java/com/smartearth/poiexcel/service/EntService.java index ab9f5c3..abdb64b 100644 --- a/src/main/java/com/smartearth/poiexcel/service/EntService.java +++ b/src/main/java/com/smartearth/poiexcel/service/EntService.java @@ -8,12 +8,23 @@ import com.smartearth.poiexcel.utils.RestHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContexts; +import org.apache.http.ssl.TrustStrategy; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; +import javax.net.ssl.SSLContext; import java.lang.reflect.Field; import java.util.HashMap; import java.util.List; @@ -25,6 +36,7 @@ * @date 2023-10-05 */ @Service +@SuppressWarnings("ALL") public class EntService { @Resource EntMapper entMapper; @@ -38,23 +50,38 @@ @Value("${qylweb.pwd}") String pwd; + private static RestTemplate restTemplate; + private final static Log log = LogFactory.getLog(EntService.class); - private final static String generateToken = "%s/yqfwg/app/generateToken"; + private final static String GENERATE_TOKEN = "%s/yqfwg/app/generateToken"; - private final static String getEntBaseInfo = "%s/yqfwg/api/project/getEntBaseInfoForOtherSysListPage?ctoken=%s&showCount=%d¤tPage=%d"; + private final static String GET_ENT_BASE_INFO = "%s/yqfwg/api/project/getEntBaseInfoForOtherSysListPage?ctoken=%s&showCount=%d¤tPage=%d"; /** * 鑾峰彇浠ょ墝 */ public String selectToken() { try { - String url = String.format(generateToken, host); + String url = String.format(GENERATE_TOKEN, host); - Map<String, String> map = new HashMap<>(2); - map.put("userName", user); - map.put("passWord", pwd); + //Map<String, String> map = new HashMap<>(2); + //map.put("userName", user); + //map.put("passWord", pwd); + //String map = "userName=shikong001&passWord=123!@#qwe"; + + // 璇锋眰澶翠俊鎭� + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.valueOf("application/x-www-form-urlencoded")); + + //璁剧疆涓鸿〃鍗曟彁浜わ紝鎸夐渶姹傚姞 + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + // 缁勮璇锋眰淇℃伅 + MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); + map.add("userName", user); + map.add("passWord", pwd); + HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers); // String str = RestHelper.postForRest(url, map) Result rs = postForRest(url, map, Result.class); @@ -77,7 +104,7 @@ */ public List<EntEntity> selectEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) { try { - String url = String.format(getEntBaseInfo, host, token, showCount, currentPage); + String url = String.format(GET_ENT_BASE_INFO, host, token, showCount, currentPage); if (null != startDate) { url += "&buildDate_startdate=" + startDate; } @@ -129,7 +156,8 @@ * get璇锋眰锛圧est锛� */ public <T> T getForRest(String url, Class<T> clazz) { - RestTemplate rest = RestHelper.getRestTemplate(); + // RestTemplate rest = RestHelper.getRestTemplate() + RestTemplate rest = getRestTemplate(); return rest.getForObject(url, clazz); } @@ -139,6 +167,7 @@ */ public <T> T postForRest(String url, Object map, Class<T> clazz) { RestTemplate rest = RestHelper.getRestTemplate(); + // RestTemplate rest = getRestTemplate(); return rest.postForObject(url, map, clazz); } @@ -152,7 +181,8 @@ HttpEntity<?> entity = new HttpEntity<>(map, headers); - RestTemplate rest = RestHelper.getRestTemplate(); + // RestTemplate rest = RestHelper.getRestTemplate() + RestTemplate rest = getRestTemplate(); ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz); return rs.getBody(); @@ -182,4 +212,30 @@ return map; } + + /** + * 鑾峰彇RestTemplate + */ + public static RestTemplate getRestTemplate() { + if (null == restTemplate) { + try { + TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; + SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); + SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); + + HttpClientBuilder clientBuilder = HttpClients.custom(); + + CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslFactory).build(); + + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + requestFactory.setHttpClient(httpClient); + + restTemplate = new RestTemplate(requestFactory); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + return restTemplate; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 227cea2..04d418f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,8 +9,10 @@ # 数据源配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smart_earth?allowMultiQueries=true&useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.username=root +#spring.datasource.url=jdbc:mysql://127.0.0.1:3366/smart_earth?allowMultiQueries=true&useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8 +#spring.datasource.password=Admin@123 +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smart_earth?allowMultiQueries=true&useUnicode=true&rewriteBatchedStatements=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.password=mysql # mybatis -- Gitblit v1.9.3