1. 登录模块新增软key登录模块代码

develop
admin 2024-10-31 11:49:49 +08:00
parent f611a65475
commit 678c1e775d
5 changed files with 163 additions and 2 deletions

View File

@ -32,6 +32,20 @@ public class TrxController {
return generalResult;
}
/**
* token
*
* @param ngxCookie
* @return randomstr
*/
@GetMapping("/soft/randomstr")
@Operation(summary = "获取SoftRandomstr")
public GeneralResult<String> getSoftRandomstr(@RequestParam(value = "ngxCookie") String ngxCookie,
@RequestParam(value = "account") String account) {
GeneralResult generalResult = trxService.getSoftRandomstr(ngxCookie, account);
return generalResult;
}
@PostMapping("/login")
@Operation(summary = "用户登录")
public GeneralResult<String> getAuthToken(@RequestBody TrxAuthModel trxAuthModel,
@ -42,6 +56,16 @@ public class TrxController {
return generalResult;
}
@PostMapping("/softlogin")
@Operation(summary = "用户登录")
public GeneralResult<String> getSoftAuthToken(@RequestBody TrxAuthModel trxAuthModel,
HttpServletRequest request) {
String ipAddress = trxService.getRequestIpAddress(request);
trxAuthModel.setClientIp(ipAddress);
GeneralResult generalResult = trxService.getpwdAuthen(trxAuthModel);
return generalResult;
}
@PostMapping("/logout")
@Operation(summary = "天融信用户登出")
public GeneralResult<String> logout(@RequestBody TrxAuthModel trxAuthModel,

View File

@ -7,8 +7,12 @@ import jakarta.servlet.http.HttpServletRequest;
public interface TrxService {
GeneralResult getRandomstr(String ngxCookie);
GeneralResult getSoftRandomstr(String ngxCookie, String account);
GeneralResult getAuthToken(TrxAuthModel trxAuthModel);
GeneralResult getpwdAuthen(TrxAuthModel trxAuthModel);
GeneralResult logout(TrxAuthModel trxAuthModel);
String getRequestIpAddress(HttpServletRequest request);

View File

@ -16,6 +16,7 @@ import com.bocloud.sms.model.*;
import com.bocloud.sms.repository.RoleRepository;
import com.bocloud.sms.repository.UserRepository;
import com.bocloud.sms.service.utils.QxUtils;
import com.bocloud.sms.service.utils.Sha256Util;
import com.bocloud.sms.service.utils.YkUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@ -31,6 +32,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;
@ -77,6 +79,31 @@ public class TrxServiceImpl implements TrxService {
return new GeneralResult(true, result.getString("result"), "获取随机数成功");
}
@Override
public GeneralResult getSoftRandomstr(String ngxCookie, String account) {
String url = trxUrl + "/getRandomStr";
Map<String, String> paramsMap = new HashMap<>();
paramsMap.put("isToken", "false");
paramsMap.put("ngx_cookie", ngxCookie);
JSONObject result;
try {
HttpRequest httpRequest = HttpRequest.get(url)
.addHeaders(paramsMap);
httpRequest.cookie("ngx_cookie=" + ngxCookie + ";account=" + account);
log.info("调用天融信获取软随机字符串接口, url:" + httpRequest.getUrl() + "请求头:" + JSONObject.toJSONString(httpRequest.headers()));
log.info("调用天融信获取随机字符串接口, 请求信息:" + JSONObject.toJSONString(httpRequest));
HttpResponse response = httpRequest.execute();
result = JSONObject.parseObject(response.body());
if ("-1".equals(result.getString("result"))) {
return new GeneralResult(false, "获取随机数失败" + result.getString("errmsg"));
}
} catch (Exception e) {
log.error("调用天融信获取随机字符串失败", e);
return new GeneralResult(false, "获取随机数失败" + e.getMessage());
}
return new GeneralResult(true, result.getString("result"), "获取随机数成功");
}
@Override
public GeneralResult getAuthToken(TrxAuthModel trxAuthModel) {
@ -143,6 +170,50 @@ public class TrxServiceImpl implements TrxService {
return login;
}
@Override
public GeneralResult getpwdAuthen(TrxAuthModel trxAuthModel) {
String url = trxUrl + "/pwdAuthen";
String s1 = Sha256Util.sha256(trxAuthModel.getPasswd());
String rs1 = Sha256Util.sha256(trxAuthModel.getRandoms() + s1);
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("cookie", trxAuthModel.getClientHello());
paramsMap.put("confusn_pwd", rs1);
paramsMap.put("client_ip", trxAuthModel.getClientIp());
JSONObject result = new JSONObject();
GeneralResult<Map<String, Object>> login = new GeneralResult<>();
try {
HttpRequest httpRequest = HttpRequest.post(url)
.header("isToken", "false")
.body(JSONObject.toJSONString(paramsMap));
log.info("调用天融信用户软key登录接口, 请求信息:" + JSONObject.toJSONString(httpRequest));
HttpResponse response = httpRequest.execute();
result = JSONObject.parseObject(response.body());
if (!"0".equals(result.getString("result"))) {
return new GeneralResult(false, "用户登录天融信失败" + result.getString("errmsg"));
}
String trxToken = result.getString("token");
String userId = result.getString("user_id");
// 处理ukey登录用户
log.info("开始软Key登录用户入库, userId:" + userId);
saveUserByTrx(userId);
log.info("软Key登录用户入库结束");
// 登录
log.info("开始软Key登录用户登录云管平台");
User user = userRepository.getByUserId(userId);
Encryptor encryptor = new AESEncryptor();
String password = encryptor.encrypt(userId + "CMP", null);
login = userServiceImpl.login(user.getAccount(), password, null, null, true);
login.getData().put("trxToken", trxToken);
log.info("软Key登录用户登录云管平台结束");
} catch (Exception e) {
log.error("用户登录失败:", e);
return new GeneralResult(false, "用户登录失败" + e.getMessage());
}
return login;
}
private void saveUserByTrx(String userId) {
User user = userRepository.getByUserId(userId);
// 根据userId获取运控系统用户信息
@ -249,7 +320,8 @@ public class TrxServiceImpl implements TrxService {
// 获取运控系统应用列表
String sysAppList = redisTemplate.opsForValue().get("SYS_APP_LIST");
Gson gson = new Gson();
Type listType = new TypeToken<List<AppEntity>>() {}.getType();
Type listType = new TypeToken<List<AppEntity>>() {
}.getType();
List<AppEntity> appList = gson.fromJson(sysAppList, listType);
List<AppPerm> permList = JSONArray.parseArray(qxUtils.queryApp(userToken, userId), AppPerm.class);
@ -270,7 +342,7 @@ public class TrxServiceImpl implements TrxService {
}
// 获取天融信用户在权限系统的门户应用的菜单
public List<AppModule> queryQxCategoryList(String userToken ,String userId) {
public List<AppModule> queryQxCategoryList(String userToken, String userId) {
List<AppModule> appModules = JSONArray.parseArray(qxUtils.queryAppModules(userToken, userId), AppModule.class);
log.info("获取用户在指定应用中可操作的功能模块信息结果:" + JSONObject.toJSONString(appModules));
return appModules;

View File

@ -0,0 +1,60 @@
package com.bocloud.sms.service.utils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
/**
* @author tyl
* @time 2020/9/8 16:19
*/
@Slf4j
public class Sha256Util {
/**
* SHA-256
* @return
*/
public static String sha256(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -10,4 +10,5 @@ public class TrxAuthModel {
private String serverHello; // certMd5
private String trxToken;
private String clientIp;
private String randoms;
}