shiro多账号登录(用户名,手机号,邮箱)
shiro多賬號登錄(用戶名,手機(jī)號,郵箱)
最近的一個(gè)需求,可以使用用戶名和手機(jī)號和郵箱進(jìn)行登錄。百度得到的那些都過于復(fù)雜。
分析:其實(shí)本質(zhì)代碼的核心就是在于驗(yàn)證,驗(yàn)證你輸入的東西到底能不能通過,你傳進(jìn)去的賬號和密碼需要進(jìn)行驗(yàn)證后才能登錄,其實(shí)一種最簡單的方法就是在驗(yàn)證的時(shí)候,對username替換成為郵箱和手機(jī)號,下面的代碼基本不用更改,就是controller里面的登錄后處理代碼,不需要更改,需要更改的代碼是在驗(yàn)證的時(shí)候,在自定義的Realm里面的doGetAuthenticationInfo方法更改。
Subject currentUser = SecurityUtils.getSubject();
Session session = SecurityUtils.getSubject().getSession();
User user = (User) session.getAttribute("user");
//把用戶名和密碼封裝為 UsernamePasswordToken 對象
AuthenticationToken token = new UsernamePasswordToken(username, password);
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
try {
//執(zhí)行登錄
currentUser.login(token);
//開啟rememberMe功能
usernamePasswordToken.setRememberMe(true);
log.info("[{}] login success", username);
returnMap.put("status", "1");
returnMap.put("message", "登陸成功");
if (currentUser.hasRole("userComment")) {
//普通用戶登錄后臺
log.info("[{}] 普通用戶成功登錄后臺", currentUser);
return "redirect:/";
}
if (currentUser.hasRole("administrator")) {
log.info("[{}] 管理員成功登錄后臺", currentUser);
//管理員登錄后臺
return "admin/index";
}
return "redirect:/";
//若沒有指定的賬戶或者密碼不匹配
} catch (IncorrectCredentialsException | UnknownAccountException ice) {
returnMap.put("status", "0");
returnMap.put("message", "用戶名或密碼錯(cuò)誤");
log.info("[{}] username or password is wrong", username);
attributes.addFlashAttribute("message", "用戶名或密碼錯(cuò)誤");
return REDIRECT_TO_LOGIN;
} catch (ExcessiveAttemptsException e) {
returnMap.put("status", "0");
returnMap.put("message", "登錄失敗5次,賬號鎖定10分鐘");
log.info("[{}] is locked", username);
attributes.addFlashAttribute("message", "登錄失敗5次,賬號鎖定10分鐘");
return REDIRECT_TO_LOGIN;
} catch (Exception e) {
returnMap.put("status", "0");
returnMap.put("message", "服務(wù)不可用");
log.error("[{}] login failed", username, e);
attributes.addFlashAttribute("message", "登錄失敗5次,賬號鎖定10分鐘");
return REDIRECT_TO_LOGIN;
}
大致的代碼就是這樣。主要代碼:
/**
* 認(rèn)證
* 獲取身份驗(yàn)證相關(guān)信息
* */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//先使用用戶名
String username = authenticationToken.getPrincipal().toString();
User user = userService.getUserByUserName(username);
if (user == null) {
//通過郵箱
User byUserEmail = userService.findByUserEmail(username);
if (byUserEmail == null){
User byUserTel = userService.findByUserTel(username);
if (byUserTel == null){
throw new UnknownAccountException("賬號不存在。");
}else{
//組裝 SimpleAuthenticationInfo 信息
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(byUserTel, byUserTel.getPassword(), ByteSource.Util.bytes(byUserTel.getSalt()), getName());
SecurityUtils.getSubject().getSession().setAttribute("user",byUserTel);
return authenticationInfo;
}
}else{
//組裝 SimpleAuthenticationInfo 信息
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(byUserEmail, byUserEmail.getPassword(), ByteSource.Util.bytes(byUserEmail.getSalt()), getName());
SecurityUtils.getSubject().getSession().setAttribute("user",byUserEmail);
return authenticationInfo;
}
}
//組裝 SimpleAuthenticationInfo 信息
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName());
SecurityUtils.getSubject().getSession().setAttribute("user",user);
return authenticationInfo;
}
總的思路就是,先通過用戶名進(jìn)行查詢,看用戶是否使用用戶名登錄,如果查不出來就證明不是使用用戶名,然后進(jìn)入下一個(gè)if判斷,根據(jù)郵箱查詢用戶,如果用戶存在,則組裝信息,如果不存在,再查找手機(jī)號的。
大概的思路就是這樣,網(wǎng)上的都是再重新定義一個(gè)自定義的Realm,然后在post請求也是不同的。
這個(gè)功能點(diǎn)的特點(diǎn)是:在一個(gè)登陸的請求中,處理了多種賬號的登錄。
缺點(diǎn):代碼有點(diǎn)繁瑣且算法復(fù)雜度較高,效率低。
博客網(wǎng)站 https://yamon.top
個(gè)人網(wǎng)站 https://yamon.top/resume
GitHub網(wǎng)站 https://github.com/yamonc
歡迎前來訪問
總結(jié)
以上是生活随笔為你收集整理的shiro多账号登录(用户名,手机号,邮箱)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和历与西历之间的转换
- 下一篇: 各种电子面单-Api接口(顺丰、快递鸟、