📄 login.java
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的 */package biz.tbuy.user.bean;import biz.tbuy.common.Constants;import biz.tbuy.common.Encoder;import biz.tbuy.common.Utils;import biz.tbuy.user.UserAction;import biz.tbuy.user.UserModel;import biz.tbuy.common.Visitor;import java.io.UnsupportedEncodingException;import java.security.NoSuchAlgorithmException;import javax.faces.application.Application;import javax.faces.component.UIViewRoot;import javax.faces.context.FacesContext;import javax.servlet.http.HttpSession;/** * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class Login extends BaseBean{ private boolean _login; // 系统是否开启了一般用户的登录 private String _id; // 用户的登录ID private String _password; // 用户的登录密码 private boolean _hidden; // 是否以隐身方式登录 private String _validateCode;// 验证码,当系统启用了验证码功能时使用 public Login() { _login = getComApplication().isLogin(); if (!_login) { Utils.addWarnMessage(getBundle(), "当前系统关闭了“用户登录”,你可能暂时无法正常登录系统"); } } public boolean isLogin() { return _login; } /** * @param id */ public void setId(String id) { _id = id; } public String getId() { return _id; } public void setPassword(String password) { _password = password; } public String getPassword() { return _password; } public void setHidden(boolean hidden) { _hidden = hidden; } public boolean isHidden() { return _hidden; } public void setValidateCode(String validateCode) { _validateCode = validateCode; } public String getValidateCode() { return _validateCode; } /* method *****************************************************************/ /** * @return boolean */ public String loginCheck() throws NoSuchAlgorithmException, UnsupportedEncodingException { // 1 检相验证码,如果需要注册验证 if (getComApplication().isValidateLogin()) { HttpSession session = (HttpSession)getFacesContext(). getExternalContext().getSession(false); if (_validateCode == null || !_validateCode.toUpperCase().equals( session.getAttribute("validateCode"))) { Utils.addErrorMessage(getBundle(), "login_validateCodeError"); return Constants.OUT_FAILURE; } } // 2 包含密码的MD5加密,获取user,如果成功则说明用户id及密码正确 UserModel user = null; user = UserAction.getUser(_id, Encoder.encodeByMd5(_password)); if (user == null) { Utils.addErrorMessage(getBundle(), "login_failure"); return Constants.OUT_FAILURE; } // 3 检查用户帐号是否已经被封停 if (user.getBeRevoke()) { Utils.addErrorMessage(getBundle(), "对不起,该帐号已经被停止使用"); return Constants.OUT_FAILURE; } // 4 检查用户帐号是否已经激活 if (!user.getEnabled()) { // 如果系统开启了email认证,则发出警告,并重新发送确认邮件 if (getComApplication().isEmailRegister()) { Utils.addErrorMessage(getBundle(), "login_idNoEnable"); redirect(); // 转到重发激活邮件页面 } else { // 否则激活,同时设置帐号为活的 if (UserAction.enableUser(user.getId())) { user.setEnabled(true); } } } // 5 检查系统是否允许登录,如果不允许登录,同时用户又非管理员... if (!_login && !getComApplication().isAdmin(user.getId())) { Utils.addErrorMessage(getBundle(), "对不起,系统关闭了“用户登录”," + "你暂时无法登录系统"); return Constants.OUT_FAILURE; } // 6 登录并更新 Visitor if (user != null && user.getEnabled()) { login(user, _hidden); Utils.addInfoMessage(getBundle(), "login_success"); return Constants.OUT_SUCCESS; } return Constants.OUT_FAILURE; } /** * 跳转到另一个页面 */ private void redirect() { FacesContext fc = getFacesContext(); Application app = getApplication(); UIViewRoot view = app.getViewHandler(). createView(fc, "/common/user/resendEmail.jsp"); fc.setViewRoot(view); fc.renderResponse(); } /** * 设置用户登录 * 创建Bean Visitor 并绑定到 sessionScope.Visitor * @param user 需要被设置为登录状态的用户 * @param hidden 是否以隐身方式登录系统 */ public void login(UserModel user, boolean hidden) { getComApplication().getVisitorsOper().login(user, hidden); } /** * 用于退出登录,该方法会首先清除当前用户的在线情况,即从在线列表中将该用户移除 * 再移除session信息:<br> * 1.移除在线信息<br> * 2.清除session<br> * @return String "success/failure"; */ public String logout() { Visitor visitor = getVisitor(); getComApplication().getVisitorsOper().logout(visitor); return Constants.OUT_SUCCESS; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -