Quickly build a Spring Boot interface debugging environment accessible to the public network through intranet penetration

Recently, I discovered a super powerful artificial intelligence learning website. It presents complex concepts in an easy-to-understand way and is entertaining. I thought it might be helpful to everyone, so I'm sharing it here. Click here to jump to the website.

Preface

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

1. Local environment setup

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. In order to see it more intuitively, create a pos request interface 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, which supports http/https/tcp protocols, does not limit traffic, does not require public IP, and does not require setting up a router. It is simple to use.

2.1 Install and configure cpolar intranet penetration

cpolar官网: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 (for 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 cpolar official website backend, click Verify on the left, check 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 services to the system
sudo systemctl enable cpolar
  • Start cpolar service
sudo systemctl start cpolar
  • Check service status

20230417112718

2.2 Create tunnel mapping local port

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

20230130105810

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

  • Tunnel name: The name can be customized. 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 Status on the left - Online Tunnel List, view the generated public network address, and then copy the address

20230130105902

2.3 Test 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. The parameters are in JSON format. Set the parameters and click

20230130105903

Debug the debugging interface on the service interface side to check whether the request enters the interface. Entering the interface indicates that the call is successful.

20230130105904

3. Fixed public network address

Since the tunnel created using cpolar above 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. This address is a fixed address and will not change randomly.

Note that you need to upgrade the cpolar package to a basic package or above, and the bandwidth corresponding to each package is different. [cpolar.cn has been registered]

3.1 Reserve a second-level subdomain name

Log in to the cpolar official website, click Reserve on the left, select 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

Visithttp://127.0.0.1:9200/, log in to the cpolar web UI management interface, and click Tunnel Management on the left dashboard ——Tunnel list, find the 8080 tunnel to be configured, 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 second-level subdomain name that has been successfully reserved, in this case test01

Click更新

20230130105907

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

20230130105908

3.2 Test using fixed public network address

Open postman and use the fixed http address to make the call

20230130105909

Also debug on the server side to check whether the request enters the interface. 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 and deal with a nasty bug. You can even replay the request message package to speed up the test request, click the Replay button, and resend the HTTP signaling request instead of manually retriggering the operation. The following describes how to use 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 4040 port in the browser,http://localhost:4040

20230130105913

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

20230130105914

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

Guess you like

Origin blog.csdn.net/xz2935117143/article/details/134330732