|
@@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.time.Duration;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
@@ -94,27 +95,36 @@ public class SignController {
|
|
public ResponseBodyVO<SimulationTokenVO> single(@RequestBody SignSingleParameter signSingleParameter, HttpServletRequest request) {
|
|
public ResponseBodyVO<SimulationTokenVO> single(@RequestBody SignSingleParameter signSingleParameter, HttpServletRequest request) {
|
|
|
|
|
|
String code = signSingleParameter.getCode();
|
|
String code = signSingleParameter.getCode();
|
|
- String encodeCode = EncodeUtil.utf8(code);
|
|
|
|
String ticket = signSingleParameter.getTicket();
|
|
String ticket = signSingleParameter.getTicket();
|
|
- String encodeTicket = EncodeUtil.utf8(ticket);
|
|
|
|
- log.info("------- /single 接收到的 code 为:" + code);
|
|
|
|
- log.info("------- /single code 编码之后为:" + encodeCode);
|
|
|
|
- log.info("------- /single 接收到的 ticket 为:" + ticket);
|
|
|
|
- log.info("------- /single ticket 编码之后为:" + encodeTicket);
|
|
|
|
- String zoogooyTokenUrl = oauthParameter.getZoogooyTokenUri() +
|
|
|
|
- "?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
|
- "&secret=" + oauthParameter.getZoogooyAppSecret() +
|
|
|
|
- "&code=" + encodeCode +
|
|
|
|
- "&grant_type=authorization_code";
|
|
|
|
- log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的地址为:" + zoogooyTokenUrl);
|
|
|
|
- String zoogooyTokenJson = HttpUtil.get(closeableHttpClient, requestConfig, zoogooyTokenUrl);
|
|
|
|
- log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的结果为:" + zoogooyTokenJson);
|
|
|
|
- ObjectMapper objectMapper1 = new ObjectMapper();
|
|
|
|
- JsonNode tokenRoot = objectMapper1.readTree(zoogooyTokenJson);
|
|
|
|
- if (!tokenRoot.path("success").asBoolean()) {
|
|
|
|
- throw new RuntimeException(tokenRoot.path("message").asText());
|
|
|
|
- }
|
|
|
|
- log.info("------- /single 众工业平台 token 信息为:" + zoogooyTokenJson);
|
|
|
|
|
|
+
|
|
|
|
+ String simulationToken;
|
|
|
|
+ String userId = "";
|
|
|
|
+ String username = null;
|
|
|
|
+ // 首先判断 redis 里是否存在
|
|
|
|
+ if (Boolean.TRUE.equals(redisTemplate.hasKey(("code:" + code + ":ticket:" + ticket)))) {
|
|
|
|
+ simulationToken = redisTemplate.opsForValue().get("code:" + code + ":ticket:" + ticket);
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ String encodeCode = EncodeUtil.utf8(code);
|
|
|
|
+ String encodeTicket = EncodeUtil.utf8(ticket);
|
|
|
|
+ log.info("------- /single 接收到的 code 为:" + code);
|
|
|
|
+ log.info("------- /single code 编码之后为:" + encodeCode);
|
|
|
|
+ log.info("------- /single 接收到的 ticket 为:" + ticket);
|
|
|
|
+ log.info("------- /single ticket 编码之后为:" + encodeTicket);
|
|
|
|
+ String zoogooyTokenUrl = oauthParameter.getZoogooyTokenUri() +
|
|
|
|
+ "?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
|
+ "&secret=" + oauthParameter.getZoogooyAppSecret() +
|
|
|
|
+ "&code=" + encodeCode +
|
|
|
|
+ "&grant_type=authorization_code";
|
|
|
|
+ log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的地址为:" + zoogooyTokenUrl);
|
|
|
|
+ String zoogooyTokenJson = HttpUtil.get(closeableHttpClient, requestConfig, zoogooyTokenUrl);
|
|
|
|
+ log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的结果为:" + zoogooyTokenJson);
|
|
|
|
+ ObjectMapper objectMapper1 = new ObjectMapper();
|
|
|
|
+ JsonNode tokenRoot = objectMapper1.readTree(zoogooyTokenJson);
|
|
|
|
+ if (!tokenRoot.path("success").asBoolean()) {
|
|
|
|
+ throw new RuntimeException(tokenRoot.path("message").asText());
|
|
|
|
+ }
|
|
|
|
+ log.info("------- /single 众工业平台 token 信息为:" + zoogooyTokenJson);
|
|
/*
|
|
/*
|
|
{
|
|
{
|
|
"data" : {
|
|
"data" : {
|
|
@@ -133,20 +143,20 @@ public class SignController {
|
|
}
|
|
}
|
|
*/
|
|
*/
|
|
|
|
|
|
- String accessToken = tokenRoot.path("data").path("access_token").asText();
|
|
|
|
- String encodeAccessToken = EncodeUtil.utf8(accessToken);
|
|
|
|
- String openid = tokenRoot.path("data").path("openid").asText();
|
|
|
|
- String encodeOpenid = EncodeUtil.utf8(openid);
|
|
|
|
|
|
+ String accessToken = tokenRoot.path("data").path("access_token").asText();
|
|
|
|
+ String encodeAccessToken = EncodeUtil.utf8(accessToken);
|
|
|
|
+ String openid = tokenRoot.path("data").path("openid").asText();
|
|
|
|
+ String encodeOpenid = EncodeUtil.utf8(openid);
|
|
|
|
|
|
- log.info("------- /single 众工业平台 access_token 为:" + accessToken);
|
|
|
|
- log.info("------- /single 众工业平台 access_token 编码之后为:" + encodeAccessToken);
|
|
|
|
- log.info("------- /single 众工业平台 openid 为:" + openid);
|
|
|
|
- log.info("------- /single 众工业平台 openid 编码之后为:" + encodeOpenid);
|
|
|
|
- String zoogooyUserUrl = oauthParameter.getZoogooyUserUri() +
|
|
|
|
- "?access_token=" + encodeAccessToken +
|
|
|
|
- "&openid=" + encodeOpenid +
|
|
|
|
- "&ticket=" + encodeTicket;
|
|
|
|
- log.info("------- /single 根据编码之后的 access_token、openid、ticket 获取众工业平台用户信息:" + zoogooyUserUrl);
|
|
|
|
|
|
+ log.info("------- /single 众工业平台 access_token 为:" + accessToken);
|
|
|
|
+ log.info("------- /single 众工业平台 access_token 编码之后为:" + encodeAccessToken);
|
|
|
|
+ log.info("------- /single 众工业平台 openid 为:" + openid);
|
|
|
|
+ log.info("------- /single 众工业平台 openid 编码之后为:" + encodeOpenid);
|
|
|
|
+ String zoogooyUserUrl = oauthParameter.getZoogooyUserUri() +
|
|
|
|
+ "?access_token=" + encodeAccessToken +
|
|
|
|
+ "&openid=" + encodeOpenid +
|
|
|
|
+ "&ticket=" + encodeTicket;
|
|
|
|
+ log.info("------- /single 根据编码之后的 access_token、openid、ticket 获取众工业平台用户信息:" + zoogooyUserUrl);
|
|
/*
|
|
/*
|
|
{
|
|
{
|
|
"data" : {
|
|
"data" : {
|
|
@@ -162,61 +172,65 @@ public class SignController {
|
|
"nowTime" : "2022-03-28 11:11:09"
|
|
"nowTime" : "2022-03-28 11:11:09"
|
|
}
|
|
}
|
|
*/
|
|
*/
|
|
- String zoogooyUserJson = HttpUtil.get(closeableHttpClient, requestConfig, zoogooyUserUrl);
|
|
|
|
- ObjectMapper objectMapper2 = new ObjectMapper();
|
|
|
|
- JsonNode userRoot = objectMapper2.readTree(zoogooyUserJson);
|
|
|
|
- if (!userRoot.path("success").asBoolean()) {
|
|
|
|
- throw new RuntimeException("------- /single 获取众工业平台用户信息出错:" + zoogooyUserJson);
|
|
|
|
- }
|
|
|
|
- log.info("------- /single 众工业平台用户信息为:" + zoogooyUserJson);
|
|
|
|
|
|
+ String zoogooyUserJson = HttpUtil.get(closeableHttpClient, requestConfig, zoogooyUserUrl);
|
|
|
|
+ ObjectMapper objectMapper2 = new ObjectMapper();
|
|
|
|
+ JsonNode userRoot = objectMapper2.readTree(zoogooyUserJson);
|
|
|
|
+ if (!userRoot.path("success").asBoolean()) {
|
|
|
|
+ throw new RuntimeException("------- /single 获取众工业平台用户信息出错:" + zoogooyUserJson);
|
|
|
|
+ }
|
|
|
|
+ log.info("------- /single 众工业平台用户信息为:" + zoogooyUserJson);
|
|
|
|
|
|
- String username = "zoogooy-" + userRoot.path("data").path("loginName").asText();
|
|
|
|
- String nickname = userRoot.path("data").path("nickname").asText();
|
|
|
|
- String password = EncryptUtil.getLowerMD5(oauthParameter.getSimulationDefaultPassword());
|
|
|
|
|
|
+ username = "zoogooy-" + userRoot.path("data").path("loginName").asText();
|
|
|
|
+ String nickname = userRoot.path("data").path("nickname").asText();
|
|
|
|
+ String password = EncryptUtil.getLowerMD5(oauthParameter.getSimulationDefaultPassword());
|
|
|
|
|
|
- //3 使用 union_id 查询数据库,是否已在仿真平台存在该用户
|
|
|
|
- UserPO oldUser = userMapper.selectByOpenIdIgnoreDelete(openid);
|
|
|
|
- String userId = "";
|
|
|
|
- if (oldUser == null) { //3-1 仿真平台不存在用户,直接创建新的
|
|
|
|
- UserPO newUser = new UserPO();
|
|
|
|
- newUser.setId(StringUtil.getRandomUUID());
|
|
|
|
- newUser.setUsername(username);
|
|
|
|
- newUser.setNickname(nickname);
|
|
|
|
- newUser.setOpenid(openid);
|
|
|
|
- newUser.setPassword(password);
|
|
|
|
- newUser.setCompany("国汽");
|
|
|
|
- newUser.setRoleCode(DictConstants.ROLE_CODE_SUBUESR);// 默认为索为普通账户的子账户
|
|
|
|
- newUser.setUseType(DictConstants.USER_TYPE_PUBLIC);// 索为账户默认为共享用户
|
|
|
|
- newUser.setVisible("1");//启用登录功能
|
|
|
|
- newUser.setCreateUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
|
|
- newUser.setModifyUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
|
|
- newUser.setCreateTime(TimeUtil.getNowForMysql());
|
|
|
|
- newUser.setModifyTime(TimeUtil.getNowForMysql());
|
|
|
|
- newUser.setIsDeleted("0");
|
|
|
|
- userMapper.insert(newUser);
|
|
|
|
- userId = newUser.getId();
|
|
|
|
- log.info("------- /single 成功插入新用户信息:" + newUser);
|
|
|
|
- } else {
|
|
|
|
- if ("1".equals(oldUser.getIsDeleted())) { //3-2 仿真平台存在删除状态用户,改为未删除
|
|
|
|
- userMapper.updateIsDeleted(openid, "0");
|
|
|
|
|
|
+ //3 使用 union_id 查询数据库,是否已在仿真平台存在该用户
|
|
|
|
+ UserPO oldUser = userMapper.selectByOpenIdIgnoreDelete(openid);
|
|
|
|
+ userId = "";
|
|
|
|
+ if (oldUser == null) { //3-1 仿真平台不存在用户,直接创建新的
|
|
|
|
+ UserPO newUser = new UserPO();
|
|
|
|
+ newUser.setId(StringUtil.getRandomUUID());
|
|
|
|
+ newUser.setUsername(username);
|
|
|
|
+ newUser.setNickname(nickname);
|
|
|
|
+ newUser.setOpenid(openid);
|
|
|
|
+ newUser.setPassword(password);
|
|
|
|
+ newUser.setCompany("国汽");
|
|
|
|
+ newUser.setRoleCode(DictConstants.ROLE_CODE_SUBUESR);// 默认为索为普通账户的子账户
|
|
|
|
+ newUser.setUseType(DictConstants.USER_TYPE_PUBLIC);// 索为账户默认为共享用户
|
|
|
|
+ newUser.setVisible("1");//启用登录功能
|
|
|
|
+ newUser.setCreateUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
|
|
+ newUser.setModifyUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
|
|
+ newUser.setCreateTime(TimeUtil.getNowForMysql());
|
|
|
|
+ newUser.setModifyTime(TimeUtil.getNowForMysql());
|
|
|
|
+ newUser.setIsDeleted("0");
|
|
|
|
+ userMapper.insert(newUser);
|
|
|
|
+ userId = newUser.getId();
|
|
|
|
+ log.info("------- /single 成功插入新用户信息:" + newUser);
|
|
|
|
+ } else {
|
|
|
|
+ if ("1".equals(oldUser.getIsDeleted())) { //3-2 仿真平台存在删除状态用户,改为未删除
|
|
|
|
+ userMapper.updateIsDeleted(openid, "0");
|
|
|
|
+ }
|
|
|
|
+ //3-3 仿真平台用户存在未删除用户,放行。不会每次都更新,防止在仿真云平台更新的信息被众工业覆盖
|
|
|
|
+ username = oldUser.getUsername();
|
|
|
|
+ password = oldUser.getPassword();
|
|
|
|
+ userId = oldUser.getId();
|
|
}
|
|
}
|
|
- //3-3 仿真平台用户存在未删除用户,放行。不会每次都更新,防止在仿真云平台更新的信息被众工业覆盖
|
|
|
|
- username = oldUser.getUsername();
|
|
|
|
- password = oldUser.getPassword();
|
|
|
|
- userId = oldUser.getId();
|
|
|
|
|
|
+
|
|
|
|
+ // -------------------------------- 这里需要用到前面创建的用户信息,所以不使用事务 --------------------------------
|
|
|
|
+ //4 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
|
|
+ String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
|
+ "?grant_type=password" +
|
|
|
|
+ "&client_id=" + oauthParameter.getSimulationClientId() +
|
|
|
|
+ "&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
|
+ "&username=" + username +
|
|
|
|
+ "&password=" + password;
|
|
|
|
+ log.info("------- /single 获取仿真云平台 token 信息:" + simulationTokenUrl);
|
|
|
|
+ simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
|
|
+ log.info("------- /single 仿真云平台 token 信息为:" + simulationToken);
|
|
|
|
+ redisTemplate.opsForValue().set("code:" + code + ":ticket:" + ticket, simulationToken, Duration.ofMinutes(30L));
|
|
}
|
|
}
|
|
|
|
|
|
- // -------------------------------- 这里需要用到前面创建的用户信息,所以不使用事务 --------------------------------
|
|
|
|
- //4 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
|
|
- String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
|
- "?grant_type=password" +
|
|
|
|
- "&client_id=" + oauthParameter.getSimulationClientId() +
|
|
|
|
- "&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
|
- "&username=" + username +
|
|
|
|
- "&password=" + password;
|
|
|
|
- log.info("------- /single 获取仿真云平台 token 信息:" + simulationTokenUrl);
|
|
|
|
- String simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
|
|
- log.info("------- /single 仿真云平台 token 信息为:" + simulationToken);
|
|
|
|
|
|
+
|
|
SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
//登录日志
|
|
//登录日志
|
|
LogLoginPO po = new LogLoginPO();
|
|
LogLoginPO po = new LogLoginPO();
|
|
@@ -256,11 +270,11 @@ public class SignController {
|
|
po.setIp(remoteAddress);
|
|
po.setIp(remoteAddress);
|
|
|
|
|
|
//登录逻辑判断
|
|
//登录逻辑判断
|
|
- if (userVO == null ) {
|
|
|
|
|
|
+ if (userVO == null) {
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "用户名或密码错误!");
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "用户名或密码错误!");
|
|
- }else if(!"1".equals(userVO.getVisible())){
|
|
|
|
|
|
+ } else if (!"1".equals(userVO.getVisible())) {
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "账户已停用!");
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "账户已停用!");
|
|
- }else if(!userVO.getPassword().equals(password)) {
|
|
|
|
|
|
+ } else if (!userVO.getPassword().equals(password)) {
|
|
//记录登录错误日志
|
|
//记录登录错误日志
|
|
po.setUserId(userVO.getId());
|
|
po.setUserId(userVO.getId());
|
|
po.setUsername(userVO.getUsername());
|
|
po.setUsername(userVO.getUsername());
|
|
@@ -269,10 +283,10 @@ public class SignController {
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "用户名或密码错误!");
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "用户名或密码错误!");
|
|
}
|
|
}
|
|
//根据登录日志判断是否锁定账户
|
|
//根据登录日志判断是否锁定账户
|
|
- List<LogLoginPO> log = logLoginMapper.selectLogByUserId(userVO.getId(),oauthParameter.getSimulationLoginRepeatHours(),oauthParameter.getSimulationLoginRepeatCount());
|
|
|
|
- if(ObjectUtil.isNotNull(log) && log.size() >= oauthParameter.getSimulationLoginRepeatCount()){
|
|
|
|
|
|
+ List<LogLoginPO> log = logLoginMapper.selectLogByUserId(userVO.getId(), oauthParameter.getSimulationLoginRepeatHours(), oauthParameter.getSimulationLoginRepeatCount());
|
|
|
|
+ if (ObjectUtil.isNotNull(log) && log.size() >= oauthParameter.getSimulationLoginRepeatCount()) {
|
|
int sum = log.stream().mapToInt(m -> m.getState()).sum();
|
|
int sum = log.stream().mapToInt(m -> m.getState()).sum();
|
|
- if(sum <= 0){
|
|
|
|
|
|
+ if (sum <= 0) {
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "登录失败次数过多,账户被锁定,请" + oauthParameter.getSimulationLoginRepeatHours() + "小时后重新登录!");
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "登录失败次数过多,账户被锁定,请" + oauthParameter.getSimulationLoginRepeatHours() + "小时后重新登录!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -335,7 +349,7 @@ public class SignController {
|
|
/**
|
|
/**
|
|
* 服务监控
|
|
* 服务监控
|
|
*/
|
|
*/
|
|
- @RequestMapping ("online")
|
|
|
|
|
|
+ @RequestMapping("online")
|
|
@ResponseBody
|
|
@ResponseBody
|
|
public ResponseBodyVO<Integer> online() {
|
|
public ResponseBodyVO<Integer> online() {
|
|
int online;
|
|
int online;
|