20238 月10
Nutz Https请求忽略SSL证书
@IocBean
@Slf4j
public class CimApiServer {
@Inject
private RedisService redisService;
private String redis_key = "cim:accessToken";
@Inject
@Reference(check = false)
private ISysConfigProvider sysConfigProvider;
public String getAccessToken() {
String token = redisService.get(redis_key);
if (Strings.isBlank(token)) {
token = this.getHttpToken();
redisService.setex(redis_key, 3600 * 24 - 100, token);
}
return token;
}
private String getHttpToken() {
String CIM_GIS_APPID = sysConfigProvider.getString("COMMON", "CIM_GIS_APPID");
String CIM_GIS_HTTP_BASE = sysConfigProvider.getString("COMMON", "CIM_GIS_HTTP_BASE");
String CIM_GIS_APPKEY = sysConfigProvider.getString("COMMON", "CIM_GIS_APPKEY");
String CIM_GIS_APPSECRET = sysConfigProvider.getString("COMMON", "CIM_GIS_APPSECRET");
Map<String, Object> params = new HashMap<>();
params.put("apiKey", CIM_GIS_APPKEY);
params.put("secret", CIM_GIS_APPSECRET);
Header header = Header.create();
header.addv("Content-Type", "application/json");
Request request = Request.create(CIM_GIS_HTTP_BASE + "/auth/getAccessToken", Request.METHOD.POST);
request.setHeader(header);
request.setData(Json.toJson(params));
Sender sender = Sender.create(request).setTimeout(20 * 1000);
if (CIM_GIS_HTTP_BASE.startsWith("https")) {
try {
SSLContext sslcontext = createIgnoreVerifySSL();
sender.setSSLSocketFactory(sslcontext.getSocketFactory());
sender.setHostnameVerifier((urlHostName, session) -> true);
} catch (Exception e) {
e.printStackTrace();
}
}
Response response = sender.send();
if (response.isOK()) {
NutMap map = Json.fromJson(NutMap.class, response.getContent());
log.debug("getHttpToken:::" + Json.toJson(map));
if (0 == map.getInt("code")) {
return map.getString("data");
}
}
return "";
}
private static class TrustAllManager
implements X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
}
}
public SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAllManager()}, null);
return sc;
}
}
本文地址:https://wizzer.cn/archives/3757 , 转载请保留.