|
@@ -3,6 +3,8 @@ package cn.rankin.apiweb.service.user;
|
|
|
import cn.rankin.apiweb.code.ApiWebCode;
|
|
|
import cn.rankin.apiweb.utils.SecurityManager;
|
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
|
+import cn.rankin.common.utils.api.model.BaseCode;
|
|
|
+import cn.rankin.common.utils.constant.PlatForm;
|
|
|
import cn.rankin.common.utils.constant.RedisKey;
|
|
|
import cn.rankin.common.utils.service.RedisService;
|
|
|
import cn.rankin.data.api.app.dto.LoginInfoDTO;
|
|
@@ -30,6 +32,9 @@ public class UserService {
|
|
|
@Value(value = "${token.expiration:604800}")
|
|
|
private int expiration;
|
|
|
|
|
|
+ @Value(value = "${login.web.force.interval:2400}")
|
|
|
+ private int forceLoginInterval;
|
|
|
+
|
|
|
@Autowired
|
|
|
private UserClient userClient;
|
|
|
|
|
@@ -39,7 +44,7 @@ public class UserService {
|
|
|
public APIResult<UserInfoVo> login(LoginInfoDTO loginInfoDTO) {
|
|
|
String deviceCode = loginInfoDTO.getDeviceCode();
|
|
|
String eid = loginInfoDTO.getEid();
|
|
|
- String password = loginInfoDTO.getPassword();
|
|
|
+ String loginPassword = loginInfoDTO.getPassword();
|
|
|
log.info("user login start, user={}", JSON.toJSONString(loginInfoDTO));
|
|
|
|
|
|
APIResult<TerminalUserVo> userVoAPIResult = userClient.loadUserByEid(eid);
|
|
@@ -49,15 +54,34 @@ public class UserService {
|
|
|
return APIResult.error(ApiWebCode.LOGIN_ERROR);
|
|
|
}
|
|
|
|
|
|
- String key = userVo.getPassword();
|
|
|
- if (!SecurityManager.validate(password, key)) {
|
|
|
- log.error("密码校验错误, password={}, key={}", password, key);
|
|
|
+ String storePassword = userVo.getPassword();
|
|
|
+ if (!SecurityManager.validate(loginPassword, storePassword)) {
|
|
|
+ log.error("密码校验错误, loginPassword={}, storePassword={}", loginPassword, storePassword);
|
|
|
return APIResult.error(ApiWebCode.PASSWORD_ERROR);
|
|
|
}
|
|
|
|
|
|
String userId = userVo.getId();
|
|
|
String ip = loginInfoDTO.getIp();
|
|
|
- TerminalDeviceVo deviceVo = this.bind(userId, deviceCode, ip);
|
|
|
+ String terminal = loginInfoDTO.getTerminal();
|
|
|
+
|
|
|
+ // 网页端登陆在40分钟后可以踢掉对方网页登陆
|
|
|
+ if (PlatForm.WEB.equals(terminal)) {
|
|
|
+ String key = getUserFormatKey(userId);
|
|
|
+ DeviceUserVo deviceUserVo = (DeviceUserVo) redisService.get(key);
|
|
|
+ if (deviceUserVo != null ) {
|
|
|
+ Date refreshAt = deviceUserVo.getRefreshAt();
|
|
|
+ String lastTerminal = deviceUserVo.getTerminal();
|
|
|
+ if (DateUtils.addSeconds(refreshAt, forceLoginInterval).before(new Date()) && PlatForm.WEB.equals(lastTerminal)) {
|
|
|
+ log.info("网页端强制登陆, deviceCode={}, eid={}", deviceCode, eid);
|
|
|
+ APIResult<Boolean> forceLogoutResult = this.logout(userId);
|
|
|
+ if (!forceLogoutResult.getSuccess()) {
|
|
|
+ return APIResult.error(new BaseCode(forceLogoutResult.getCode(), forceLogoutResult.getMessage()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ TerminalDeviceVo deviceVo = this.bind(userId, deviceCode, ip, terminal);
|
|
|
if (deviceVo == null) {
|
|
|
return APIResult.error(ApiWebCode.DEVICE_BOUND_ERROR);
|
|
|
}
|
|
@@ -85,11 +109,12 @@ public class UserService {
|
|
|
return APIResult.ok();
|
|
|
}
|
|
|
|
|
|
- public TerminalDeviceVo bind(String userId, String deviceCode, String ip) {
|
|
|
+ public TerminalDeviceVo bind(String userId, String deviceCode, String ip, String terminal) {
|
|
|
TerminalDeviceDTO deviceDTO = new TerminalDeviceDTO();
|
|
|
deviceDTO.setUserId(userId);
|
|
|
deviceDTO.setDeviceCode(deviceCode);
|
|
|
deviceDTO.setIp(ip);
|
|
|
+ deviceDTO.setTerminal(terminal);
|
|
|
APIResult<TerminalDeviceVo> apiResult = userClient.deviceBind(deviceDTO);
|
|
|
if (!apiResult.getSuccess()) {
|
|
|
log.error("bind user api error");
|
|
@@ -104,6 +129,8 @@ public class UserService {
|
|
|
DeviceUserVo deviceUserVo = (DeviceUserVo) redisService.get(key);
|
|
|
if (deviceUserVo == null) {
|
|
|
deviceUserVo = getDeviceUserVo(uid);
|
|
|
+ }
|
|
|
+ if (deviceUserVo != null) {
|
|
|
this.save(deviceUserVo);
|
|
|
}
|
|
|
return deviceUserVo;
|
|
@@ -208,14 +235,17 @@ public class UserService {
|
|
|
deviceUserVo.setMerchantName(userVo.getMerchantName());
|
|
|
deviceUserVo.setMerchantContactName(userVo.getMerchantContactName());
|
|
|
deviceUserVo.setMerchantContactMobile(userVo.getMerchantContactMobile());
|
|
|
+ deviceUserVo.setTerminal(deviceVo.getTerminal());
|
|
|
refreshToken(deviceUserVo);
|
|
|
return deviceUserVo;
|
|
|
}
|
|
|
|
|
|
public void refreshToken(DeviceUserVo deviceUserVo) {
|
|
|
String token = SecurityManager.generateToken(deviceUserVo.getPassword());
|
|
|
- Date expireAt = DateUtils.addSeconds(new Date(), this.expiration);
|
|
|
+ Date nowTime = new Date();
|
|
|
+ Date expireAt = DateUtils.addSeconds(nowTime, this.expiration);
|
|
|
deviceUserVo.setToken(token);
|
|
|
deviceUserVo.setExpireAt(expireAt);
|
|
|
+ deviceUserVo.setRefreshAt(nowTime);
|
|
|
}
|
|
|
}
|