session的登录与注销

	protected void clearSession() {

		SessionMap sessionMap = (SessionMap) ActionContext.getContext()
				.getSession();
		try {
			sessionMap.clear();
		} catch (Exception e) {
			//
		}

		HttpSession httpSession = this.getServletRequest().getSession();
		try {
			httpSession.invalidate();
		} catch (Exception e) {
			//
		}
	}

 

 

doLogin

 

public String doLogin() {
		clearSession();
		String loginUsername = this.getLoginUsername();
		String loginPassword = this.getLoginPassword();
		if (StringUtils.isBlank(loginUsername)) {
			setMessage("用户名必填项");
			return INPUT;
		} else if (StringUtils.isBlank(loginPassword)) {
			setMessage("密码必填项");
			return INPUT;
		}

		try {
			setSessionAttribute(SESSION_ATTRIBUTE_KEY_USER,user);
					setCookiesAttribute(COOKIES_ATTRIBUTE_KEY_EMPLOYEE_NO, loginUsername);
			return "home";
		} catch (WebApplicationRuntimeException e) {
			this.setMessage(e.getMessage());
		}

		return INPUT;
	}

 

 

loginout

 

	public String execute() {
		try {
			clearSession();
			ReserverCookie reserverCookie = new ReserverCookie();
			reserverCookie.removeCookie(this.getServletRequest(), getServletResponse());
		} catch (Exception e) {

		}
		return "login";
	}

 

 

	public void removeCookie(HttpServletRequest request,
			HttpServletResponse response) {
		Cookie cookie = getCookie(request);
		if (cookie != null) {
			cookie = new Cookie(COOKIE_NAME, "");
			cookie.setMaxAge(0);
			cookie.setValue("");
			cookie.setPath("/");
			cookie.setDomain(CSAIR_COM);
			response.addCookie(cookie);
		}
	}

 

好的,关于用户登录注销,可以通过 session 实现。在用户登录时,将用户的信息存储在 session 中;在用户注销时,清除 session 中的用户信息即可。 以下是一个简单的示例代码: ```python from flask import Flask, session, redirect, url_for, request app = Flask(__name__) app.secret_key = 'your_secret_key' # 用户登录 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': # 验证用户信息 username = request.form['username'] password = request.form['password'] if username == 'admin' and password == 'password': # 用户信息存储在 sessionsession['username'] = username return redirect(url_for('index')) else: return 'Invalid username/password' else: return ''' <form method="post"> <p><input type="text" name="username"> <p><input type="password" name="password"> <p><input type="submit" value="Login"> </form> ''' # 用户注销 @app.route('/logout') def logout(): # 清除 session 中的用户信息 session.pop('username', None) return redirect(url_for('index')) # 首页 @app.route('/') def index(): if 'username' in session: return 'Logged in as {}'.format(session['username']) else: return 'You are not logged in' if __name__ == '__main__': app.run() ``` 在上面的示例中,首先需要设置应用程序的 secret_key,这是为了安全起见。然后定义了一个登录页面,用户在该页面输入用户名和密码,如果用户名和密码正确,则将用户名存储在 session 中,并重定向到首页。在首页中,如果 session 中存在用户名,则表示用户已登录,否则表示用户未登录。最后,定义了一个注销页面,当用户点击注销时,清除 session 中的用户名,并重定向到首页。 需要注意的是,session 中存储的数据量应该尽量少,以免影响性能。在本例中,只存储了用户名,如果需要存储更多的信息,可以考虑将其存储在数据库中,然后在 session 中存储一个标识符,用于查询数据库中的用户信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值