微信公众平台基本配置--服务器token验证(Java实现)

avatar
作者
筋斗云
阅读量:7
1. 登录微信公众平台

对于自学者,可以申请公众号测试账号,地址:微信公众平台 注册账号

对于企业开发者,可以直接用公司微信公众号登录微信公众平台,地址:微信公众平台

登录后,在导航栏最底部找到开发—基本配置

2.后台token验证接口
<mp.weixin.version>4.3.0</mp.weixin.version>  <!--微信依赖--> <dependency>     <groupId>com.github.binarywang</groupId>     <artifactId>weixin-java-mp</artifactId>     <version>${mp.weixin.version}</version> </dependency> <dependency>     <groupId>com.github.binarywang</groupId>     <artifactId>weixin-java-cp</artifactId>     <version>${mp.weixin.version}</version> </dependency> <dependency>     <groupId>com.github.binarywang</groupId>     <artifactId>weixin-java-common</artifactId>     <version>${mp.weixin.version}</version> </dependency>
@RestController @AllArgsConstructor @RequestMapping("/{appId}/portal") public class WxPortalController {  /** 	 * 微信接入校验处理 	 * @param appId 多公众号标志位 	 * @param signature 微信签名 	 * @param timestamp 时间戳 	 * @param nonce 随机数 	 * @param echostr 随机字符串 	 * @return 	 */ 	@XssCleanIgnore 	@GetMapping(produces = "text/plain;charset=utf-8") 	public String checkToken(@PathVariable("appId") String appId, 			@RequestParam(name = "signature", required = false) String signature, 			@RequestParam(name = "timestamp", required = false) String timestamp, 			@RequestParam(name = "nonce", required = false) String nonce, 			@RequestParam(name = "echostr", required = false) String echostr) {  		log.info("接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);  		if (StrUtil.isAllBlank(signature, timestamp, nonce, echostr)) { 			throw new IllegalArgumentException("请求参数非法,请核实!"); 		}  		final WxMpService wxService = WxMpInitConfigRunner.getMpServices().get(appId);  		if (wxService == null) { 			throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appId)); 		}  		if (wxService.checkSignature(timestamp, nonce, signature)) { 			return echostr; 		}  		return "非法请求"; 	} }
 3.填写接口

https://api.xxx.com/mp/youerAppId/portal

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!