日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

shiro多账号登录(用户名,手机号,邮箱)

發(fā)布時(shí)間:2023/12/13 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 shiro多账号登录(用户名,手机号,邮箱) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。