From 16e682896b211b8973f255e3c131ac42684f76b4 Mon Sep 17 00:00:00 2001 From: mjkhan21 Date: Wed, 14 Feb 2024 10:53:00 +0900 Subject: [PATCH] =?UTF-8?q?LocalHost=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xit/foundation/ApplicationContainer.java | 30 +++------------- .../java/cokr/xit/foundation/LocalHost.java | 35 +++++++++++++++++++ .../cokr/xit/foundation/web/AccessFilter.java | 16 +++++++++ .../xit/foundation/web/AccessInitializer.java | 17 +-------- 4 files changed, 56 insertions(+), 42 deletions(-) create mode 100644 src/main/java/cokr/xit/foundation/LocalHost.java diff --git a/src/main/java/cokr/xit/foundation/ApplicationContainer.java b/src/main/java/cokr/xit/foundation/ApplicationContainer.java index ba1beb2..81f5de7 100644 --- a/src/main/java/cokr/xit/foundation/ApplicationContainer.java +++ b/src/main/java/cokr/xit/foundation/ApplicationContainer.java @@ -28,53 +28,31 @@ public class ApplicationContainer implements ApplicationContextAware { return obj; } - private String - hostAddress, - hostName, - canonicalHostName; private ApplicationContext actx; private String applicationName; private List activeProfiles; private Date startupDate; private boolean secured; - public ApplicationContainer() { - try { - init(); - } catch (Exception e) { - throw ApplicationException.get(e); - } - } - /**호스트의 IP 주소를 반환한다. * @return 호스트의 IP 주소 */ public String getHostAddress() { - return hostAddress; + return LocalHost.getAddress(); } /**호스트 이름을 반환한다. * @return 호스트 이름 */ public String getHostName() { - return hostName; + return LocalHost.getName(); } /**{@link InetAddress#getCanonicalHostName()} 참고 * @return Canonical host name */ public String getCanonicalHostName() { - return canonicalHostName; - } - - /**ApplicationContainer를 초기화한다. - * @throws Exception - */ - protected void init() throws Exception { - InetAddress inetAddress = InetAddress.getLocalHost(); - hostAddress = inetAddress.getHostAddress(); - hostName = inetAddress.getHostName(); - canonicalHostName = inetAddress.getCanonicalHostName(); + return LocalHost.getCanonicalName(); } /**ApplicationContext를 반환한다. @@ -157,6 +135,6 @@ public class ApplicationContainer implements ApplicationContextAware { @Override 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" : "") + "]"; } } \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/LocalHost.java b/src/main/java/cokr/xit/foundation/LocalHost.java new file mode 100644 index 0000000..dcf28be --- /dev/null +++ b/src/main/java/cokr/xit/foundation/LocalHost.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/web/AccessFilter.java b/src/main/java/cokr/xit/foundation/web/AccessFilter.java index a585f0a..232fc97 100644 --- a/src/main/java/cokr/xit/foundation/web/AccessFilter.java +++ b/src/main/java/cokr/xit/foundation/web/AccessFilter.java @@ -12,6 +12,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import cokr.xit.foundation.Access; +import cokr.xit.foundation.Assert; +import cokr.xit.foundation.LocalHost; /**사용자의 {@link Access 접근정보}를 추출하는 필터.
* 추출한 접근정보는 @@ -46,11 +48,25 @@ public class AccessFilter implements Filter { .setSessionID(session != null ? session.getId() : null) .setAjaxRequest(hreq.getHeader("X-Requested-With")) .setJsonResponse(hreq.getHeader("accept")) + .setIpAddress(getClientAddress(hreq)) .setCurrent(); 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 public void destroy() {} } \ No newline at end of file diff --git a/src/main/java/cokr/xit/foundation/web/AccessInitializer.java b/src/main/java/cokr/xit/foundation/web/AccessInitializer.java index 4ffb262..96d52c2 100644 --- a/src/main/java/cokr/xit/foundation/web/AccessInitializer.java +++ b/src/main/java/cokr/xit/foundation/web/AccessInitializer.java @@ -78,22 +78,7 @@ public class AccessInitializer extends RequestInterceptor { Locale locale = localeResolver.resolveLocale(hreq); - Access.current() - .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()); + Access.current().setLocale(locale); } @Override