📄 wlogin.java
字号:
/******************************************************************************
* 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 Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.www;
import java.io.*;
import java.security.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.ecs.*;
import org.apache.ecs.xhtml.*;
import org.compiere.model.*;
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.21 2005/10/01 23:56:11 jjanke Exp $
*/
public class WLogin extends HttpServlet
{
/** Logger */
protected CLogger log = CLogger.getCLogger(getClass());
/**
* Initialize
* @param config confif
* @throws ServletException
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
if (!WebEnv.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.info("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.info("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.info("doPost");
// Create New Session
HttpSession sess = request.getSession(true);
sess.setMaxInactiveInterval(WebEnv.TIMEOUT);
// Get Cookie Properties
Properties cProp = WebUtil.getCookieProprties(request);
// Create Context
WebSessionCtx wsc = WebSessionCtx.get (request);
// Page
WebDoc doc = null;
// Check DB connection
if (!DB.isConnected())
{
String msg = Msg.getMsg(wsc.ctx, "WLoginNoDB");
if (msg.equals("WLoginNoDB"))
msg = "No Database Connection";
doc = WebDoc.createWindow (msg);
}
// Login Info from request?
else
{
// Get Parameters: UserName/Password
String usr = WebUtil.getParameter (request, P_USERNAME);
String pwd = WebUtil.getParameter (request, P_PASSWORD);
// Get Principle
Principal userPrincipal = request.getUserPrincipal();
log.info("Principal=" + userPrincipal + "; User=" + usr);
// Login info not from request and not pre-authorized
if (userPrincipal == null && (usr == null || pwd == null))
doc = createFirstPage (cProp, request, "");
// Login info from request or authorized
else
{
KeyNamePair[] roles = null;
Login login = new Login(wsc.ctx);
// Pre-authorized
if (userPrincipal != null)
{
roles = login.getRoles(userPrincipal);
usr = userPrincipal.getName();
}
else
roles = login.getRoles(usr, pwd);
//
if (roles == null)
doc = createFirstPage(cProp, request, Msg.getMsg(wsc.ctx, "UserPwdError"));
else
{
doc = createSecondPage(request, WebUtil.convertToOption(roles, null), "");
// Create Compiere Session - user id in ctx
MSession.get (wsc.ctx, request.getRemoteAddr(),
request.getRemoteHost(), sess.getId() );
}
// Can we save Cookie ?
if (WebUtil.getParameter (request, 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
}
}
}
WebUtil.createResponse (request, response, this, cProp, doc, true);
} // doPost
// Variable Names
public static final String P_USERNAME = "User";
private static final String P_PASSWORD = "Password";
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";
/**************************************************************************
* First Login Page
* @param cProp Login Cookie information for defaults
* @param request request
* @param errorMessage error message
* @return WDoc page
*/
private WebDoc createFirstPage(Properties cProp, HttpServletRequest request,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -