Compare commits

..

No commits in common. "atyou_prod" and "main" have entirely different histories.

View File

@ -1,5 +1,7 @@
package com.red.circle.auth.endpoint; package com.red.circle.auth.endpoint;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.red.circle.auth.common.ProcessToken; import com.red.circle.auth.common.ProcessToken;
import com.red.circle.auth.dto.TokenCredentialCO; import com.red.circle.auth.dto.TokenCredentialCO;
import com.red.circle.auth.response.AuthErrorCode; import com.red.circle.auth.response.AuthErrorCode;
@ -30,6 +32,12 @@ import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -60,6 +68,9 @@ public class EndpointRestController {
private final String LOGIN_WHITE_CONFIG = "LOGIN_WHITE_CONFIG"; private final String LOGIN_WHITE_CONFIG = "LOGIN_WHITE_CONFIG";
private final String APPCODE_CONFIG = "APPCODE 35493154e8304e47943ca596706da44f";
private final String REQUEST_URL = "https://c2ba.api.huachen.cn/ip";
private final RedisService redisService; private final RedisService redisService;
/** /**
@ -272,8 +283,44 @@ public class EndpointRestController {
} }
public String checkIpAddress(String ip) { public String checkIpAddress(String ip) {
log.info("IP:{} 临时跳过 IP 国家校验", ip); String redisKey = "IP_KEY:" + ip;
return null; String data = redisService.getString(redisKey);
JSONObject dataObject = null;
if (StringUtils.isNotBlank(data)) {
dataObject = JSONObject.parseObject(data);
} else {
//每一个ip地址只能有一次校验
HttpGet httpGet = new HttpGet(REQUEST_URL + "?ip=" + ip);
httpGet.addHeader("Authorization", APPCODE_CONFIG);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(10000).build();
try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse response = httpClient.execute(httpGet)) {
String result = EntityUtils.toString(response.getEntity());
log.info("请求地址:{},返回信息:{}", REQUEST_URL + "?ip=" + ip, result);
JSONObject jsonObject = JSON.parseObject(result);
if (jsonObject.getInteger("ret").equals(200)) {
dataObject = jsonObject.getJSONObject("data");
redisService.setString(redisKey, dataObject.toJSONString());
}
} catch (Exception e) {
log.error("请求地址:{}, 异常信息:{}", REQUEST_URL + "?ip=" + ip, e.getMessage());
}
}
log.info("IP:{}, 国家信息:{}", ip, dataObject);
// IP 归属接口超时或返回异常时不能让登录链路 NPE中港台拦截只在拿到明确国家信息时执行
if (dataObject == null) {
log.warn("IP:{} 国家信息获取失败,跳过 IP 国家校验", ip);
return null;
}
if (dataObject != null && "中国".equals(dataObject.getString("country"))) {
if (!Arrays.asList("香港", "澳门", "台湾").contains(dataObject.getString("region"))) {
ResponseAssert.isTrue(UserErrorCode.REGISTRATION_FAILED, false);
}
}
return dataObject.getString("country_id");
} }
public void checkZone(String reqZone) { public void checkZone(String reqZone) {