You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
343 lines
13 KiB
343 lines
13 KiB
3 years ago
|
package com.fr.plugin.third.party.jsdbabbe;
|
||
|
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.webservice.bean.user.UserBean;
|
||
|
import com.fr.decision.webservice.bean.user.UserUpdateBean;
|
||
|
import com.fr.decision.webservice.impl.user.UserPageQueryAuthorityParam;
|
||
|
import com.fr.decision.webservice.utils.ControllerFactory;
|
||
|
import com.fr.decision.webservice.utils.controller.UserController;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.plugin.third.party.jsdbabbe.config.CustomDataConfig;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.query.data.DataList;
|
||
|
import com.fr.stable.query.restriction.Restriction;
|
||
|
import com.fr.third.org.apache.http.HttpEntity;
|
||
|
import com.fr.third.org.apache.http.HttpStatus;
|
||
|
import com.fr.third.org.apache.http.client.config.RequestConfig;
|
||
|
import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse;
|
||
|
import com.fr.third.org.apache.http.client.methods.HttpGet;
|
||
|
import com.fr.third.org.apache.http.client.methods.HttpPost;
|
||
|
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||
|
import com.fr.third.org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||
|
import com.fr.third.org.apache.http.entity.StringEntity;
|
||
|
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
|
||
|
import com.fr.third.org.apache.http.impl.client.HttpClients;
|
||
|
import com.fr.third.org.apache.http.ssl.SSLContextBuilder;
|
||
|
import com.fr.third.org.apache.http.ssl.TrustStrategy;
|
||
|
import com.fr.third.org.apache.http.util.EntityUtils;
|
||
|
|
||
|
import javax.net.ssl.HostnameVerifier;
|
||
|
import javax.net.ssl.SSLContext;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.io.IOException;
|
||
|
import java.security.cert.CertificateException;
|
||
|
import java.security.cert.X509Certificate;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.UUID;
|
||
|
|
||
|
public class Utils {
|
||
|
public static String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36";
|
||
|
public static RequestConfig REQUEST_CONFIG = RequestConfig.custom()
|
||
|
.setConnectionRequestTimeout(30000)
|
||
|
.setSocketTimeout(30000) // 服务端相应超时
|
||
|
.setConnectTimeout(30000) // 建立socket链接超时时间
|
||
|
.build();
|
||
|
|
||
|
public static CloseableHttpClient createSslHttpClient() {
|
||
|
try {
|
||
|
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
||
|
|
||
|
@Override
|
||
|
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||
|
return true;
|
||
|
}
|
||
|
}).build();
|
||
|
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
|
||
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
|
||
|
return HttpClients.custom()
|
||
|
.setSSLSocketFactory(sslsf)
|
||
|
.build();
|
||
|
} catch (Exception e) {
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
return HttpClients.createDefault();
|
||
|
}
|
||
|
|
||
|
public static CloseableHttpClient createDefaultHttpClient() {
|
||
|
CloseableHttpClient httpClient = HttpClients.custom()
|
||
|
.build();
|
||
|
return httpClient;
|
||
|
}
|
||
|
|
||
|
public static synchronized CloseableHttpClient createHttpClient(String url) {
|
||
|
CloseableHttpClient httpClient = null;
|
||
|
if (StringKit.isEmpty(url)) {
|
||
|
httpClient = createDefaultHttpClient();
|
||
|
return httpClient;
|
||
|
}
|
||
|
|
||
|
if (url.startsWith("https://")) {
|
||
|
httpClient = createSslHttpClient();
|
||
|
return httpClient;
|
||
|
}
|
||
|
httpClient = createDefaultHttpClient();
|
||
|
return httpClient;
|
||
|
}
|
||
|
|
||
|
public static synchronized String createHttpGetContent(CloseableHttpClient httpClient, String url, String basicAuth) throws IOException {
|
||
|
if ((httpClient == null) || (StringKit.isEmpty(url))) {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
HttpGet httpGet = new HttpGet(url);
|
||
|
httpGet.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT);
|
||
|
if (StringKit.isNotEmpty(basicAuth)) {
|
||
|
httpGet.addHeader("Authorization", basicAuth);
|
||
|
}
|
||
|
|
||
|
httpGet.setConfig(Utils.REQUEST_CONFIG);
|
||
|
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||
|
if (statusCode != HttpStatus.SC_OK) {
|
||
|
response.close();
|
||
|
LogKit.info("http请求出错,http status:" + statusCode);
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
HttpEntity httpEntity = response.getEntity();
|
||
|
if (httpEntity == null) {
|
||
|
response.close();
|
||
|
LogKit.info("http请求出错,http响应内容为空");
|
||
|
return "";
|
||
|
}
|
||
|
String responseContent = EntityUtils.toString(httpEntity, "UTF-8");
|
||
|
response.close();
|
||
|
if (StringKit.isEmpty(responseContent)) {
|
||
|
LogKit.info("http请求出错,http响应内容为空1");
|
||
|
return "";
|
||
|
}
|
||
|
return responseContent;
|
||
|
}
|
||
|
|
||
|
public static synchronized String createHttpPostContent(CloseableHttpClient httpClient, String url, String bodyContent, String basicAuth, String contentType, Map<String, String> headers) throws IOException {
|
||
|
if ((httpClient == null) || (StringKit.isEmpty(url))) {
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
HttpPost httpPost = new HttpPost(url);
|
||
|
httpPost.addHeader("User-Agent", Utils.DEFAULT_USER_AGENT);
|
||
|
httpPost.setConfig(Utils.REQUEST_CONFIG);
|
||
|
if (StringKit.isNotEmpty(basicAuth)) {
|
||
|
httpPost.addHeader("Authorization", basicAuth);
|
||
|
}
|
||
|
if ((headers != null) && (headers.size() >= 1)) {
|
||
|
for (Map.Entry<String,String> entry: headers.entrySet()) {
|
||
|
httpPost.addHeader(entry.getKey(), entry.getValue());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (StringKit.isNotEmpty(contentType)) {
|
||
|
httpPost.addHeader("Content-Type", contentType);
|
||
|
}
|
||
|
StringEntity bodyEntity = new StringEntity(bodyContent, "UTF-8");
|
||
|
httpPost.setEntity(bodyEntity);
|
||
|
CloseableHttpResponse response = httpClient.execute(httpPost);
|
||
|
int statusCode = response.getStatusLine().getStatusCode();
|
||
|
if (statusCode != HttpStatus.SC_OK) {
|
||
|
response.close();
|
||
|
LogKit.info("http请求出错,http status:" + statusCode);
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
HttpEntity httpEntity = response.getEntity();
|
||
|
if (httpEntity == null) {
|
||
|
response.close();
|
||
|
LogKit.info("http请求出错,http响应内容为空");
|
||
|
return "";
|
||
|
}
|
||
|
String responseContent = EntityUtils.toString(httpEntity, "UTF-8");
|
||
|
response.close();
|
||
|
if (StringKit.isEmpty(responseContent)) {
|
||
|
LogKit.info("http请求出错,http响应内容为空1");
|
||
|
return "";
|
||
|
}
|
||
|
return responseContent;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取完整请求链接
|
||
|
*
|
||
|
* @param req 请求
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getFullRequestUrl(HttpServletRequest req) {
|
||
|
if (req == null) {
|
||
|
return "";
|
||
|
}
|
||
|
String url = req.getRequestURL().toString();
|
||
|
String queryUrl = req.getQueryString();
|
||
|
if ((queryUrl == null) || "null".equalsIgnoreCase(queryUrl)) {
|
||
|
queryUrl = "";
|
||
|
} else {
|
||
|
queryUrl = "?" + queryUrl;
|
||
|
}
|
||
|
String fullUrl = url + queryUrl;
|
||
|
return fullUrl;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 重定向
|
||
|
*
|
||
|
* @param res
|
||
|
* @param url
|
||
|
*/
|
||
|
public static void sendRedirect(HttpServletResponse res, String url) {
|
||
|
if ((res == null) || (StringKit.isEmpty(url))) {
|
||
|
return;
|
||
|
}
|
||
|
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
|
||
|
res.setHeader("Location", url);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static synchronized String getUuid() {
|
||
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
||
|
return uuid;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取用户信息
|
||
|
*
|
||
|
* @param userCount 用户数,-1为不限制用户数
|
||
|
* @param isAdminContained 是否包含管理员用户
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static List<UserBean> getAllUsers(int userCount, boolean isAdminContained) throws Exception {
|
||
|
List<UserBean> userBeans = new ArrayList<>();
|
||
|
if (userCount == 0) {
|
||
|
return userBeans;
|
||
|
}
|
||
|
int limitCount = userCount;
|
||
|
if (userCount <= -1) {
|
||
|
limitCount = Integer.MAX_VALUE;
|
||
|
}
|
||
|
List<String> adminUserIds = UserService.getInstance().getAdminUserIdList();
|
||
|
if ((adminUserIds == null) || (adminUserIds.size() <= 0)) {
|
||
|
return userBeans;
|
||
|
}
|
||
|
String adminUserId = adminUserIds.get(0);
|
||
|
|
||
|
UserController userController = ControllerFactory.getInstance().getUserController(adminUserId);
|
||
|
|
||
|
DataList dataList = userController.findPageUsersPerfectMatch(adminUserId, new UserPageQueryAuthorityParam(1, limitCount, "", null), new Restriction[0]);
|
||
|
if (dataList == null) {
|
||
|
return userBeans;
|
||
|
}
|
||
|
List users = dataList.getList();
|
||
|
if ((users == null) || (users.size() <= 0)) {
|
||
|
return userBeans;
|
||
|
}
|
||
|
User user;
|
||
|
UserBean userBean;
|
||
|
String userId;
|
||
|
for (int i = 0, max = users.size() - 1; i <= max; i++) {
|
||
|
user = (User) users.get(i);
|
||
|
userId = user.getId();
|
||
|
if ((!isAdminContained) && (adminUserIds.contains(userId))) {
|
||
|
continue;
|
||
|
}
|
||
|
userBean = new UserBean(user.getEmail(), user.isEnable(), user.getMobile(), user.getRealName(), user.getUserName(), user.getId());
|
||
|
userBean.setCreationType(user.getCreationType().toInteger());
|
||
|
userBeans.add(userBean);
|
||
|
}
|
||
|
return userBeans;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取不包含管理员的用户信息
|
||
|
*
|
||
|
* @param userCount
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static List<UserBean> getAllUsersWithoutAdmin(int userCount) throws Exception {
|
||
|
return getAllUsers(userCount, false);
|
||
|
}
|
||
|
|
||
|
public static List<String> getAllUserNames(int userCount, boolean isAdminContained) throws Exception {
|
||
|
List<String> userNames = new ArrayList<>();
|
||
|
List<UserBean> userBeans = getAllUsers(userCount, isAdminContained);
|
||
|
UserBean userBean;
|
||
|
for (int i = 0, max = userBeans.size() - 1; i <= max; i++) {
|
||
|
userBean = userBeans.get(i);
|
||
|
userNames.add(userBean.getUsername());
|
||
|
}
|
||
|
return userNames;
|
||
|
}
|
||
|
|
||
|
public static List<String> getAllUserNamesWithoutAdmin(int userCount) throws Exception {
|
||
|
return getAllUserNames(userCount, false);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 通过用户名删除用户,管理员用户无法删除
|
||
|
*
|
||
|
* @param username 用户名
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static int deleteUsersByUsername(String username) throws Exception {
|
||
|
if (StringUtils.isEmpty(username)) {
|
||
|
return 0;
|
||
|
}
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user == null) {
|
||
|
return 0;
|
||
|
}
|
||
|
String userId = user.getId();
|
||
|
List<String> adminUserIds = UserService.getInstance().getAdminUserIdList();
|
||
|
if ((adminUserIds != null) && (adminUserIds.size() >= 1) && (adminUserIds.contains(userId))) {
|
||
|
return 0;
|
||
|
}
|
||
|
UserUpdateBean userUpdateBean = new UserUpdateBean();
|
||
|
userUpdateBean.setRemoveUserIds(new String[]{userId});
|
||
|
return UserService.getInstance().deleteUsers(userUpdateBean);
|
||
|
}
|
||
|
|
||
|
public static int deleteUsersByUsernames(List<String> usernames) throws Exception {
|
||
|
if ((usernames == null) || (usernames.size() <= 0)) {
|
||
|
return 0;
|
||
|
}
|
||
|
String username;
|
||
|
int totalCount = 0, count;
|
||
|
for (int i = 0, max = usernames.size() - 1; i <= max; i++) {
|
||
|
username = usernames.get(i);
|
||
|
count = deleteUsersByUsername(username);
|
||
|
totalCount = totalCount + count;
|
||
|
}
|
||
|
return totalCount;
|
||
|
}
|
||
|
|
||
|
public static void editRealNameByUsername(String username, String realName) throws Exception {
|
||
|
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(realName)) {
|
||
|
return;
|
||
|
}
|
||
|
User user = UserService.getInstance().getUserByUserName(username);
|
||
|
if (user == null) {
|
||
|
return;
|
||
|
}
|
||
|
UserBean userBean = new UserBean(user.getEmail(), user.isEnable(), user.getMobile(), realName, user.getUserName(), user.getId());
|
||
|
UserService.getInstance().editAccount(username, userBean);
|
||
|
}
|
||
|
}
|