|
@@ -1,12 +1,17 @@
|
|
package com.css.simulation.oauth.client.controller;
|
|
package com.css.simulation.oauth.client.controller;
|
|
|
|
|
|
import api.common.pojo.common.ResponseBodyVO;
|
|
import api.common.pojo.common.ResponseBodyVO;
|
|
-import api.common.pojo.param.SignSingleParameter;
|
|
|
|
-import api.common.pojo.param.SignUsernameParameter;
|
|
|
|
-import api.common.pojo.po.UserPO;
|
|
|
|
-import api.common.pojo.vo.SimulationTokenVO;
|
|
|
|
|
|
+import api.common.pojo.constants.DictConstants;
|
|
|
|
+import api.common.pojo.param.sign.SignRefreshParameter;
|
|
|
|
+import api.common.pojo.param.sign.SignSingleParameter;
|
|
|
|
+import api.common.pojo.param.sign.SignUsernameParameter;
|
|
|
|
+import api.common.pojo.po.system.UserPO;
|
|
|
|
+import api.common.pojo.po.log.LogLoginPO;
|
|
|
|
+import api.common.pojo.vo.sign.SimulationTokenVO;
|
|
|
|
+import api.common.pojo.vo.system.UserVO;
|
|
import api.common.util.*;
|
|
import api.common.util.*;
|
|
import com.css.simulation.oauth.client.configuration.oauth.OauthParameter;
|
|
import com.css.simulation.oauth.client.configuration.oauth.OauthParameter;
|
|
|
|
+import com.css.simulation.oauth.client.mapper.LogLoginMapper;
|
|
import com.css.simulation.oauth.client.mapper.UserMapper;
|
|
import com.css.simulation.oauth.client.mapper.UserMapper;
|
|
import com.css.simulation.oauth.client.util.EncodeUtil;
|
|
import com.css.simulation.oauth.client.util.EncodeUtil;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
@@ -25,6 +30,8 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
@Controller
|
|
@Controller
|
|
@@ -32,10 +39,12 @@ import java.util.Set;
|
|
@Slf4j
|
|
@Slf4j
|
|
public class SignController {
|
|
public class SignController {
|
|
|
|
|
|
- static final String USER_ID = "simulation-oauth-client";
|
|
|
|
-
|
|
|
|
@Resource
|
|
@Resource
|
|
UserMapper userMapper;
|
|
UserMapper userMapper;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ LogLoginMapper logLoginMapper;
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
CloseableHttpClient closeableHttpClient;
|
|
CloseableHttpClient closeableHttpClient;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -160,7 +169,7 @@ public class SignController {
|
|
}
|
|
}
|
|
log.info("------- /single 众工业平台用户信息为:" + zoogooyUserJson);
|
|
log.info("------- /single 众工业平台用户信息为:" + zoogooyUserJson);
|
|
|
|
|
|
- String username = userRoot.path("data").path("loginName").asText();
|
|
|
|
|
|
+ String username = "zoogooy-" + userRoot.path("data").path("loginName").asText();
|
|
String nickname = userRoot.path("data").path("nickname").asText();
|
|
String nickname = userRoot.path("data").path("nickname").asText();
|
|
String password = EncryptUtil.getLowerMD5(oauthParameter.getSimulationDefaultPassword());
|
|
String password = EncryptUtil.getLowerMD5(oauthParameter.getSimulationDefaultPassword());
|
|
|
|
|
|
@@ -173,8 +182,11 @@ public class SignController {
|
|
newUser.setNickname(nickname);
|
|
newUser.setNickname(nickname);
|
|
newUser.setOpenid(openid);
|
|
newUser.setOpenid(openid);
|
|
newUser.setPassword(password);
|
|
newUser.setPassword(password);
|
|
- newUser.setCreateUserId(USER_ID);
|
|
|
|
- newUser.setModifyUserId(USER_ID);
|
|
|
|
|
|
+ newUser.setCompany("国汽");
|
|
|
|
+ newUser.setRoleCode(DictConstants.ROLE_CODE_SUBUESR);//默认为国汽账户子账户
|
|
|
|
+ newUser.setVisible("1");//启动页面登录功能
|
|
|
|
+ newUser.setCreateUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
|
|
+ newUser.setModifyUserId(oauthParameter.getSimulationZoogooyUserId());
|
|
newUser.setCreateTime(TimeUtil.getNowForMysql());
|
|
newUser.setCreateTime(TimeUtil.getNowForMysql());
|
|
newUser.setModifyTime(TimeUtil.getNowForMysql());
|
|
newUser.setModifyTime(TimeUtil.getNowForMysql());
|
|
newUser.setIsDeleted("0");
|
|
newUser.setIsDeleted("0");
|
|
@@ -213,21 +225,39 @@ public class SignController {
|
|
@RequestMapping("/username")
|
|
@RequestMapping("/username")
|
|
@SneakyThrows
|
|
@SneakyThrows
|
|
@ResponseBody
|
|
@ResponseBody
|
|
- public ResponseBodyVO<SimulationTokenVO> username(@RequestBody @Validated SignUsernameParameter signSingleParameter) {
|
|
|
|
-
|
|
|
|
- //1 使用 username 查询数据库,是否已在仿真平台存在该用户
|
|
|
|
|
|
+ public ResponseBodyVO<SimulationTokenVO> username(@RequestBody @Validated SignUsernameParameter signSingleParameter, HttpServletRequest request) {
|
|
|
|
+ log.info("登录参数为:" + signSingleParameter);
|
|
String username = signSingleParameter.getUsername();
|
|
String username = signSingleParameter.getUsername();
|
|
String password = signSingleParameter.getPassword();
|
|
String password = signSingleParameter.getPassword();
|
|
- log.info("登录参数为:" + signSingleParameter);
|
|
|
|
- UserPO oldUser = userMapper.selectByUsername(username);
|
|
|
|
- if (oldUser == null) { //2-1 仿真平台不存在用户,直接创建新的
|
|
|
|
- return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "不存在该用户!");
|
|
|
|
- } else { //2-2 仿真平台存在用户判断密码
|
|
|
|
- if (!oldUser.getPassword().equals(password)) {
|
|
|
|
- return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "密码错误!");
|
|
|
|
|
|
+ //密码加密
|
|
|
|
+ password = EncryptUtil.getLowerMD5(password);
|
|
|
|
+ UserVO userVO = userMapper.selectByUsername(username);
|
|
|
|
+ //登录日志
|
|
|
|
+ LogLoginPO po = new LogLoginPO();
|
|
|
|
+ po.setId(StringUtil.getRandomUUID());
|
|
|
|
+ po.setCreateTime(TimeUtil.getNowForMysql());
|
|
|
|
+ po.setIp(request.getRemoteAddr());
|
|
|
|
+ //登录逻辑判断
|
|
|
|
+ if (userVO == null ) {
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "用户名或密码错误!");
|
|
|
|
+ }else if("0".equals(userVO.getVisible())){
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "账户已停用!");
|
|
|
|
+ }else if(!userVO.getPassword().equals(password)) {
|
|
|
|
+ //记录登录错误日志
|
|
|
|
+ po.setUserId(userVO.getId());
|
|
|
|
+ po.setUsername(userVO.getUsername());
|
|
|
|
+ po.setState(0);
|
|
|
|
+ logLoginMapper.insertLog(po);
|
|
|
|
+ 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()){
|
|
|
|
+ int sum = log.stream().mapToInt(m -> m.getState()).sum();
|
|
|
|
+ if(sum <= 0){
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "登录失败次数过多,账户被锁定,请" + oauthParameter.getSimulationLoginRepeatHours() + "小时后重新登录!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
//3 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
//3 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
"?grant_type=password" +
|
|
"?grant_type=password" +
|
|
@@ -236,11 +266,41 @@ public class SignController {
|
|
"&username=" + username +
|
|
"&username=" + username +
|
|
"&password=" + password;
|
|
"&password=" + password;
|
|
String simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
String simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
- System.out.println("------- 仿真平台令牌信息为:" + simulationToken);
|
|
|
|
SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
|
|
+ //记录登录成功日志
|
|
|
|
+ po.setUserId(userVO.getId());
|
|
|
|
+ po.setUsername(userVO.getUsername());
|
|
|
|
+ po.setState(1);
|
|
|
|
+ logLoginMapper.insertLog(po);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, simulationTokenVO);
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, simulationTokenVO);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 令牌刷新
|
|
|
|
+ *
|
|
|
|
+ * @return token 信息
|
|
|
|
+ */
|
|
|
|
+ @RequestMapping("/refreshToken")
|
|
|
|
+ @ResponseBody
|
|
|
|
+ public ResponseBodyVO<SimulationTokenVO> refreshToken(@RequestBody @Validated SignRefreshParameter signRefreshParameter) {
|
|
|
|
+ String refreshToken = signRefreshParameter.getRefreshToken();
|
|
|
|
+ //刷新令牌
|
|
|
|
+ String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
|
+ "?grant_type=refresh_token" +
|
|
|
|
+ "&client_id=" + oauthParameter.getSimulationClientId() +
|
|
|
|
+ "&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
|
+ "&refresh_token=" + refreshToken;
|
|
|
|
+ try {
|
|
|
|
+ String simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
|
|
+ SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, simulationTokenVO);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "Invalid refresh token");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 服务监控
|
|
* 服务监控
|
|
*/
|
|
*/
|