zoukankan      html  css  js  c++  java
  • 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请求也是不同的。

    这个功能点的特点是:在一个登陆的请求中,处理了多种账号的登录。

    缺点:代码有点繁琐且算法复杂度较高,效率低。

    博客网站 https://yamon.top 个人网站 https://yamon.top/resume GitHub网站 https://github.com/yamonc 欢迎前来访问
  • 相关阅读:
    JavaScript传递参数方法
    IScroll5不能滑到最底端的解决办法
    VS Less Compiler插件使用
    Sql查询某个字段是否包含小写字母
    试用VS2019正式版
    Ext.net MessageBox提示
    VS打开项目 提示Asp.net4.0未在web服务器上注册的解决方案
    罗技M185鼠标飘
    Ext.Net的一例Ext Undefined解决办法
    JGUI源码:DataTable固定列样式(20)
  • 原文地址:https://www.cnblogs.com/chenyameng/p/12493564.html
Copyright © 2011-2022 走看看