示例#1
0
  @Clear(UserInterceptor.class)
  @ActionKey(Consts.ROUTER_USER_LOGIN) // 固定登陆的url
  public void login() {
    keepPara();

    String username = getPara("username");
    String password = getPara("password");

    if (username == null || password == null) {
      render("user_login.html");
      return;
    }

    long errorTimes = CookieUtils.getLong(this, "_login_errors", 0);
    if (errorTimes >= 3) {
      if (!validateCaptcha("_login_captcha")) { // 验证码没验证成功!
        if (isAjaxRequest()) {
          renderAjaxResultForError("没有该用户");
        } else {
          redirect(Consts.ROUTER_USER_LOGIN);
        }
        return;
      }
    }

    User user = User.DAO.findUserByUsername(username);
    if (null == user) {
      if (isAjaxRequest()) {
        renderAjaxResultForError("没有该用户");
      } else {
        setAttr("errorMsg", "没有该用户");
        render("user_login.html");
      }
      CookieUtils.put(this, "_login_errors", errorTimes + 1);
      return;
    }

    if (EncryptUtils.verlifyUser(user, password)) {
      MessageKit.sendMessage(Actions.USER_LOGINED, user);
      CookieUtils.put(this, Consts.COOKIE_LOGINED_USER, user.getId());
      if (this.isAjaxRequest()) {
        renderAjaxResultForSuccess("登陆成功");
      } else {
        String gotoUrl = getPara("goto");
        if (StringUtils.isNotEmpty(gotoUrl)) {
          gotoUrl = StringUtils.urlDecode(gotoUrl);
          gotoUrl = StringUtils.urlRedirect(gotoUrl);
          redirect(gotoUrl);
        } else {
          redirect(Consts.ROUTER_USER_CENTER);
        }
      }
    } else {
      if (isAjaxRequest()) {
        renderAjaxResultForError("密码错误");
      } else {
        setAttr("errorMsg", "密码错误");
        render("user_login.html");
      }
      CookieUtils.put(this, "_login_errors", errorTimes + 1);
    }
  }