“This is the 14th day of my participation in the November Gwen Challenge.The final text challenge in 2021”.

preface

At present, many websites have swept code login, usually with wechat public number, when we want to visit the site of some resources when we need to sweep code attention to the public number, and then automatic registration, login.

The preparatory work

  1. An external network can access the address (no matter, later will introduce how to solve)
  2. AppID, appsecret

1. Obtain appID and AppSecret

In the learning stage, we will directly use the wechat test number, and then directly switch to those without our own authentication

Test Number Login

After scanning for login, we can get the appID and AppSecret of the test number

2. Configure the interface

Here is a pit, the address must be filled in the external network can access the address. The logic here is that wechat platform will send a verification request to our configured address, and our service will make a correct response, indicating that the association is successful. Only after some attention is paid, the request triggered by scanning code will be sent to our server.

We can’t buy a server just to learn, and we develop locally, we can’t debug easily with a server. Here, we can use ngrok to generate a proxy address through Intranet penetration. This address can be accessed from the Internet, but the validity period is only two hours. After all, it is a white prostitute, so please modify it manually in two hours.

Ngrok download Download decompress, you will get an ngrok.exe executable file, click run, enter the port number of the agent, generally for our background service port

ngrok http 9000
Copy the code

This gives us an extranet address that has direct access to our service

Fill in the configuration information and just fill in the token

3. Verify the interface on the server

As I configuration address above, when I click save WeChat server will send a check request to the agent address, and then forwarded to my local, which is http://127.0.0.1:9000/wechat, then we just need to accept the request, the correct response.

I’m not going to bother you here. I’ll just go to the code

@apiOperation (value = "verify wechat server ")
@GetMapping("/wechat")
public String wechatServer(@RequestParam(name = "signature") String signature,
                           @RequestParam(name = "timestamp") String timestamp,
                           @RequestParam(name = "nonce") String nonce,
                           @RequestParam(name = "echostr") String echostr) {

    String token = "Fill in the token in the configuration";
    String[] arr = new String[]{token, timestamp, nonce};
    Arrays.sort(arr);
    String s = SecureUtil.sha1(arr[0] + arr[1] + arr[2]);
    return signature.equals(s) ? echostr : "";
}
Copy the code

Specific authentication logic can be consulted official documentation

conclusion

Up to now, our wechat platform and local service have been successfully connected. After that, we only need to monitor events, expand interfaces and complete business according to our requirements