zoukankan      html  css  js  c++  java
  • shiro 登录

    @Controller
    public class LoginController {

    @RequestMapping(value="/login")
    public @ResponseBody
    ResultInfo login(@Valid User user, BindingResult bindingResult,RedirectAttributes redirectAttributes){
    ResultInfo info = new ResultInfo();

    if(bindingResult.hasErrors()){
    info.setCode(ResultStatus.ERROR.getCode());
    return info;
    }

    UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(), user.getPwd());
    //获取当前的Subject
    Subject currentUser = SecurityUtils.getSubject();
    try {
    //在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查
    //每个Realm都能在必要时对提交的AuthenticationTokens作出反应
    //所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法
    currentUser.login(token);
    }catch(UnknownAccountException uae){
    // logger.info("对用户[" + username + "]进行登录验证..验证未通过,未知账户");
    info.setMsg( "未知账户");
    // redirectAttributes.addFlashAttribute("message", "未知账户");
    }catch(IncorrectCredentialsException ice){
    // logger.info("对用户[" + username + "]进行登录验证..验证未通过,错误的凭证");
    info.setMsg( "密码不正确");
    // redirectAttributes.addFlashAttribute("message", "密码不正确");
    }catch(LockedAccountException lae){
    // logger.info("对用户[" + username + "]进行登录验证..验证未通过,账户已锁定");
    info.setMsg( "账户已锁定");
    // redirectAttributes.addFlashAttribute("message", "账户已锁定");
    }catch(ExcessiveAttemptsException eae){
    //logger.info("对用户[" + username + "]进行登录验证..验证未通过,错误次数过多");
    info.setMsg( "用户名或密码错误次数过多");
    // redirectAttributes.addFlashAttribute("message", "用户名或密码错误次数过多");
    }catch(AuthenticationException ae){
    //通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景
    //logger.info("对用户[" + username + "]进行登录验证..验证未通过,堆栈轨迹如下");
    info.setMsg( "用户名或密码不正确");
    ae.printStackTrace();
    // redirectAttributes.addFlashAttribute("message", "用户名或密码不正确");
    }
    //验证是否登录成功
    if(currentUser.isAuthenticated()){
    info.setCode(ResultStatus.SUCCESS.getCode());
    return info;
    }else{
    token.clear();
    info.setCode(ResultStatus.ERROR.getCode());
    return info;
    }
    }

    @RequestMapping(value="/logout",method=RequestMethod.GET)
    public String logout(RedirectAttributes redirectAttributes ){
    //使用权限管理工具进行用户的退出,跳出登录,给出提示信息
    SecurityUtils.getSubject().logout();
    redirectAttributes.addFlashAttribute("message", "您已安全退出");
    return "redirect:/login";
    }
    }
  • 相关阅读:
    2020年Android面试题含答案
    flutter系列(一)----- 开发环境搭建
    Android应用安全防护和逆向分析 ——apk混淆成其他语言代码
    Android应用安全防护和逆向分析 ——apk反编译
    Android中 TextView 加载 混合字符 自动换行解决方案
    H5跳转app本地的规则定义
    Android ListView 九大重要属性详细分析
    ListView和ScrollView滑动到顶部
    简要的汇总Android
    关于ViewPager+Fragment中Fragment不销毁/生命周期
  • 原文地址:https://www.cnblogs.com/newlangwen/p/9024101.html
Copyright © 2011-2022 走看看