java method to get request ip

 

In the last article, we introduced the method of obtaining the request IP in Java, so in this article we will explain in detail the method of obtaining the request IP. The method of obtaining the requested IP is based on the HTTP protocol. The principle is as follows: 1. Use a web application to communicate between the web server and the client through the HTTP protocol. 2. The client sends request information, which is processed by the server. 3. The server responds with data and returns the information to the client. 4. The server returns data, which is parsed and displayed by the browser. 5. Let’s take the login webpage as an example to introduce the method of obtaining the requested IP. We need to use the servlet framework to do a small project. Since the project is relatively small, we will not explain it in detail. Let's take a look at the specific code:

  • (1) Get cookies

    We use a method to obtain cookies, and the method used in Java is setCookie. The setCookie method is in the servlet framework and is based on the HTTP protocol, so we need to put the contents of the obtained cookie into setCookie. In the servlet framework, we only need to put the obtained cookie into setCookie without doing any processing on the cookie. Therefore, when obtaining cookies, you only need to care about what setCookie is, and then call the method provided in the servlet framework. We can see that there are two methods in setCookie: $currentContents here refers to the content to be read during access. $token means that the server will put the returned content in the token every time it is accessed. Between $contents and $token, add a comma to separate them.

  • (2) Get token

    In order for the browser to identify the request information, we need to provide a token to the browser. In the servlet framework, there is a method called "protected token", which is used to obtain the token provided by the browser. This method is used by the browser to detect whether the requested information is legal. We can use the following code to obtain the token: Of course, we can also use tokens provided by third parties. For example, if we need to obtain the token of a JavaScript script, we can use the following code to obtain it: We write this code in the servlet, and then use the createToken method to obtain the token of the script. Since Java does not support the token of the Javascript script, we You need to use javascript to generate the token of this script, and then send the generated token to the browser. In this way, the script can be parsed and displayed through the browser.

  • (3) Obtain login password

    The method of obtaining the request IP is introduced here. Let’s take a look at the method of obtaining the login password: The first step is to use the servlet framework to write a small project, and obtain the request IP through the double class in the servlet: The second step is: Obtain the account information of the login password from the URL through javascript code: The third step, after obtaining the account information, we need to verify its legitimacy, and then we can use the setnx method to log in: The fourth step, then, we You can enter your username and password on the web server to log in. Step 5: After successful login, we need to perform some verification operations on the web page to ensure the legitimate rights and interests of the user. Step 6. Finally, we need to verify whether the user's identity is a legitimate user. After successful login, we need to verify again whether the identity is a legal user, otherwise it may cause system insecurity. Therefore, you should also pay attention to legality issues when obtaining the login password.

  • (4) Get the browser address

    To obtain the browser address, we must first obtain the IP address of the browser. Through the HTTP protocol, we can obtain the request IP. Through the reflection mechanism of Java, we can also obtain the request IP. Let's take a look at how to obtain the request IP through the reflection mechanism. First, we need to define a function as the object of reflection. Next, we create a reflection object and call it in the function. For example: Java's reflection mechanism is similar to the definition in java.util.Request.Restart. It should be noted that this class cannot directly reference a custom class because the parent class is referenced in reflection. Then we create a Referer object in the current class. Define a ref function in this object to receive request data. What needs to be noted here is that the ref function needs to be used together with the proxy method of the referer object to obtain the request IP. What needs to be noted here is that the refs function cannot directly reference custom classes. Next, we create a Refs object and use it with the proxy method to receive request data.

  • (5) Obtain HTTP protocol information

    In the previous code, we have obtained the requested information, but the information is incomplete. For example, the requested IP address cannot be known which network segment it is from. At this time we need to obtain the requested information again. Since we have already obtained the request information, in the servlet, we only need to pass one parameter to the servlet, and this parameter is the HTTP protocol address. In fact, this parameter is relatively simple. We only need to pass an HTTP protocol address in the servlet without passing any other information. Of course, if you want to obtain the requested information, you can pass this parameter to the servlet and let the servlet obtain the HTTP protocol address. So how to get the HTTP protocol address? Below we will introduce how to obtain it. First we need to have a connection: This is the code for how the servlet obtains the request IP, which contains some keywords, as follows: In this way, the HTTP protocol address can be obtained.

  • (6) Parse data

    When the client obtains the server's data through an HTTP request, the corresponding data will be returned in the connection interface in the servlet, and then we can use the client class in the servlet to parse the data. After obtaining the request information, we can process it according to the data, or we can directly call the client class for processing, for example: You can see: When we use the client class, we need to call the connection interface in the servlet. In fact, servlet also contains many interfaces. When we want to use these interfaces, we need to implement the corresponding methods. For example: The simplest interface is: When we use the client class for processing, we can use reflection technology. For example: You can see: When processing data, we need to first create a variable myCountData () in the servlet, and then use the createString method to obtain the corresponding data.

Commonly used java obtain request ip code:

1. 使用HttpServletRequest对象获取请求IP地址:
```java
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
```
2. 使用InetAddress对象获取请求IP地址:
```java
InetAddress inetAddress = InetAddress.getLocalHost();
String ipAddress = inetAddress.getHostAddress();
```
3. 使用Spring框架获取请求IP地址:
```java
public String getIpAddress(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
return ipAddress;
}
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130194839#comments_28777508