Java Code Examples for com.blade.mvc.http.Session#attribute()

The following examples show how to use com.blade.mvc.http.Session#attribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaleUtils.java    From tale with MIT License 5 votes vote down vote up
/**
 * 返回当前登录用户
 *
 * @return
 */
public static Users getLoginUser() {
    Session session = com.blade.mvc.WebContext.request().session();
    if (null == session) {
        return null;
    }
    Users user = session.attribute(TaleConst.LOGIN_SESSION_KEY);
    return user;
}
 
Example 2
Source File: AttributesExampleController.java    From tutorials with MIT License 5 votes vote down vote up
@GetRoute("/session-attribute-example")
public void getSessionAttribute(Request request, Response response) {
    Session session = request.session();
    session.attribute("session-val", SESSION_VALUE);
    String sessionVal = session.attribute("session-val");
    response.text(sessionVal);
}
 
Example 3
Source File: AuthController.java    From tale with MIT License 4 votes vote down vote up
@Route(value = "login", method = HttpMethod.POST)
@JSON
public RestResponse doLogin(LoginParam loginParam, Request request,
                            Session session, Response response) {

    Integer error_count = cache.get("login_error_count");
    try {
        error_count = null == error_count ? 0 : error_count;
        if (null != error_count && error_count > 3) {
            return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
        }

        long count = new Users().where("username", loginParam.getUsername()).count();
        if (count < 1) {
            return RestResponse.fail("不存在该用户");
        }
        String pwd = EncryptKit.md5(loginParam.getUsername(), loginParam.getPassword());

        Users user = new Users().where("username", loginParam.getUsername()).and("password", pwd).find();
        if (null == user) {
            return RestResponse.fail("用户名或密码错误");
        }
        session.attribute(TaleConst.LOGIN_SESSION_KEY, user);
        if (StringKit.isNotBlank(loginParam.getRemeberMe())) {
            TaleUtils.setCookie(response, user.getUid());
        }

        Users temp = new Users();
        temp.setLogged(DateKit.nowUnix());
        temp.update(user.getUid());
        log.info("登录成功:{}", loginParam.getUsername());
        cache.set("login_error_count", 0);

        new Logs(LogActions.LOGIN, loginParam.getUsername(), request.address(), user.getUid()).save();
    } catch (Exception e) {
        error_count += 1;
        cache.set("login_error_count", error_count, 10 * 60);
        String msg = "登录失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}