⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginevents.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * $Id: LoginEvents.java 6695 2006-02-06 22:35:58Z jonesde $ * *  Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * *  Permission is hereby granted, free of charge, to any person obtaining a *  copy of this software and associated documentation files (the "Software"), *  to deal in the Software without restriction, including without limitation *  the rights to use, copy, modify, merge, publish, distribute, sublicense, *  and/or sell copies of the Software, and to permit persons to whom the *  Software is furnished to do so, subject to the following conditions: * *  The above copyright notice and this permission notice shall be included *  in all copies or substantial portions of the Software. * *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *  THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.securityext.login;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javolution.util.FastList;import javolution.util.FastMap;import org.ofbiz.base.component.ComponentConfig;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilFormatOut;import org.ofbiz.base.util.UtilHttp;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.party.contact.ContactHelper;import org.ofbiz.product.product.ProductEvents;import org.ofbiz.product.store.ProductStoreWorker;import org.ofbiz.security.Security;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ModelService;import org.ofbiz.webapp.control.LoginWorker;import org.ofbiz.webapp.control.RequestHandler;import org.ofbiz.webapp.stats.VisitHandler;/** * LoginEvents - Events for UserLogin and Security handling. * * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author     <a href="">Dustin Caldwell</a> * @author     <a href="mailto:therrick@yahoo.com">Tom Herrick</a> * @version    $Rev: 6695 $ * @since      2.0 */public class LoginEvents {    public static final String module = LoginEvents.class.getName();    public static final String resource = "SecurityextUiLabels";    /**     * Save USERNAME and PASSWORD for use by auth pages even if we start in non-auth pages.     *     * @param request The HTTP request object for the current JSP or Servlet request.     * @param response The HTTP response object for the current JSP or Servlet request.     * @return String     */    public static String saveEntryParams(HttpServletRequest request, HttpServletResponse response) {        GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");        HttpSession session = request.getSession();        // save entry login parameters if we don't have a valid login object        if (userLogin == null) {            String username = request.getParameter("USERNAME");            String password = request.getParameter("PASSWORD");            if ((username != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "username.lowercase")))) {                username = username.toLowerCase();            }            if ((password != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "password.lowercase")))) {                password = password.toLowerCase();            }            // save parameters into the session - so they can be used later, if needed            if (username != null) session.setAttribute("USERNAME", username);            if (password != null) session.setAttribute("PASSWORD", password);        } else {            // if the login object is valid, remove attributes            session.removeAttribute("USERNAME");            session.removeAttribute("PASSWORD");        }        return "success";    }    /**     * An HTTP WebEvent handler that checks to see is a userLogin is logged in.     * If not, the user is forwarded to the login page.     *     * @param request The HTTP request object for the current JSP or Servlet request.     * @param response The HTTP response object for the current JSP or Servlet request.     * @return String     */    public static String checkLogin(HttpServletRequest request, HttpServletResponse response) {        GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");        HttpSession session = request.getSession();        // anonymous shoppers are not logged in        if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) {            userLogin = null;        }        // user is logged in; check to see if they have globally logged out if not        // check if they have permission for this login attempt; if not log them out        if (userLogin != null) {            if (!hasBasePermission(userLogin, request) || isFlaggedLoggedOut(userLogin)) {                Debug.logInfo("User does not have permission or is flagged as logged out", module);                doBasicLogout(userLogin, request);                userLogin = null;                // have to reget this because the old session object will be invalid                session = request.getSession();            }        }        String username = null;        String password = null;        if (userLogin == null) {            // check parameters            if (username == null) username = request.getParameter("USERNAME");            if (password == null) password = request.getParameter("PASSWORD");            // check session attributes            if (username == null) username = (String) session.getAttribute("USERNAME");            if (password == null) password = (String) session.getAttribute("PASSWORD");            if ((username != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "username.lowercase")))) {                username = username.toLowerCase();            }            if ((password != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "password.lowercase")))) {                password = password.toLowerCase();            }            // in this condition log them in if not already; if not logged in or can't log in, save parameters and return error            if ((username == null) || (password == null) || ("error".equals(login(request, response)))) {                Map reqParams = UtilHttp.getParameterMap(request);                String queryString = UtilHttp.urlEncodeArgs(reqParams);                Debug.logInfo("reqParams Map: " + reqParams, module);                Debug.logInfo("queryString: " + queryString, module);                session.setAttribute("_PREVIOUS_REQUEST_", request.getPathInfo());                if (queryString != null && queryString.length() > 0) {                    session.setAttribute("_PREVIOUS_PARAMS_", queryString);                }                if (Debug.infoOn()) Debug.logInfo("checkLogin: queryString=" + queryString, module);                if (Debug.infoOn()) Debug.logInfo("checkLogin: PathInfo=" + request.getPathInfo(), module);                return "error";            }        }        return "success";    }    /**     * An HTTP WebEvent handler that logs in a userLogin. This should run before the security check.     *     * @param request The HTTP request object for the current JSP or Servlet request.     * @param response The HTTP response object for the current JSP or Servlet request.     * @return Return a boolean which specifies whether or not the calling Servlet or     *         JSP should generate its own content. This allows an event to override the default content.     */    public static String login(HttpServletRequest request, HttpServletResponse response) {        HttpSession session = request.getSession();        String username = request.getParameter("USERNAME");        String password = request.getParameter("PASSWORD");        if (username == null) username = (String) session.getAttribute("USERNAME");        if (password == null) password = (String) session.getAttribute("PASSWORD");                // allow a username and/or password in a request attribute to override the request parameter or the session attribute; this way a preprocessor can play with these a bit...        if (UtilValidate.isNotEmpty((String) request.getAttribute("USERNAME"))) {            username = (String) request.getAttribute("USERNAME");        }        if (UtilValidate.isNotEmpty((String) request.getAttribute("PASSWORD"))) {            password = (String) request.getAttribute("PASSWORD");        }        List unpwErrMsgList = FastList.newInstance();        if (UtilValidate.isEmpty(username)) {            unpwErrMsgList.add(UtilProperties.getMessage(resource, "loginevents.username_was_empty_reenter", UtilHttp.getLocale(request)));        }        if (UtilValidate.isEmpty(password)) {            unpwErrMsgList.add(UtilProperties.getMessage(resource, "loginevents.password_was_empty_reenter", UtilHttp.getLocale(request)));        }        if (!unpwErrMsgList.isEmpty()) {            request.setAttribute("_ERROR_MESSAGE_LIST_", unpwErrMsgList);            return "error";        }                if ((username != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "username.lowercase")))) {            username = username.toLowerCase();        }        if ((password != null) && ("true".equalsIgnoreCase(UtilProperties.getPropertyValue("security.properties", "password.lowercase")))) {            password = password.toLowerCase();        }        // get the visit id to pass to the userLogin for history        String visitId = VisitHandler.getVisitId(session);        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");        Map result = null;        try {            result = dispatcher.runSync("userLogin", UtilMisc.toMap("login.username", username, "login.password", password, "visitId", visitId, "locale", UtilHttp.getLocale(request)));        } catch (GenericServiceException e) {            Debug.logError(e, "Error calling userLogin service", module);            Map messageMap = UtilMisc.toMap("errorMessage", e.getMessage());            String errMsg = UtilProperties.getMessage(resource, "loginevents.following_error_occurred_during_login", messageMap, UtilHttp.getLocale(request));            request.setAttribute("_ERROR_MESSAGE_", errMsg);            return "error";        }        if (ModelService.RESPOND_SUCCESS.equals(result.get(ModelService.RESPONSE_MESSAGE))) {            GenericValue userLogin = (GenericValue) result.get("userLogin");            Map userLoginSession = (Map) result.get("userLoginSession");            if (userLogin != null && hasBasePermission(userLogin, request)) {                doBasicLogin(userLogin, request);            } else {                String errMsg = UtilProperties.getMessage(resource, "loginevents.unable_to_login_this_application", UtilHttp.getLocale(request));                request.setAttribute("_ERROR_MESSAGE_", errMsg);                return "error";            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -