微信公众号服务器配置token验证

pom中引入jar:

		
			commons-codec
			commons-codec
			1.9
		
		
			com.github.binarywang
			weixin-java-mp
			3.1.0
		

 token验证方法:

    /**
	 * 将微信公众号发送来的加密后的消息体解密
	 * @return
	 * @throws Exception
	 */
	@GetMapping(produces = "text/plain;charset=utf-8",value = "/msg")
	public static String msgEncrypt(
			@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
	) throws Exception{
		System.out.print("进入token验证");
		System.out.print("signature:"+signature);
		String token = "token";

                 //字典序排序
		String[] strs = new String[]{token,timestamp,nonce};
		Arrays.sort(strs);

		System.out.print("加密后:"+SHA1.gen(strs));
		try {
			//判断加密后的字符串与signature字符串是否一致
			if( SHA1.gen(strs).equals(signature)){
				//一致返回rechostr
				return echostr;
			}
			else {
				//不一致返回error
				return "error";
			}
		} catch (Exception var5) {
			return "error";
		}
	}

 

 

你可能感兴趣的:(java)