1
1
package com .github .binarywang .demo .wx .mp .controller ;
2
2
3
+ import org .apache .commons .lang3 .StringUtils ;
4
+ import org .slf4j .Logger ;
5
+ import org .slf4j .LoggerFactory ;
6
+ import org .springframework .web .bind .annotation .GetMapping ;
7
+ import org .springframework .web .bind .annotation .PathVariable ;
8
+ import org .springframework .web .bind .annotation .PostMapping ;
9
+ import org .springframework .web .bind .annotation .RequestBody ;
10
+ import org .springframework .web .bind .annotation .RequestMapping ;
11
+ import org .springframework .web .bind .annotation .RequestParam ;
12
+ import org .springframework .web .bind .annotation .RestController ;
13
+
3
14
import com .github .binarywang .demo .wx .mp .config .WxMpConfiguration ;
4
15
import me .chanjar .weixin .mp .api .WxMpService ;
5
16
import me .chanjar .weixin .mp .bean .message .WxMpXmlMessage ;
6
17
import me .chanjar .weixin .mp .bean .message .WxMpXmlOutMessage ;
7
- import org .apache .commons .lang3 .StringUtils ;
8
- import org .slf4j .Logger ;
9
- import org .slf4j .LoggerFactory ;
10
- import org .springframework .web .bind .annotation .*;
11
18
12
19
/**
13
20
* @author Binary Wang(https://github.com/binarywang)
@@ -23,15 +30,18 @@ public String authGet(@PathVariable String appid,
23
30
@ RequestParam (name = "timestamp" , required = false ) String timestamp ,
24
31
@ RequestParam (name = "nonce" , required = false ) String nonce ,
25
32
@ RequestParam (name = "echostr" , required = false ) String echostr ) {
26
- final WxMpService wxService = WxMpConfiguration .getMpServices ().get (appid );
27
33
28
34
this .logger .info ("\n 接收到来自微信服务器的认证消息:[{}, {}, {}, {}]" , signature ,
29
35
timestamp , nonce , echostr );
30
-
31
36
if (StringUtils .isAnyBlank (signature , timestamp , nonce , echostr )) {
32
37
throw new IllegalArgumentException ("请求参数非法,请核实!" );
33
38
}
34
39
40
+ final WxMpService wxService = WxMpConfiguration .getMpServices ().get (appid );
41
+ if (wxService == null ) {
42
+ throw new IllegalArgumentException (String .format ("未找到对应appid=[%d]的配置,请核实!" , appid ));
43
+ }
44
+
35
45
if (wxService .checkSignature (timestamp , nonce , signature )) {
36
46
return echostr ;
37
47
}
@@ -45,12 +55,13 @@ public String post(@PathVariable String appid,
45
55
@ RequestParam ("signature" ) String signature ,
46
56
@ RequestParam ("timestamp" ) String timestamp ,
47
57
@ RequestParam ("nonce" ) String nonce ,
58
+ @ RequestParam ("openid" ) String openid ,
48
59
@ RequestParam (name = "encrypt_type" , required = false ) String encType ,
49
60
@ RequestParam (name = "msg_signature" , required = false ) String msgSignature ) {
50
61
final WxMpService wxService = WxMpConfiguration .getMpServices ().get (appid );
51
- this .logger .info ("\n 接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
62
+ this .logger .info ("\n 接收微信请求:[openid=[{}], [ signature=[{}], encType=[{}], msgSignature=[{}],"
52
63
+ " timestamp=[{}], nonce=[{}], requestBody=[\n {}\n ] " ,
53
- signature , encType , msgSignature , timestamp , nonce , requestBody );
64
+ openid , signature , encType , msgSignature , timestamp , nonce , requestBody );
54
65
55
66
if (!wxService .checkSignature (timestamp , nonce , signature )) {
56
67
throw new IllegalArgumentException ("非法请求,可能属于伪造的请求!" );
0 commit comments