Commit 3e493ed7 by xieshaohua

麦当劳机构用户信息同步接口改为https

parent 06888308
......@@ -133,8 +133,7 @@ public class LoginManagement {
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
log.info("单点登录用户:" + userName);
MDC.put("user", userName);
MDC.put("user", userName + "(" + userDName + ")");
MDC.put("session", session.getId());
LogManager.logInfo(Constants.SSO_API, "登录");
response.sendRedirect(ssoRedirectUrl);
......
......@@ -3,15 +3,12 @@ package com.keymobile.login.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keymobile.login.persistence.SsoOrganMcclRepository;
import com.keymobile.login.persistence.SsoOrganRepository;
import com.keymobile.login.persistence.SsoUserMcclRepository;
import com.keymobile.login.persistence.SsoUserRepository;
import com.keymobile.login.persistence.*;
import com.keymobile.login.persistence.model.SsoOrganAbstract;
import com.keymobile.login.persistence.model.SsoOrganMcclAbstract;
import com.keymobile.login.persistence.model.SsoUserAbstract;
import com.keymobile.login.persistence.model.SsoUserMcclAbstract;
import com.keymobile.login.util.HttpUtil;
import com.keymobile.login.util.HttpsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
......@@ -27,14 +24,10 @@ import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.transaction.Transactional;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.TimeUnit;
......@@ -82,6 +75,7 @@ public class PeopleCenterApi {
private SsoOrganRepository ssoOrganRepository;
@Autowired
private SsoOrganMcclRepository ssoOrganMcclRepository;
@Autowired
private RedisTemplate<String, String> redisTemplate;
......@@ -256,6 +250,11 @@ public class PeopleCenterApi {
}
@RequestMapping(value = "/refreshSyncKey", method = {RequestMethod.POST, RequestMethod.GET})
public void refreshSyncKey(){
redisTemplate.delete("pcToken");
}
@RequestMapping(value = "/userSync", method = {RequestMethod.POST, RequestMethod.GET})
@Async
public void userSync(HttpServletRequest request, @RequestParam(required = false) Integer startPage,
......@@ -265,7 +264,7 @@ public class PeopleCenterApi {
String token = redisTemplate.opsForValue().get("pcToken");
if (StringUtils.isBlank(token)) {
token = getToken();
redisTemplate.opsForValue().set("pcToken", token, 60 * 60, TimeUnit.SECONDS);
redisTemplate.opsForValue().set("pcToken", token, 60, TimeUnit.SECONDS);
}
if (StringUtils.isBlank(token)) {
log.info("token获取失败");
......@@ -278,6 +277,7 @@ public class PeopleCenterApi {
header.put(HEADER_AUTHORIZATION, token);
header.put("SysId", sysId);
if (startPage == null && isDelete) {
log.info("删除旧数据");
ssoUserRepository.deleteAll();
}
......@@ -288,7 +288,7 @@ public class PeopleCenterApi {
if (searchAfters != null) {
body.put("searchAfters", searchAfters);
}
String result = HttpUtil.doPost(userUrl, header, JSON.toJSONString(body));
String result = HttpsUtil.doPost(userUrl, header, JSON.toJSONString(body));
JSONObject resultJson = JSON.parseObject(result);
if (StringUtils.equalsIgnoreCase(resultJson.getString("code"), "SUCCESS")) {
JSONObject dataJson = resultJson.getJSONObject("data");
......@@ -331,7 +331,7 @@ public class PeopleCenterApi {
String token = redisTemplate.opsForValue().get("pcToken");
if (StringUtils.isBlank(token)) {
token = getToken();
redisTemplate.opsForValue().set("pcToken", token, 60 * 60, TimeUnit.SECONDS);
redisTemplate.opsForValue().set("pcToken", token, 60, TimeUnit.SECONDS);
}
if (StringUtils.isBlank(token)) {
log.info("token获取失败");
......@@ -344,6 +344,7 @@ public class PeopleCenterApi {
header.put(HEADER_AUTHORIZATION, token);
header.put("SysId", sysId);
if (startPage == null && isDelete) {
log.info("删除旧数据");
ssoOrganRepository.deleteAll();
}
......@@ -354,7 +355,7 @@ public class PeopleCenterApi {
if (searchAfters != null) {
body.put("searchAfters", searchAfters);
}
String result = HttpUtil.doPost(organUrl, header, JSON.toJSONString(body));
String result = HttpsUtil.doPost(organUrl, header, JSON.toJSONString(body));
JSONObject resultJson = JSON.parseObject(result);
if (StringUtils.equalsIgnoreCase(resultJson.getString("code"), "SUCCESS")) {
JSONObject dataJson = resultJson.getJSONObject("data");
......@@ -387,7 +388,6 @@ public class PeopleCenterApi {
}
log.info("完成机构同步");
}
......@@ -397,7 +397,7 @@ public class PeopleCenterApi {
body.put("sysId", sysId);
JSON.toJSONString(body);
String result = HttpUtil.doPost(tokenUrl, null, JSON.toJSONString(body));
String result = HttpsUtil.doPost(tokenUrl, null, JSON.toJSONString(body));
JSONObject jsonObject = JSON.parseObject(result);
if (StringUtils.equalsIgnoreCase(jsonObject.getString("code"), "success")) {
return jsonObject.getJSONObject("data").getString("access_token");
......
package com.keymobile.login.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
/**
* @author xiesh
* @version 1.0.0
* @date 2024/4/26
* @desc https工具类
*/
public class HttpsUtil {
public static String doGet(String url, Map<String, String> headerMap) {
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String res = "";
try {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
client = HttpClients.custom().setSSLContext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpGet request = new HttpGet(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach((k, v) -> request.setHeader(k, v));
}
request.setHeader("Content-Type", "application/json");
response = client.execute(request);
res = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
public static String doPost(String url, Map<String, String> headerMap, String bodyJsonStr){
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
String res = "";
try {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
client = HttpClients.custom().setSSLContext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpPost request = new HttpPost(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach((k, v) -> request.setHeader(k, v));
}
request.setHeader("Content-Type", "application/json");
if (StringUtils.isNotBlank(bodyJsonStr)) {
StringEntity entity = new StringEntity(bodyJsonStr, "UTF-8");
request.setEntity(entity);
}
response = client.execute(request);
res = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment