How to change Remot Ip address from jsoup request? - java

I have Jsoup code and successfully send request.Also this code work fine in hide/change 'X-Forwarded-For' Header data, but i cant hide/change Remote/System Ip Address.
Client Side Code:
Document doc = Jsoup.connect("http://192.168.XX.XX:XXXX/microFin/XXXX")
.header("X-Forwarded-For", "192.168.0.1").get();
Server Side Code:
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String authCredentials = request.getHeader("Authorization");
String pathInfo = request.getServletPath();/////api/auth
String ip = request.getHeader("X-Forwarded-For");
String ip11 = request.getRemoteAddr();
if (ip == null) {
ip = request.getRemoteAddr();
}
System.out.println("IP-ADDRESS::" + ip);//192.168.0.1
System.out.println("IP-ADDRESS::" + ip11);//actual ip ???
If any solution for change System Ip then please help me.

You can use a VPN service to hide the IP address of the client machine. There are several software ranging from premium to paid.
My software of preference is: TunnelBear Link

Related

How to get the remote ip address from google cloud endpoint?

I'm trying to check callers to a method in Google Cloud Endpoint against a
whitelist. How to get the remote address of a client? (And how to get the request object?)
UPDATE: Thanks to #ikerlasaga:
#ApiMethod(name = "echo")
public Message echo(HttpServletRequest req, Message message, #Named("n") #Nullable Integer n) {
String remote = req.getRemoteAddr();
return doEcho(message, n);
}
From this post: Getting raw HTTP Data (Headers, Cookies, etc) in Google Cloud Endpoints
#ApiMethod(name = "echo")
public Message echo(HttpServletRequest req, Message message, #Named("n") #Nullable Integer n) {
String remote = req.getRemoteAddr();
return doEcho(message, n);
}
Thanks to #ikerlasaga

Is there a way to write a function that gets an IP Address using HttpServletRequest without passing it as an argument?

I'm creating a function that doesn't take an HttpServletRequest object as an argument because depending on the session (it could be either through a mobile device or a web browser). If it's a mobile device, it uses the latitude and longitude, or if its from a web browser, I want to be able to grab the IP address. Is there a way to achieve this? Every examples I saw takes an HttpServletRequest as an argument.
This is an example of what I would like to accomplish, if possible.
public String getLocation(Session session) {
switch(session.getLocation()) {
case Mobile:
System.out.printf("Latitude is %s and Longitude is %s\n", session.getLatitude(), session.getLongitude());
break;
case Web:
HttpServletRequest request;
String ipAddress = request.getRemoteAddr();
System.out.printf("The IP Adress is %s", ipAddress);
break;
default:
System.out.print("Error\n");
break;
}
}
An IP address is a property of a request. Session may correspond to many requests (that comprise that session), and they may come from different IP addresses. So it is not possible to derive a unique IP address from a Session in some 'standard' way.
But what you can do is store an IP address from request to its session in a filter. For example, each request could simply write its IP address to the session's attribute, so at any moment you'd have access to the IP address of the last request in that session. Or you could implement some Accumulator and store it in your session and put request's IP address to that Accumulator which would implement some logic (like choosing 'the most popular' IP address, or do something else). That's up to you.
How to implement the filter:
public class RequestIPSavingFilter implements Filter {
#Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
if (req instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) req;
final String ipAddress = request.getRemoteAddr();
Accumulator accumulator = (Accumulator) request.getSession().getAttribute("accumulator");
if (accumulator == null) {
accumulator = new Accumulator();
request.getSession().setAttribute("accumulator", accumulator);
}
accumulator.putIpAddress(ipAddress);
}
chain.doFilter(req, resp);
}
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
#Override
public void destroy() {
}
}
Later, in your session processing code, you do
Accumulator accumulator = (Accumulator) request.getSession().getAttribute("accumulator");
and use accumulator.getBestIpAddress()

How to get public ip from desktop application call to our webservice

I need to capture public ip of the system calling our api, that system is a desktop Application thats calling our rest api and I am using the following code
#RequestMapping(value = "/api", method = RequestMethod.POST)
public #ResponseBody JSON_Response sendMessage(#RequestBody Objectjson objjson,HttpServletRequest
request) {
LOG.debug("sending message via payless server");
inputjson.setSentFrom("Message Sent From Api");
// inputjson.setHostip(""+request.getRemoteHost());
//I am using following to capture it
LOG.debug("--------------------------"+request.getRemoteUser());
LOG.debug("--------------------------"+request.getLocalAddr());
LOG.debug("--------------------------"+request.getRemoteHost());
LOG.debug("--------------------------"+request.getPathInfo());
LOG.debug("--------------------------"+request.getPathTranslated());
LOG.debug("--------------------------"+request.getRemoteUser());
LOG.debug("--------------------------"+request.getRemoteUser());
LOG.debug("--------------------------"+request.getRemoteUser());
LOG.debug("--------------------------"+request.getRemoteUser());
LOG.debug("--------------------------"+request.getRemoteUser());
JSON_Response response = sendMessageInterface.processInput(inputjson);
LOG.debug("sending response message " + response.getStatusDescription());
return response;
}
I am getting my own server ip in the ip address.If i call the rest api from postman i am getting the correct ip address.
Please let me know if you find any other way to retrieve public ip .
I am using Wildfly Server wildfly-10.1.0.Final
This is the method that i Use to get the remote user IP address. Hope it helps
public HttpServletRequest getRequest() {
RequestAttributes attribs = RequestContextHolder.getRequestAttributes();
if (attribs instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)attribs).getRequest();
return request;
}
return null;
}
public String getClientIp() {
String remoteAddr = "";
HttpServletRequest request = getRequest();
if (request != null) {
remoteAddr = request.getHeader("X-FORWARDED-FOR");
if (remoteAddr == null || remoteAddr.trim().isEmpty()) {
remoteAddr = request.getRemoteAddr();
}
}
return remoteAddr;
}

Java HttpServletRequest Issue

I have a web service hosted for sharing transaction details. There are two clients connected with my web service.
User setup at my end is like below
KEJESTORE ==== 201.XXX.XX.XX
MARIOBROS ==== 81.XX.XX.XX
I get their Username and ip address of the client server for security reasons using the below method in each time when they call transaction method;
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest) msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIp = req.getRemoteAddr();
String user = wsCtxt.getUserPrincipal().getName();
But I get results like below some times (This happens very rarely).
KEJESTORE === 81.XX.XX.XX
MARIOBROS === 201.XXX.XX.XX
I can not figure out if there is any problem with my above code which i'm using for this purpose.
Please advice.
Edit:
Method
public TranResponse sendTransaction(WebServiceContext wsCtxt, Transaction tran){
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest) msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIp = req.getRemoteAddr();
String user = wsCtxt.getUserPrincipal().getName();
// more code
}

How to identify IP address of requesting client?

How does a server actually identify the requesting client address (IP), and send a response?
Is it possible to get the IP address of requesting client in GAE?
In a Java servlet you could use request.getRemoteAddr():
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
String ipAddress = req.getRemoteAddr();
}
If you are using Appengine with Go, the Request object contains the address in the string field RemoteAddr:
import (
"fmt"
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.RemoteAddr)
}

Categories