Local development and debugging micro letter with NATAPP

Under development environment, in order to dock with the micro letter backstage, need access to the public Internet URL address of the server machine (only use port 80/443). Therefore, it is necessary to map the local address to the public Internet using mapping tools.

1. NATAPP

① purchase tunnel

  • Address: https://natapp.cn/
  • Click After registration 登录and purchase tunnel
    | Note: free tunnel will replace the mapping address each time window starts.

   Alt text

  • Fill 80 -port / 443 ports (micro-channel support only two port numbers)

   Alt text

  • Then enter the tunnel just remember to buy your authtoken

② Mapping

  • download apps

   Alt text

  • Create a new config.inidocument reads as follows
    | will authtokenchange the value of a field as a tunnel just purchased authtoken.
1
2
3
4
5
6
7
8

# As the command line parameter mode natapp -authtoken = xxx same parameters and the like will overwrite this configuration.
[default]
authtoken = your authtoken # corresponds authtoken a tunnel.
clienttoken = # corresponds clienttoken client will ignore authtoken, if not leave blank.
log = none #log log file, you can specify a local file, none = not recorded, stdout = direct screen output, default is none.
the LogLevel = ERROR # log level DEBUG, INFO, WARNING, ERROR default is DEBUG.
the http_proxy = # proxy settings as http://10.123.10.10:3128 non-proxy Internet users, be sure to empty.
  • The client programs and config.inifiles in the same folder
  • Start the command line enter the folder input natapp -authtoken=你的authtokento obtain the following results

   Alt text

Note again! ! For the tunnel for free, every time you start natapp.exewhen mapping, mapping its external network address will change.

③ mapping test

  • The construction of Tomcatthe port number to 80 or port 443 (port number corresponding to the tunnel)
    | in Spring Boot project, modify as follows:

   Alt text

   | Other engineering modifications:

   Alt text

If this time access 127.0.0.1:80(或localhost:80)and access 映射外网:80is the same effect, indicating that mapping is successful!


2. The micro-channel mode access Developer

① fill Server Configuration

  • Server Address URL: 所映射的外网地址/工程校验代码的访问地址 .
    | Such as: http://qc95we.natappfree.cc/weixinA.
  • Token: the developer free to fill generate a signature at the time of access authentication.
    | Need to define a string called the Token, this string value corresponding to the filled information, see below in the specific check code.

② verify the validity of the server's address

  • After the developer to submit information, micro-channel server sends a GET request to fill in the server address URL, GET request to bring the following four parameters.
       signature Micro-channel cryptographic signature
       timestamp Timestamp
       nonce random number
       echostr Random string
  • Will need to develop token, timestamp, nonce sort these three parameters, and then splice them into strings sha1 encrypted, then the encrypted string with the micro-channel encrypted signature (Signature) were compared with two strings At the same time (the GET request may identify a server from a micro-channel) parameter returns echostr content, then access success developer.

③ specific code

I configured Servlet check code in Spring Boot project. If you do not want to test in the Spring Boot engineering, you can in other web application web.xmlconfiguration Servlet own file. But different profiles, check code are the same.

  • Project directory structure is as follows:

   Alt text

  • I enclose my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.example.springbootdemo.config;

import com.example.springbootdemo.util.CheckUtil;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/ **
* @Auther : wjy
* @date : 2018/12/4 20:56
* @Description : acquisition parameters.
* /
Public class the extends the HttpServlet {


Large columns   micro channel local development debugging NATAPP SS = "keyword"> protected void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws IOException {
Request.setCharacterEncoding ( "UTF-. 8" );
response.setCharacterEncoding ( "UTF-. 8" );
PrintWriter out = response.getWriter ();

String echostr = request.getParameter("echostr");
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");

if (CheckUtil.check(timestamp, nonce, signature) == true)
out.print(echostr);
}
}
--------------------------------------------------------------------------------
package com.example.springbootdemo.util;

import java.security.MessageDigest;
import java.util.Arrays;

/ **
* @Auther : wjy
* @date : 2018/12/4 21:57
* @Description : developers check.
* /
Public class CheckUtil { public static String token = "weixinAAAA" ;


/ *
* Micro channel according to claim public internet number checksum.
* / Public static Boolean Check (String timestamp, the nonce String, String Signature) { String [] String = new new String [] {timestamp, the nonce, token}; String = S new new String (); String the encrypt = null ;





Arrays.sort(string);
for (int i = 0; i < string.length; i++)
s += string[i];

encrypt = getSha1(s);
System.out.println(encrypt);

if (encrypt.equals(signature))
return true;
else
return false;
}

/*
* 加密。
*/publicstatic String getSha1(String decript){if(decript == null | decript .length() == 0)returnnull;char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd ' ,' e ',





'f'};

try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(decript.getBytes("UTF-8"));
byte[] md = mdTemp.digest();
int j = md.length, k = 0;
char buf[] = new char[j*2];

for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}

return new String(buf);
} catch (Exception e) {
return null;
}
}
}
--------------------------------------------------------------------------------
package com.example.springbootdemo.servlet;

import com.example.springbootdemo.config.WeixinServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

/**
* @Auther: wjy
* @Date: 2018/12/4 21:33
* @Description: SpringBoot中的Servlet配置。
*/
@SpringBootApplication
public class ServletConfig extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(ServletConfig.class, args);
}


protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ServletConfig.class);
}

// 注册Servlet。
@Bean
public ServletRegistrationBean wexinA() {
return new ServletRegistrationBean(new WeixinServlet(), "/weixinA");
}
}

④ mapping debugging

  • At this point can 映射地址/wenxinA 或 localhost:80/weixinAaccess the Servlet
    | Note: Because access this verification code needs to pass four parameters, so as to directly access the console quoted null pointer exception, the problem is not configured! !

   Alt text
   Alt text

  • Servlet's URL will have access to micro-channel public platform to configure the server URL column
  • Finally, fill in Tokenthe corresponding value
    | check my code, the Tokenstring value wenxinAAAA .

Alt text


appendix

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/12390590.html