Search in sources :

Example 6 with SysLog

use of com.tale.annotation.SysLog in project tale by otale.

the class AuthController method doLogin.

@SysLog("登录后台")
@PostRoute("login")
public RestResponse<?> doLogin(LoginParam loginParam, RouteContext context) {
    CommonValidator.valid(loginParam);
    Integer errorCount = cache.get(LOGIN_ERROR_COUNT);
    try {
        errorCount = null == errorCount ? 0 : errorCount;
        if (errorCount > 3) {
            return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
        }
        long count = new Users().where("username", loginParam.getUsername()).count();
        if (count < 1) {
            errorCount += 1;
            return RestResponse.fail("不存在该用户");
        }
        String pwd = EncryptKit.md5(loginParam.getUsername(), loginParam.getPassword());
        Users user = select().from(Users.class).where(Users::getUsername, loginParam.getUsername()).and(Users::getPassword, pwd).one();
        if (null == user) {
            errorCount += 1;
            return RestResponse.fail("用户名或密码错误");
        }
        context.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
        if (StringKit.isNotBlank(loginParam.getRememberMe())) {
            TaleUtils.setCookie(context, user.getUid());
        }
        Users temp = new Users();
        temp.setLogged(DateKit.nowUnix());
        temp.updateById(user.getUid());
        log.info("登录成功:{}", loginParam.getUsername());
        cache.set(LOGIN_ERROR_COUNT, 0);
        return RestResponse.ok();
    } catch (Exception e) {
        errorCount += 1;
        cache.set(LOGIN_ERROR_COUNT, errorCount, 10 * 60);
        String msg = "登录失败";
        if (e instanceof ValidatorException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : ValidatorException(com.blade.exception.ValidatorException) Users(com.tale.model.entity.Users) ValidatorException(com.blade.exception.ValidatorException) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog)

Aggregations

SysLog (com.tale.annotation.SysLog)6 PostRoute (com.blade.mvc.annotation.PostRoute)4 Users (com.tale.model.entity.Users)4 Environment (com.blade.Environment)1 ValidatorException (com.blade.exception.ValidatorException)1 JSON (com.blade.mvc.annotation.JSON)1 FileItem (com.blade.mvc.multipart.FileItem)1 Attach (com.tale.model.entity.Attach)1 Logs (com.tale.model.entity.Logs)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1