說明
大佬的腳手架地址奉上: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修改
添加了
“?// 剔除其他此賬號在其它地方登錄,實現(xiàn)一個賬戶不能同時在多個地方登錄
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("參數(shù)不能為空");
}
/*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);
// 剔除其他此賬號在其它地方登錄,實現(xiàn)一個賬戶不能同時在多個地方登錄
? ? ? ? ? ? ? ? ? ? ? ? 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("");
// 設(shè)置用戶信息到Session作用域
req.getSession().setAttribute("admin", admin);
return new ResultUtil(0);
}
return new ResultUtil(502, "用戶名或密碼錯誤牍戚!");
}
return new ResultUtil(501, "驗證碼錯誤侮繁!");*/
}