说明
大佬的脚手架地址奉上:https://gitee.com/duxiaod/irs。此脚手架大佬已不在更新,此修改仅为个人爱好。
ShiroUtil.java修改
添加了下面的代码
// 遍历同一个账户的session
public static List<Session> getLoginedSession(Subject currentUser) {
Collection<Session> list = ((DefaultSessionManager) ((DefaultSecurityManager) SecurityUtils
.getSecurityManager()).getSessionManager()).getSessionDAO().getActiveSessions();
List<Session> loginedList = new ArrayList<Session>();
TbAdmin loginUser = (TbAdmin) currentUser.getPrincipal();
for (Session session : list) {
Subject s = new Subject.Builder().session(session).buildSubject();
if (s.isAuthenticated()) {
TbAdmin user = (TbAdmin) s.getPrincipal();
if (user.getUsername().equalsIgnoreCase(loginUser.getUsername())) {
if (!session.getId().equals(currentUser.getSession().getId())) {
loginedList.add(session);
}
}
}
}
return loginedList;
}
AdminController.java修改
添加了
“ // 剔除其他此账号在其它地方登录,实现一个账户不能同时在多个地方登录
List<Session> loginedList = ShiroUtils.getLoginedSession(subject);
for (Session session : loginedList) {
session.stop();
}
”
这一段代码,完整代码如下:
@RequestMapping("/login")
@ResponseBody
public ResultUtil login(HttpServletRequest req, String username, String password/*, String vcode*/) {
if(/*StringUtils.isEmpty(vcode)||*/StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
throw new RRException("参数不能为空");
}
/*String kaptcha = ShiroUtils.getKaptcha("kaptcha").toLowerCase();
if(!vcode.toLowerCase().equals(kaptcha)){
return ResultUtil.error("验证码不正确");
}*/
try{
Subject subject = ShiroUtils.getSubject();
//md5加密
//password=DigestUtils.md5DigestAsHex(password.getBytes());
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
// 剔除其他此账号在其它地方登录,实现一个账户不能同时在多个地方登录
List<Session> loginedList = ShiroUtils.getLoginedSession(subject);
for (Session session : loginedList) {
session.stop();
}
}catch (UnknownAccountException e) {
return ResultUtil.error(e.getMessage());
}catch (IncorrectCredentialsException e) {
return ResultUtil.error(e.getMessage());
}catch (LockedAccountException e) {
return ResultUtil.error(e.getMessage());
}catch (AuthenticationException e) {
return ResultUtil.error("账户验证失败");
}
return ResultUtil.ok();
/*String vCode = req.getSession().getAttribute("vcode").toString().toLowerCase();
if (vcode.toLowerCase().equals(vCode)) {
TbAdmin admin = adminServiceImpl.login(username, password);
if (admin != null) {
// 登陆成功
// 将密码置空
admin.setPassword("");
// 设置用户信息到Session作用域
req.getSession().setAttribute("admin", admin);
return new ResultUtil(0);
}
return new ResultUtil(502, "用户名或密码错误!");
}
return new ResultUtil(501, "验证码错误!");*/
}