shiro多賬號登錄(用戶名,手機號,郵箱)


shiro多賬號登錄(用戶名,手機號,郵箱)

最近的一個需求,可以使用用戶名和手機號和郵箱進行登錄。百度得到的那些都過於復雜。

分析:其實本質代碼的核心就是在於驗證,驗證你輸入的東西到底能不能通過,你傳進去的賬號和密碼需要進行驗證后才能登錄,其實一種最簡單的方法就是在驗證的時候,對username替換成為郵箱和手機號,下面的代碼基本不用更改,就是controller里面的登錄后處理代碼,不需要更改,需要更改的代碼是在驗證的時候,在自定義的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 {
    //執行登錄
    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", "用戶名或密碼錯誤");

    log.info("[{}] username or password is wrong", username);
    attributes.addFlashAttribute("message", "用戶名或密碼錯誤");
    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", "服務不可用");
    log.error("[{}] login failed", username, e);
    attributes.addFlashAttribute("message", "登錄失敗5次,賬號鎖定10分鍾");
    return REDIRECT_TO_LOGIN;
}

大致的代碼就是這樣。主要代碼:

/**
 * 認證
 * 獲取身份驗證相關信息
 * */
@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;
}

總的思路就是,先通過用戶名進行查詢,看用戶是否使用用戶名登錄,如果查不出來就證明不是使用用戶名,然后進入下一個if判斷,根據郵箱查詢用戶,如果用戶存在,則組裝信息,如果不存在,再查找手機號的。

大概的思路就是這樣,網上的都是再重新定義一個自定義的Realm,然后在post請求也是不同的。

這個功能點的特點是:在一個登陸的請求中,處理了多種賬號的登錄。

缺點:代碼有點繁瑣且算法復雜度較高,效率低。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM