1、联调接口https会报证书不认可,故设置oOkHttpClient不校验ssl

This commit is contained in:
PushM
2024-05-13 14:46:46 +08:00
parent 60da2b4ce5
commit 8c116c5354
3 changed files with 83 additions and 12 deletions

View File

@@ -6,8 +6,8 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class KafkaTopicConfig {
@Bean
public NewTopic batchTopic() {
return new NewTopic("topic-alert", 4, (short) 1);
}
// @Bean
// public NewTopic batchTopic() {
// return new NewTopic("topic-alert", 4, (short) 1);
// }
}

View File

@@ -0,0 +1,59 @@
package com.realtime.protection.configuration.utils;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
/**
*
* @author Jeter
*
*/
public class OkHttpUtil {
/**
* X509TrustManager instance which ignored SSL certification
*/
public static final X509TrustManager IGNORE_SSL_TRUST_MANAGER_X509 = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
};
/**
* Get initialized SSLContext instance which ignored SSL certification
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static SSLContext getIgnoreInitedSslContext() throws NoSuchAlgorithmException, KeyManagementException {
var sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { IGNORE_SSL_TRUST_MANAGER_X509 }, new SecureRandom());
return sslContext;
}
/**
* Get HostnameVerifier which ignored SSL certification
*
* @return
*/
public static HostnameVerifier getIgnoreSslHostnameVerifier() {
return new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
}
}

View File

@@ -1,10 +1,10 @@
package com.realtime.protection.server.user.login;
import com.realtime.protection.configuration.utils.OkHttpUtil;
import org.apache.logging.log4j.util.Strings;
import javax.security.auth.login.LoginException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -13,7 +13,6 @@ import com.realtime.protection.configuration.entity.user.User;
import com.realtime.protection.configuration.entity.user.UserFull;
import cn.dev33.satoken.stp.StpUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -22,6 +21,9 @@ import okhttp3.Response;
import io.micrometer.common.util.StringUtils;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
@Service
// just for example, not for production environment
public class LoginService {
@@ -45,13 +47,20 @@ public class LoginService {
return userId;
}
public UserFull loginWithSSO(String sessionData) throws LoginException {
public UserFull loginWithSSO(String sessionData) throws LoginException, NoSuchAlgorithmException, KeyManagementException {
String accessToken = "";
// 获取 ACCESS_TOKEN
ObjectMapper objectMapper = new ObjectMapper();
OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(OkHttpUtil.getIgnoreInitedSslContext().getSocketFactory(),OkHttpUtil.IGNORE_SSL_TRUST_MANAGER_X509)
.hostnameVerifier(OkHttpUtil.getIgnoreSslHostnameVerifier())
.build();
Request request = new Request.Builder()
.url("http://114.243.134.122:10217/passport/accessToken?grant_type=client_credentials")
// .url("https://passport.zx.com:10217/passport/accessToken?grant_type=client_credentials")
.url("https://114.243.134.122:10217/passport/accessToken?grant_type=client_credentials")
// .url("https://passport.iam.pub/passport/accessToken?grant_type=client_credentials")
.header("Authorization", "Basic TlNBRERAWlguT1JHOk14a1hHZ1ltOUNROUE3TCRSOCNLRW02R1pSeEhwd1c2")
.post(okhttp3.internal.Util.EMPTY_REQUEST)
@@ -76,9 +85,9 @@ public class LoginService {
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("sessionData", sessionData).build();
request = new Request.Builder()
.url("http://114.243.134.122:10217/passport/accessApplication")
// .url("https://passport.iam.pub/passport/accessToken?grant_type=client_credentials")
// .url("https://passport.zx.com:10217/passport/accessApplication")
.url("https://114.243.134.122:10217/passport/accessApplication")
// .url("https://passport.iam.pub/passport/accessApplication")
.header("Authorization", "Bearer " + accessToken)
.header("Content-Type", "application/x-www-form-urlencoded")
.post(body)
@@ -102,4 +111,7 @@ public class LoginService {
throw new LoginException("校验 SESSION_DATA 失败");
}
}
}