|
|
@@ -2,6 +2,7 @@ package com.ktg.framework.web.service;
|
|
|
|
|
|
import com.ktg.common.constant.Constants;
|
|
|
import com.ktg.common.core.domain.entity.SysUser;
|
|
|
+import com.ktg.common.core.domain.model.LoginBody;
|
|
|
import com.ktg.common.core.domain.model.LoginUser;
|
|
|
import com.ktg.common.core.redis.RedisCache;
|
|
|
import com.ktg.common.exception.ServiceException;
|
|
|
@@ -18,7 +19,10 @@ import com.ktg.framework.manager.factory.AsyncFactory;
|
|
|
import com.ktg.system.service.ISysConfigService;
|
|
|
import com.ktg.system.service.ISysUserOnlineService;
|
|
|
import com.ktg.system.service.ISysUserService;
|
|
|
+import io.jsonwebtoken.Claims;
|
|
|
+import io.jsonwebtoken.Jwts;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
@@ -36,8 +40,7 @@ import javax.annotation.Resource;
|
|
|
* @author guoruan
|
|
|
*/
|
|
|
@Component
|
|
|
-public class SysLoginService
|
|
|
-{
|
|
|
+public class SysLoginService {
|
|
|
@Autowired
|
|
|
private TokenService tokenService;
|
|
|
@Resource
|
|
|
@@ -52,7 +55,9 @@ public class SysLoginService
|
|
|
private ISysUserOnlineService userOnlineService;
|
|
|
@Resource
|
|
|
private SysPermissionService permissionService;
|
|
|
-
|
|
|
+ // 令牌秘钥
|
|
|
+ @Value("${token.secret}")
|
|
|
+ private String secret;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -60,35 +65,27 @@ public class SysLoginService
|
|
|
*
|
|
|
* @param username 用户名
|
|
|
* @param password 密码
|
|
|
- * @param code 验证码
|
|
|
- * @param uuid 唯一标识
|
|
|
+ * @param code 验证码
|
|
|
+ * @param uuid 唯一标识
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public String login(String username, String password, String code, String uuid)
|
|
|
- {
|
|
|
+ public String login(String username, String password, String code, String uuid) {
|
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
|
|
// 验证码开关
|
|
|
- if (captchaOnOff)
|
|
|
- {
|
|
|
+ if (captchaOnOff) {
|
|
|
validateCaptcha(username, code, uuid);
|
|
|
}
|
|
|
// 用户验证
|
|
|
Authentication authentication = null;
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
authentication = authenticationManager
|
|
|
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof BadCredentialsException) {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
throw new ServiceException(e.getMessage());
|
|
|
}
|
|
|
@@ -99,7 +96,7 @@ public class SysLoginService
|
|
|
// 生成token
|
|
|
String token = tokenService.createToken(loginUser);
|
|
|
// 清理多余的登录者
|
|
|
- userOnlineService.forceLogoutByName(username);
|
|
|
+ // userOnlineService.forceLogoutByName(username);
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
@@ -134,22 +131,19 @@ public class SysLoginService
|
|
|
* 校验验证码
|
|
|
*
|
|
|
* @param username 用户名
|
|
|
- * @param code 验证码
|
|
|
- * @param uuid 唯一标识
|
|
|
+ * @param code 验证码
|
|
|
+ * @param uuid 唯一标识
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public void validateCaptcha(String username, String code, String uuid)
|
|
|
- {
|
|
|
+ public void validateCaptcha(String username, String code, String uuid) {
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
|
|
|
String captcha = redisCache.getCacheObject(verifyKey);
|
|
|
redisCache.deleteObject(verifyKey);
|
|
|
- if (captcha == null)
|
|
|
- {
|
|
|
+ if (captcha == null) {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
|
throw new CaptchaExpireException();
|
|
|
}
|
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
|
- {
|
|
|
+ if (!code.equalsIgnoreCase(captcha)) {
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
throw new CaptchaException();
|
|
|
}
|
|
|
@@ -160,12 +154,36 @@ public class SysLoginService
|
|
|
*
|
|
|
* @param userId 用户ID
|
|
|
*/
|
|
|
- public void recordLoginInfo(Long userId)
|
|
|
- {
|
|
|
+ public void recordLoginInfo(Long userId) {
|
|
|
SysUser sysUser = new SysUser();
|
|
|
sysUser.setUserId(userId);
|
|
|
sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
userService.updateUserProfile(sysUser);
|
|
|
}
|
|
|
+
|
|
|
+ public String machineLogin(LoginBody loginBody) {
|
|
|
+ // 生成令牌
|
|
|
+ String token = login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
|
+ loginBody.getUuid());
|
|
|
+
|
|
|
+ // 因为只能同时在线一个,清理之前的登录
|
|
|
+ String oldToken = redisCache.getCacheObject(Constants.CABINET_LOGIN_TOKEN_KEY + loginBody.getUsername());
|
|
|
+ // 删除之前的登陆信息
|
|
|
+ if (StringUtils.isNotEmpty(oldToken)) {
|
|
|
+ try {
|
|
|
+ Claims claims = Jwts.parser()
|
|
|
+ .setSigningKey(secret)
|
|
|
+ .parseClaimsJws(oldToken)
|
|
|
+ .getBody();
|
|
|
+ // 解析对应的权限以及用户信息
|
|
|
+ String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
|
|
|
+ redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + uuid);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 更换上次登录信息的记录
|
|
|
+ redisCache.setCacheObject(Constants.CABINET_LOGIN_TOKEN_KEY + loginBody.getUsername(), token);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
}
|