logonaction.java
来自「开源项目CRM之OpenCustomer」· Java 代码 · 共 226 行
JAVA
226 行
/*******************************************************************************
* ***** BEGIN LICENSE BLOCK Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is the OpenCustomer CRM.
*
* The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
* Software-Ingenieurb黵o). Portions created by the Initial Developer are
* Copyright (C) 2005 the Initial Developer. All Rights Reserved.
*
* Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
*
* ***** END LICENSE BLOCK *****
*/
package org.opencustomer.application.web.module.common;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.opencustomer.application.auth.Authenticator;
import org.opencustomer.application.db.dao.system.UserDAO;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.web.Globals;
import org.opencustomer.application.web.struts.Action;
import org.opencustomer.application.web.util.Menu;
import org.opencustomer.util.SignatureUtility;
import org.opencustomer.web.util.SessionMonitor;
/**
*
* @author thbader
*/
public final class LogonAction extends Action<LogonForm>
{
private static Logger log = Logger.getLogger(LogonAction.class);
private static final int LOGIN_NOK = 0;
private static final int LOGIN_OK = 1;
/**
* Method which have to be overwritten to handle the request.
*
* @param servlet The ActionServlet instance owning this Action
* @param mapping The ActionMapping used to select this instance
* @param actionForm The optional ActionForm bean for this request (if any)
* @param request The servlet request we are processing
* @param response The servlet response we are processing
* @param log the log to save messages
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet exception occurs
*/
public ActionForward execute(ActionMapping mapping, LogonForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
ActionMessages errors = new ActionMessages();
int forward = LOGIN_NOK;
if (request.getSession().getAttribute(Globals.AUTHENTICATOR_KEY) != null)
{
if (log.isInfoEnabled())
log.info("user session is already active ... login is not necessary");
forward = LOGIN_OK;
}
else if (form.getDoLogin() != null)
{
// Suche den Benutzer f黵 die Anmeldung
UserVO user = null;
try
{
user = new UserDAO().getByUserName(form.getLogin());
if (user != null)
Hibernate.initialize(user.getPerson());
}
catch (HibernateException e)
{
log.error("problems finding user user", e);
}
// 躡erpr黤e die Informationen
if (user == null)
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("common.login.error.invalidUserNamePassword"));
else if (user.isLocked())
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("common.login.error.userLocked"));
else if (user.getFailedLogins() != null && user.getFailedLogins() >= 3) // TODO:
// read
// from
// property
// file
{
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(user.getLastFailedLogin());
cal.add(Calendar.MINUTE, 30); // TODO: read from property file
if (new Date().after(cal.getTime()))
forward = LOGIN_OK;
else
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("common.login.error.maxFailedLoginsReached", String.valueOf(3), String.valueOf(30)));
}
else if (!SignatureUtility.getInstance().isSignatureValid(user.getPassword(), form.getPassword()))
{
int failedLogins = 0;
if(user.getFailedLogins() != null)
failedLogins = user.getFailedLogins();
user.setFailedLogins(failedLogins + 1);
user.setLastFailedLogin(new Date());
errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("common.login.error.invalidUserNamePassword"));
}
else
forward = LOGIN_OK;
if (forward == LOGIN_OK)
{
user.setLastLogin(new Date());
user.setFailedLogins(0);
request.getSession().setAttribute(Globals.USER_KEY, user);
request.getSession().setAttribute(Globals.AUTHENTICATOR_KEY, new Authenticator(user));
// add monitor to view the active users
if (request.getSession().getAttribute(Globals.SESSION_MONITOR_KEY) == null)
request.getSession().setAttribute(Globals.SESSION_MONITOR_KEY, new SessionMonitor(System.currentTimeMillis()));
initMenu(request, mapping.getModuleConfig());
// TODO: set locale with user
request.getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY, Locale.GERMAN);
forward = LOGIN_OK;
}
// save changes
if (user != null)
{
try
{
new UserDAO().update(user);
}
catch (HibernateException e)
{
log.error("could not save user", e);
}
}
}
if (!errors.isEmpty())
saveErrors(request, errors);
if (forward == LOGIN_OK)
{
Menu menu = (Menu) request.getSession().getAttribute(Globals.MENU_KEY);
String path = menu.getFirstPath();
if (path == null)
return mapping.findForward("error");
else
return new ActionForward(getActionURL(request, path));
}
else
return mapping.getInputForward();
}
private String getActionURL(HttpServletRequest request, String path)
{
String pattern = (String) request.getSession().getServletContext().getAttribute(org.apache.struts.Globals.SERVLET_KEY);
StringBuilder url = new StringBuilder();
if (pattern.startsWith("*."))
{
url.append(path);
url.append(pattern.substring(1));
}
else if (pattern.endsWith("/*"))
{
url.append(pattern.substring(0, pattern.length() - 2));
url.append(path);
}
else if (pattern.equals("/"))
{
url.append(path);
}
return url.toString();
}
private void initMenu(HttpServletRequest request, ModuleConfig moduleConfig)
{
if (log.isDebugEnabled())
log.debug("initialize menu (load rights from config) ");
Authenticator auth = (Authenticator) request.getSession().getAttribute(Globals.AUTHENTICATOR_KEY);
Menu menu = new Menu(moduleConfig);
menu.customize(auth);
request.getSession().setAttribute(Globals.MENU_KEY, menu);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?