Using port mapping to implement public network remote debugging of Spring Boot server interface: detailed configuration and step analysis

foreword

In the front-end and back-end separation projects, when debugging the call interface, we can use the cpolar intranet penetration to simulate the local server interface to simulate the public network environment for remote call debugging. In this tutorial, we take the Java server interface as an example.

1. Local environment construction

1.1 Environmental parameters

  • JDK1.8
  • IDEA
  • SpringBoot
  • Maven
  • Tomcat9.0
  • Postman

1.2 Build springboot service project

Build a springboot service project and write an interface. For better visualization, create an interface for pos requests here

@RestController
@RequestMapping("/test")
public class InterfaceTest {
    
    
    
    /**
     * 测试接口
     * @param data
     * @return Map<String,String>
     */
    @PostMapping("/interTest")
    public Map<String,String>interTest(@RequestBody Map<String,String> data){
    
    
        System.out.println(data);

        if (data.size()>0){
    
    

            return  data;
        }

        data.put("code","404");
        return data;
    }
}

2. Intranet penetration

Here we use cpolar for intranet penetration, supports http/https/tcp protocols, does not limit traffic, does not require public network IP, and does not need to set up routers. It is easy to use.

2.1 Install and configure cpolar intranet penetration

cpolar official website: https://www.cpolar.com/

2.1.1 windows system

After entering the cpolar official website, download the windows version, double-click the installation package and install it by default.

2.1.2 Linux system

  • cpolar installation (domestic use)
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
  • Or cpolar short link installation method: (for foreign use)
curl -sL https://git.io/cpolar | sudo bash
  • Check the version number, if the version number is displayed normally, the installation is successful
cpolar version
  • token authentication

Log in to the background of the cpolar official website, click the verification on the left to view your authentication token, and then paste the token in the command line

cpolar authtoken xxxxxxx

20230417112726

  • Simple Penetration Test
cpolar http 8080

Press ctrl+c to exit

  • Add a service to the system
sudo systemctl enable cpolar
  • Start the cpolar service
sudo systemctl start cpolar
  • View service status

20230417112718

2.2 Create a tunnel mapping local port

After cpolar is successfully installed, visit the local port 9200 [ http://localhost:9200 ] on the browser , and log in with the cpolar account.

20230130105810

Click Tunnel Management on the left dashboard - Create Tunnel to create a tomcat port 8080 http tunnel

  • Tunnel name: you can customize the name, be careful not to duplicate the existing tunnel name
  • Protocol: select http
  • Local address: 8080
  • Domain name type: choose a random domain name for free
  • Region: Select China VIP

click创建

20230130105901

After the tunnel is successfully created, click on the status on the left - online tunnel list, view the generated public network address, and then copy the address

20230130105902

2.3 Test the public network address

Here, the Postman interface debugging tool is used to send a request to the interface, and a post request method is created in postman. Enter the copied public network address plus the interface path, and the parameters are in JSON format. After setting the parameters, click

20230130105903

Debug the debugging interface on the service interface side to check whether the request enters the interface, and entering the interface indicates that the call is successful

20230130105904

3. Fixed public network address

Since the above tunnel created by using cpolar uses a random public network address, it will change randomly within 24 hours, which is not conducive to long-term remote access. Therefore, we can configure a second-level subdomain name for it, which is a fixed address and will not change randomly

Note that the cpolar package needs to be upgraded to the basic package or above, and the bandwidth corresponding to each package is different. [cpolar.cn has been filed]

3.1 Reserve a second-level subdomain

Log in to the cpolar official website, click Reserve on the left, choose to reserve the second-level subdomain name, set a second-level subdomain name, click Reserve, and copy the reserved second-level subdomain name after the reservation is successful

20230130105905

3.2 Configure the second-level subdomain name

Visit http://127.0.0.1:9200/ , log in to the cpolar web UI management interface, click Tunnel Management on the left dashboard - Tunnel List, find the 8080 tunnel to be configured, and click Edit on the right

20230130105906

Modify the tunnel information, and configure the successfully reserved second-level subdomain name into the tunnel

  • Domain name type: select a second-level subdomain name
  • Sub Domain: Fill in the reserved second-level sub-domain name, in this case test01

click更新

20230130105907

After the update is complete, open the online tunnel list. At this time, you can see that the public network address has changed, and the address name has also become the reserved second-level subdomain name. Copy it down

20230130105908

3.2 Test using a fixed public network address

Open postman and use a fixed http address to call

20230130105909

Also debug on the server side to check whether the request enters the interface, and entering the interface indicates success

20230130105910

4. Cpolar listener

We can also use the cpolar listener ( http://localhost:4040 ) to view the interface request log, dealing with a nasty bug. It is even possible to replay the request message packet, to speed up the test request, click the replay (Replay) button, and resend the HTTP signaling request instead of manually retriggering the operation. The following describes the use of the cpolar listener to monitor requests.

4.1 Turn on the listening function

Select the http tunnel we just created and configured, and click on the right编辑

20230130105911

Open the advanced settings and turn on the listening function

20230130105912

4.2 Request Listening

Access the local port 4040 in the browser, http://localhost:4040

20230130105913

After sending a request to the server, the relevant request log will be displayed here, and you can see the request method, requested data, interface path, and return status and results, which greatly improves the debugging efficiency.

20230130105914

Reprinted from the article of cpolar intranet penetration: Springboot server interface public network remote debugging, and HTTP service monitoring

Guess you like

Origin blog.csdn.net/w2915w/article/details/132474402