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

📄 wlogin.java

📁 Java写的ERP系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * 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                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.www;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import java.sql.*;
import java.text.*;
import java.security.*;

import org.apache.ecs.*;
import org.apache.ecs.xhtml.*;

import org.compiere.util.*;

/**
 *  Web Login Page.
 *  <p>
 *  Page request:
 *  <pre>
 *  - Check database connection
 *  - LoginInfo from request?
 *      - Yes: DoLogin success ?
 *          - Yes: return (second) preferences page
 *          - No: return (first) user/password page
 *      - No: User Principal ?
 *          - Yes: DoLogin success ?
 *              - Yes: return (second) preferences page
 *              - No: return (first) user/password page
 *          - No: return (first) user/password page
 *  </pre>
 *
 *  @author Jorg Janke
 *  @version  $Id: WLogin.java,v 1.2 2003/01/20 05:42:07 jjanke Exp $
 */
public class WLogin extends HttpServlet
{
	/**
	 *	Initialize
	 *  @param config confif
	 *  @throws ServletException
	 */
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		if (!WEnv.initWeb(config))
			throw new ServletException("WLogin.init");
	}	//	init

	/**
	 * Get Servlet information
	 * @return servlet info
	 */
	public String getServletInfo()
	{
		return "Compiere Web Login";
	}	//	getServletInfo

	/**
	 *	Clean up
	 */
	public void destroy()
	{
		Log.trace(Log.l1_User, "WLogin.destroy");
		super.destroy();
	}	//	destroy


	/**
	 *	Process the HTTP Get request - forward to Post
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Log.trace(Log.l1_User, "WLogin.doGet");
		doPost (request, response);
	}	//	doGet


	/**
	 *	Process the HTTP Post request.
	 *  <pre>
	 *  - Optionally create Session
	 *  - Check database connection
	 *  - LoginInfo from request?
	 *      - Yes: DoLogin success ?
	 *          - Yes: return (second) preferences page
	 *          - No: return (first) user/password page
	 *      - No: User Principal ?
	 *          - Yes: DoLogin success ?
	 *              - Yes: return (second) preferences page
	 *              - No: return (first) user/password page
	 *          - No: return (first) user/password page
	 *  </pre>
	 *  @param request request
	 *  @param response response
	 *  @throws ServletException
	 *  @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Log.trace(Log.l1_User, "WLogin.doPost");
		//  Create New Session
		HttpSession sess = request.getSession(true);
		sess.setMaxInactiveInterval(WEnv.TIMEOUT);

		//  Get/set Context
		Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
		if (ctx == null)
			ctx = new Properties();
		sess.setAttribute(WEnv.SA_CONTEXT, ctx);

		//  Get Cookie
		Properties cProp = WUtil.getCookieProprties(request);
		//  Language
		checkLanguage (request, sess, ctx, cProp);
		//  Page
		WDoc doc = null;

		//  Check DB connection
		if (!DB.isConnected())
		{
			String msg = Msg.getMsg(ctx, "WLoginNoDB");
			doc = WDoc.create (msg);
			if (msg.equals("WLoginNoDB"))
				msg = "No Database Connection";
			doc.getBody().addElement(new h1(msg));
		}

		//  Login Info from request?
		else
		{
			//  Get Parameters:     UserName/Password
			String usr = request.getParameter(P_USERNAME);
			String pwd = request.getParameter(P_PASSWORD);
			//  Get Principle
			Principal userPr = request.getUserPrincipal();
			Log.trace(Log.l4_Data, "Principal=" + userPr + "; User=" + usr);

			//  Login info not from request and not pre-authorized
			if (userPr == null && (usr == null || pwd == null))
				doc = createFirstPage (cProp, request, "");
			//  Login info from request or authorized
			else
			{
				KeyNamePair[] roles = null;
				//  Pre-authorized
				if (userPr != null)
				{
					roles = DB.login(ctx, userPr);
					usr = userPr.getName();
				}
				else
					roles = DB.login(ctx, usr, pwd);
				//
				if (roles == null)
					doc = createFirstPage(cProp, request, Msg.getMsg(ctx, "UserPwdError"));
				else
					doc = createSecondPage(cProp, request, WUtil.convertToOption(roles, null), "");
				//  Can we save Cookie ?
				if (request.getParameter(P_STORE) == null)
				{
					cProp.clear();                          //  erase all
				}
				else    //  Save Cookie Parameter
				{
					cProp.setProperty(P_USERNAME, usr);
					cProp.setProperty(P_STORE, "Y");
					cProp.setProperty(P_PASSWORD, pwd);     //  For test only
				}
			}
		}

		//
		WUtil.createResponse (request, response, this, cProp, doc, true);
	}	//	doPost

	//  Variable Names
	public static final String      P_USERNAME      = "User";
	private static final String     P_PASSWORD      = "Password";
	protected static final String   P_LANGUAGE      = Env.LANG;
	private static final String     P_SUBMIT        = "Submit";
	//  WMenu picks it up
	protected static final String   P_ROLE          = "AD_Role_ID";
	protected static final String   P_CLIENT        = "AD_Client_ID";
	protected static final String   P_ORG           = "AD_Org_ID";
	protected static final String   P_DATE          = "Date";
	protected static final String   P_WAREHOUSE     = "M_Warehouse_ID";
	protected static final String   P_ERRORMSG      = "ErrorMessage";
	protected static final String   P_STORE         = "SaveCookie";

	/*************************************************************************/

	/**
	 *  Set Language from request or session in.
	 *  - Properties
	 *  - Cookie
	 *  - Session
	 *  @param request request
	 *  @param sess sess
	 *  @param ctx context
	 *  @param cProp properties
	 */
	private static void checkLanguage (HttpServletRequest request,
		HttpSession sess, Properties ctx, Properties cProp)
	{
		//  Get/set Parameter:      Language
		String AD_Language = request.getParameter(P_LANGUAGE);
		if (AD_Language == null)
		{
			//  Check Cookie
			AD_Language = cProp.getProperty(P_LANGUAGE);
			if (AD_Language == null)
			{

⌨️ 快捷键说明

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