How to check if URL is the local machine? - java

I have a java.net.URL object in a Spring 4 #RestController. How can I check if the URL (HTTP) leads to the current machine (this Spring application) or is an URL to an external HTTP-Source?
I found out that java.net.URL does hostname resolution to compare to other URL object. Is there a way to reuse this resolution to not having to reinvent the wheel?

Using the code from this post you can check if it is a local IP. You should pass an InetAddress object, so get it from your URL using the following command:
InetAddress.getByName(new URL(urlString).getHost());

I think you can use InetAddress class to check this.
1.First get the local system IP address
String localAddress = InetAddress.getLocalHost().getHostAddress();
2.Fetch the address of url that you want to match.
String requestAddress = InetAddress.getByName(new URL(url).getHost()).getHostAddress();
Now you can compare both 1 and 2 address string.
I hope it helps!!!

Related

java.security.cert.CertificateException: No subject alternative names matching IP address

I have different issues with this exception, Please try to understand.
I'm sending data from one application to another through web service call in Java.
whenever I called it will connect to some other application. in that
a situation I get the above exception, this problem occur only in
byte Grid server.
We solved above problem like this our admin removed security,
means we have https they removed s so we are working with
HTTP, but it's not good, I want to connect through web service call with security, can any one give me the best idea.Please see my sample code
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
String request = PropertyFactory.getProperty("someUrl");
url = new URL(request);
postConnection = (HttpURLConnection) url.openConnection();
can I handle through code?
If you are using Oracle JDK between 1.8.0_51 and 1.8.0_60, there was an issue when connecting via IP address instead of hostname. In the case of using the IP-address, this address also has to be mentioned in the Subject alternative names of the cert. According to Mulesoft Support a workaround would be to set the JVM argument "jdk.tls.trustNameService" to true - resulting in a reverse name lookup for the IP address.
Byte grid having internal firewall so its may be stop, Please contact with byte grid team.

Want to get the common parameter from URL itself even if proxy get change

I want to get the host name and host port from http request i.e from http://101.101.101.11:8080/abc.html
I can get these paramater of URL by httpRequest.gethostname() or request.getserverport()
but when I am acccessing that URL through proxy that time the host name and server port is getting change.
If I use Referer that time I am getting perfect host name and server port. But it is kind of header parameter with that URL. And I need some parameter from URL itself.
Is there any way to get the common parameter from URL itself even if proxy get change.
Thanks very much in advance

Is there anything in HttpServletRequest what I can use to identify node in a cluster?

How can I identify node within a cluster using info in HttpServletRequest?
Any info which is unique for each node is suitable - I need it to distinguish logs.
You can try to get the IP and hostname
// Get client's IP address
String ipAddress = request.getRemoteAddr(); // ip
// Get client's hostname
String hostname = request.getRemoteHost(); // hostname
If it did not give what you want, I would print all the request header and see if there is a unique identifier
for example, some servers would add x-forwarded-for or X_FORWARDED_FOR if the request go through proxy
Add a system variable with -Dnode.id=1, then you can access it with System.getProperty("node.id").
java.net.InetAddress.getLocalHost() would give you the name of the host that served the request. Does that get you what you need?

Get domain name in url with JSTL?

I am trying to get the domain name from the url with JSTL. The 2 methods I know return the wrong information. I need exactly what is in the URL.
When I do:
${pageContext.request.remoteHost}
I get my server's IP.
When I do:
${pageContext.request.serverName}
I normally get the right domain name, but on a server we have on amazon its returning "server1" instead of the correct domain name, probably because of the way it handles multiple domains.
Anyone know how I can get the current domain name in the URL?
I may need to get the URL and then parse it. How would I do that?
You should be using ServletRequest#getLocalName() instead. It returns the real hostname of the server. The ServletRequest#getServerName() indeed returns the value as set in Host header.
${pageContext.request.localName}
That's all. The solutions suggested in other answers are plain clumsy and hacky.
By the way, the ServletRequest#getRemoteHost() doesn't return the server's hostname, but the client's one (or the IP when the hostname is not immediately resolveable). It's obviously the same as the server's one whenever you run both the webserver and webbrowser at physically the same machine. If you're interested in the server's IP, use ServletRequest#getLocalAddr(). The terms "local" and "remote" must be interpreted from server's perspective, not from the client's. It's after all the server there where all that Java code runs.
You can parse domain name from URL
OR
public static String getDomainName(String url)
{
URL u;
try {
u = new URL(url);
}
catch (Exception e) {
return "";
}
return u.getHost();
}
You can use HttpServletRequest.getRequestUrl() to:
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
this would return a String like Get domain name in url with JSTL?
It should then be trivial to parse this to find the string that comes after the scheme (http, https, etc) and before the requestURI.
${pageContext.request.contextPath}

How can I get a the host name (with port) that a servlet is at

I thought ServletContext might provide a method. Does the getAttribute() method of ServletContext provide any help i.e. is there an attribute name (maybe "host", "port") that will be of help.
The reason for this is I want my application to run wherever it is deployed, and at one point I have to allow a user to click a link that points to a location on the file server. Hence I need to reference by the host and port and cannot use an internal reference.
ServletRequest.getServerName(...)
ServletRequest.getServerPort(...)
The ServletRequest object that has been passed to your doGet, or doPost method has getServerName and getServerPort methods that provide this information.
eg
public void doGet(ServletRequest request, ServletResponse response) {
System.out.println("Host = " + request.getServerName());
System.out.println("Port = " + request.getServerPort());
}
#Everyone has a good answer. But taking scheme, server name and port then mergin them. There is a simpler way:
You can use HttpServletRequest.getRequestURL and HttpServletRequest.getRequestURI.
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String host = url.substring(0, url.indexOf(uri)); //result
As others mentioned above, host and port can be retrieved through request. On the other hand, it is impossible for the ServletContext provide the info since java applications are unaware of your host environment. i.e., an application with context path "foo"(which could be retrieved by ServletContext#getContextPath()) could receive requests both from a http port 8080 and a https port 8043. Reference: https://web.archive.org/web/20120401225136/http://www.java.net:80/node/701934
I have found in my old project the string:
request.getHeader("host").contains("xxx")
maybe it is the solution?

Categories

Resources