同一账号只能单次登录
This commit is contained in:
parent
66a0bab855
commit
6bc572ff79
@ -86,4 +86,9 @@ public interface HttpStatus
|
|||||||
* 接口未实现
|
* 接口未实现
|
||||||
*/
|
*/
|
||||||
public static final int NOT_IMPLEMENTED = 501;
|
public static final int NOT_IMPLEMENTED = 501;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 已登陆
|
||||||
|
*/
|
||||||
|
public static final int ALREADY_LOGIN = 502;
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,16 @@ public class CustomException extends RuntimeException
|
|||||||
|
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
|
public String getObj() {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setObj(String obj) {
|
||||||
|
this.obj = obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String obj;
|
||||||
|
|
||||||
public CustomException(String message)
|
public CustomException(String message)
|
||||||
{
|
{
|
||||||
this.message = message;
|
this.message = message;
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package com.ruoyi.framework.security.service;
|
package com.ruoyi.framework.security.service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import com.ruoyi.common.constant.HttpStatus;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.project.monitor.domain.SysUserOnline;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
@ -18,6 +22,10 @@ import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|||||||
import com.ruoyi.framework.redis.RedisCache;
|
import com.ruoyi.framework.redis.RedisCache;
|
||||||
import com.ruoyi.framework.security.LoginUser;
|
import com.ruoyi.framework.security.LoginUser;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录校验方法
|
* 登录校验方法
|
||||||
*
|
*
|
||||||
@ -80,6 +88,23 @@ public class SysLoginService
|
|||||||
throw new CustomException(e.getMessage());
|
throw new CustomException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Collection<String> keys = redisCache.keys(Constants.LOGIN_TOKEN_KEY + "*");
|
||||||
|
List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
|
||||||
|
for (String key : keys) {
|
||||||
|
LoginUser user = redisCache.getCacheObject(key);
|
||||||
|
if (StringUtils.isNotEmpty(username) && StringUtils.isNotNull(user.getUser()))
|
||||||
|
{
|
||||||
|
if (StringUtils.equals(username, user.getUsername()))
|
||||||
|
{
|
||||||
|
//存在已经登录用户,抛出异常
|
||||||
|
CustomException alreadyLoginExcep = new CustomException("该账号已在别处登陆", HttpStatus.ALREADY_LOGIN);
|
||||||
|
alreadyLoginExcep.setObj(username);
|
||||||
|
throw alreadyLoginExcep;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
||||||
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||||
// 生成token
|
// 生成token
|
||||||
|
@ -145,4 +145,17 @@ public class AjaxResult extends HashMap<String, Object>
|
|||||||
{
|
{
|
||||||
return new AjaxResult(code, msg, null);
|
return new AjaxResult(code, msg, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回错误消息
|
||||||
|
*
|
||||||
|
* @param code 状态码
|
||||||
|
* @param msg 返回内容
|
||||||
|
* @return 警告消息
|
||||||
|
*/
|
||||||
|
public static AjaxResult error(int code, String msg, Object data)
|
||||||
|
{
|
||||||
|
return new AjaxResult(code, msg, data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,9 @@ public class GlobalExceptionHandler
|
|||||||
{
|
{
|
||||||
return AjaxResult.error(e.getMessage());
|
return AjaxResult.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
if (e.getObj()!=null){
|
||||||
|
return AjaxResult.error(e.getCode(),e.getMessage(),e.getObj());
|
||||||
|
}
|
||||||
return AjaxResult.error(e.getCode(), e.getMessage());
|
return AjaxResult.error(e.getCode(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,8 @@ package com.ruoyi.project.system.controller;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.ruoyi.framework.redis.RedisCache;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -39,6 +41,9 @@ public class SysLoginController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TokenService tokenService;
|
private TokenService tokenService;
|
||||||
|
|
||||||
|
// @Autowired
|
||||||
|
// private RedisCache redisCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录方法
|
* 登录方法
|
||||||
*
|
*
|
||||||
@ -59,6 +64,20 @@ public class SysLoginController
|
|||||||
return ajax;
|
return ajax;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 将在其他地方登陆的账号强退
|
||||||
|
// *
|
||||||
|
// * @param user 登录信息
|
||||||
|
// * @return 结果
|
||||||
|
// */
|
||||||
|
// @PostMapping("/forceLogout")
|
||||||
|
// public AjaxResult forcelogin(@RequestBody LoginUser user)
|
||||||
|
// {
|
||||||
|
// redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + user.getToken());
|
||||||
|
//
|
||||||
|
// return AjaxResult.success();
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取用户信息
|
* 获取用户信息
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user