LocalHost 추가

master
mjkhan21 9 months ago
parent 0188c094e2
commit 16e682896b

@ -28,53 +28,31 @@ public class ApplicationContainer implements ApplicationContextAware {
return obj;
}
private String
hostAddress,
hostName,
canonicalHostName;
private ApplicationContext actx;
private String applicationName;
private List<String> 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" : "") + "]";
}
}

@ -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 cokr.xit.foundation.Access;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.LocalHost;
/** {@link Access } .<br />
*
@ -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() {}
}

@ -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

Loading…
Cancel
Save