From b636776abb57a3169ca05e02765b6ea974adb8eb Mon Sep 17 00:00:00 2001 From: Deepak Pansheriya Date: Mon, 15 Oct 2012 17:19:30 +0530 Subject: [PATCH] IDEMPIERE-389 - Fixing AD_Session to log actual IP in case of server is behind Load balancer. --- .../src/org/compiere/model/I_AD_Session.java | 9 +++++++++ .../src/org/compiere/model/MSession.java | 2 ++ .../src/org/compiere/model/X_AD_Session.java | 14 ++++++++++++++ org.adempiere.base/src/org/compiere/util/Ini.java | 7 +++++-- .../src/org/compiere/util/WebLogin.java | 11 +++++++---- .../src/org/adempiere/webui/AdempiereWebUI.java | 7 ++++--- 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/I_AD_Session.java b/org.adempiere.base/src/org/compiere/model/I_AD_Session.java index 9fb27032be..ef58feb340 100644 --- a/org.adempiere.base/src/org/compiere/model/I_AD_Session.java +++ b/org.adempiere.base/src/org/compiere/model/I_AD_Session.java @@ -180,6 +180,15 @@ public interface I_AD_Session */ public String getRemote_Host(); + /** Column name ServerName */ + public static final String COLUMNNAME_ServerName = "ServerName"; + + /** Set Server name */ + public void setServerName (String ServerName); + + /** Get Server name */ + public String getServerName(); + /** Column name Updated */ public static final String COLUMNNAME_Updated = "Updated"; diff --git a/org.adempiere.base/src/org/compiere/model/MSession.java b/org.adempiere.base/src/org/compiere/model/MSession.java index 948bba494e..99baa121b7 100644 --- a/org.adempiere.base/src/org/compiere/model/MSession.java +++ b/org.adempiere.base/src/org/compiere/model/MSession.java @@ -148,6 +148,7 @@ public class MSession extends X_AD_Session public MSession (Properties ctx, String Remote_Addr, String Remote_Host, String WebSession, String trxName) { this (ctx, 0, trxName); + setServerName(Ini.getProperties().getProperty("ServerName")); if (Remote_Addr != null) setRemote_Addr(Remote_Addr); if (Remote_Host != null) @@ -172,6 +173,7 @@ public class MSession extends X_AD_Session try { InetAddress lh = InetAddress.getLocalHost(); + setServerName(Ini.getProperties().getProperty("ServerName")); setRemote_Addr(lh.getHostAddress()); setRemote_Host(lh.getHostName()); setDescription(Adempiere.MAIN_VERSION + "_" diff --git a/org.adempiere.base/src/org/compiere/model/X_AD_Session.java b/org.adempiere.base/src/org/compiere/model/X_AD_Session.java index 9221ef8268..372a237e2e 100644 --- a/org.adempiere.base/src/org/compiere/model/X_AD_Session.java +++ b/org.adempiere.base/src/org/compiere/model/X_AD_Session.java @@ -220,6 +220,20 @@ public class X_AD_Session extends PO implements I_AD_Session, I_Persistent return (String)get_Value(COLUMNNAME_Remote_Host); } + /** Set Server name. + @param ServerName Server name */ + public void setServerName (String ServerName) + { + set_Value (COLUMNNAME_ServerName, ServerName); + } + + /** Get Server name. + @return Server name */ + public String getServerName () + { + return (String)get_Value(COLUMNNAME_ServerName); + } + /** Set Web Session. @param WebSession Web Session ID diff --git a/org.adempiere.base/src/org/compiere/util/Ini.java b/org.adempiere.base/src/org/compiere/util/Ini.java index 7e9ae05d39..bf35e8ae39 100644 --- a/org.adempiere.base/src/org/compiere/util/Ini.java +++ b/org.adempiere.base/src/org/compiere/util/Ini.java @@ -141,6 +141,9 @@ public final class Ini implements Serializable /** Role */ public static final String P_ROLE = "Role"; private static final String DEFAULT_ROLE = ""; + /**Server Name */ + public static final String P_SERVERNAME = "ServerName"; + private static final String DEFAULT_SERVERNAME = ""; /** Client Name */ public static final String P_CLIENT = "Client"; private static final String DEFAULT_CLIENT = ""; @@ -196,7 +199,7 @@ public final class Ini implements Serializable P_ADEMPIERESYS, P_LOGMIGRATIONSCRIPT, P_SHOW_ACCT, P_SHOW_TRL, P_SHOW_ADVANCED, P_CACHE_WINDOW, P_CONTEXT, P_TEMP_DIR, - P_ROLE, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY, + P_ROLE, P_SERVERNAME, P_CLIENT, P_ORG, P_PRINTER, P_WAREHOUSE, P_TODAY, P_PRINTPREVIEW, P_VALIDATE_CONNECTION_ON_STARTUP, P_SINGLE_INSTANCE_PER_WINDOW, @@ -214,7 +217,7 @@ public final class Ini implements Serializable DEFAULT_ADEMPIERESYS?"Y":"N", DEFAULT_LOGMIGRATIONSCRIPT?"Y":"N", DEFAULT_SHOW_ACCT?"Y":"N", DEFAULT_SHOW_TRL?"Y":"N", DEFAULT_SHOW_ADVANCED?"Y":"N", DEFAULT_CACHE_WINDOW?"Y":"N", DEFAULT_CONTEXT, DEFAULT_TEMP_DIR, - DEFAULT_ROLE, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(), + DEFAULT_ROLE, DEFAULT_SERVERNAME, DEFAULT_CLIENT, DEFAULT_ORG, DEFAULT_PRINTER, DEFAULT_WAREHOUSE, DEFAULT_TODAY.toString(), DEFAULT_PRINTPREVIEW?"Y":"N", DEFAULT_VALIDATE_CONNECTION_ON_STARTUP?"Y":"N", DEFAULT_SINGLE_INSTANCE_PER_WINDOW?"Y":"N", diff --git a/org.adempiere.base/src/org/compiere/util/WebLogin.java b/org.adempiere.base/src/org/compiere/util/WebLogin.java index af315fa5bd..1b288eaffe 100644 --- a/org.adempiere.base/src/org/compiere/util/WebLogin.java +++ b/org.adempiere.base/src/org/compiere/util/WebLogin.java @@ -154,6 +154,9 @@ public class WebLogin */ public boolean action() throws IOException, ServletException { + //remote Address + String remoteIP = m_request.getHeader("X-Forwarded-For"); + remoteIP = remoteIP!=null ? remoteIP : m_request.getRemoteAddr(); // Mode if (getMode() == null) { @@ -228,7 +231,7 @@ public class WebLogin if (m_forward==null || m_forward.equals(getLogin_RelURL ())) m_forward = "/index.jsp"; // Create Session with User ID - MSession cSession = MSession.get (m_ctx, m_request.getRemoteAddr(), + MSession cSession = MSession.get (m_ctx, remoteIP, m_request.getRemoteHost(), m_session.getId()); if (cSession != null) cSession.setWebStoreSession(true); @@ -275,7 +278,7 @@ public class WebLogin // Create / set session if (m_wu.isLoggedIn()) { - MSession cSession = MSession.get (m_ctx, m_request.getRemoteAddr(), + MSession cSession = MSession.get (m_ctx, remoteIP, m_request.getRemoteHost(), m_session.getId()); if (cSession != null) cSession.setWebStoreSession(true); @@ -312,7 +315,7 @@ public class WebLogin { m_session.setAttribute (WebInfo.NAME, new WebInfo (m_ctx, m_wu)); // Create / set session - MSession cSession = MSession.get (m_ctx, m_request.getRemoteAddr(), + MSession cSession = MSession.get (m_ctx, remoteIP, m_request.getRemoteHost(), m_session.getId()); if (cSession != null) cSession.setWebStoreSession(true); @@ -380,7 +383,7 @@ public class WebLogin return false; } - MSession cSession = MSession.get (m_ctx, m_request.getRemoteAddr(), m_request.getRemoteHost(), m_session.getId()); + MSession cSession = MSession.get (m_ctx, remoteIP, m_request.getRemoteHost(), m_session.getId()); if (cSession != null) cSession.setWebStoreSession(true); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java index 5811450ae1..3631ad380d 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java @@ -202,10 +202,11 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb // Create adempiere Session - user id in ctx Session currSess = Executions.getCurrent().getDesktop().getSession(); HttpSession httpSess = (HttpSession) currSess.getNativeSession(); + String x_Forward_IP = Executions.getCurrent().getHeader("X-Forwarded-For"); + + MSession mSession = MSession.get (ctx, x_Forward_IP!=null ? x_Forward_IP : currSess.getRemoteAddr(), + currSess.getRemoteHost(), httpSess.getId() ); - MSession mSession = MSession.get (ctx, currSess.getRemoteAddr(), - currSess.getRemoteHost(), httpSess.getId() ); - currSess.setAttribute("Check_AD_User_ID", Env.getAD_User_ID(ctx)); //enable full interface, relook into this when doing preference