LocalHost 추가

master
mjkhan21 9 months ago
parent 0188c094e2
commit 16e682896b

@ -28,53 +28,31 @@ public class ApplicationContainer implements ApplicationContextAware {
return obj; return obj;
} }
private String
hostAddress,
hostName,
canonicalHostName;
private ApplicationContext actx; private ApplicationContext actx;
private String applicationName; private String applicationName;
private List<String> activeProfiles; private List<String> activeProfiles;
private Date startupDate; private Date startupDate;
private boolean secured; private boolean secured;
public ApplicationContainer() {
try {
init();
} catch (Exception e) {
throw ApplicationException.get(e);
}
}
/** IP . /** IP .
* @return IP * @return IP
*/ */
public String getHostAddress() { public String getHostAddress() {
return hostAddress; return LocalHost.getAddress();
} }
/** . /** .
* @return * @return
*/ */
public String getHostName() { public String getHostName() {
return hostName; return LocalHost.getName();
} }
/**{@link InetAddress#getCanonicalHostName()} /**{@link InetAddress#getCanonicalHostName()}
* @return Canonical host name * @return Canonical host name
*/ */
public String getCanonicalHostName() { public String getCanonicalHostName() {
return canonicalHostName; return LocalHost.getCanonicalName();
}
/**ApplicationContainer .
* @throws Exception
*/
protected void init() throws Exception {
InetAddress inetAddress = InetAddress.getLocalHost();
hostAddress = inetAddress.getHostAddress();
hostName = inetAddress.getHostName();
canonicalHostName = inetAddress.getCanonicalHostName();
} }
/**ApplicationContext . /**ApplicationContext .
@ -157,6 +135,6 @@ public class ApplicationContainer implements ApplicationContextAware {
@Override @Override
public String toString() { public String toString() {
return getClass().getSimpleName() + "[hostAddress=" + hostAddress + ", hostName=" + hostName + ", applicationName=" + applicationName + (secured ? ", secured" : "") + "]"; return getClass().getSimpleName() + "[hostAddress=" + getHostAddress() + ", hostName=" + getHostName() + ", applicationName=" + applicationName + (secured ? ", secured" : "") + "]";
} }
} }

@ -0,0 +1,35 @@
package cokr.xit.foundation;
import java.net.InetAddress;
public class LocalHost {
private static final LocalHost obj = new LocalHost();
private String
address,
name,
canonicalName;
public LocalHost() {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
address = inetAddress.getHostAddress();
name = inetAddress.getHostName();
canonicalName = inetAddress.getCanonicalHostName();
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
public static final String getAddress() {
return obj.address;
}
public static final String getName() {
return obj.name;
}
public static final String getCanonicalName() {
return obj.canonicalName;
}
}

@ -12,6 +12,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import cokr.xit.foundation.Access; import cokr.xit.foundation.Access;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.LocalHost;
/** {@link Access } .<br /> /** {@link Access } .<br />
* *
@ -46,11 +48,25 @@ public class AccessFilter implements Filter {
.setSessionID(session != null ? session.getId() : null) .setSessionID(session != null ? session.getId() : null)
.setAjaxRequest(hreq.getHeader("X-Requested-With")) .setAjaxRequest(hreq.getHeader("X-Requested-With"))
.setJsonResponse(hreq.getHeader("accept")) .setJsonResponse(hreq.getHeader("accept"))
.setIpAddress(getClientAddress(hreq))
.setCurrent(); .setCurrent();
hreq.setAttribute("currentAccess", access); hreq.setAttribute("currentAccess", access);
} }
private static final String UNKNOWN = "unknown";
private static final String[] HEADERS = {"x-forwarded-for", "X-FORWARDED-FOR", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR"};
private String getClientAddress(HttpServletRequest hreq) {
for (String header: HEADERS) {
String addr = hreq.getHeader(header);
if (!Assert.isEmpty(addr) && !UNKNOWN.equalsIgnoreCase(addr))
return addr;
}
return Access.getClientAddress(hreq.getRemoteAddr(), LocalHost.getAddress());
}
@Override @Override
public void destroy() {} public void destroy() {}
} }

@ -78,22 +78,7 @@ public class AccessInitializer extends RequestInterceptor {
Locale locale = localeResolver.resolveLocale(hreq); Locale locale = localeResolver.resolveLocale(hreq);
Access.current() Access.current().setLocale(locale);
.setIpAddress(getClientAddress(hreq))
.setLocale(locale);
}
private static final String UNKNOWN = "unknown";
private static final String[] HEADERS = {"x-forwarded-for", "X-FORWARDED-FOR", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR"};
private String getClientAddress(HttpServletRequest hreq) {
for (String header: HEADERS) {
String addr = hreq.getHeader(header);
if (!isEmpty(addr) && !UNKNOWN.equalsIgnoreCase(addr))
return addr;
}
return Access.getClientAddress(hreq.getRemoteAddr(), applicationContainer.getHostAddress());
} }
@Override @Override

Loading…
Cancel
Save