前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信开发之token认证 原

微信开发之token认证 原

作者头像
用户2603479
发布2018-08-16 09:50:33
5120
发布2018-08-16 09:50:33
举报
文章被收录于专栏:JAVA技术站JAVA技术站

服务器配置如下

代码语言:javascript
复制
package com.dongpeng;

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

import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication
public class Application {
	public static final String TOKEN = "lianghao_token";

	@RequestMapping("/")
	@ResponseBody
	public void index(String signature, String timestamp, String nonce, String echostr,HttpServletResponse response)throws Exception {
		System.out.println("signature:" + signature);
		System.out.println("timestamp:" + timestamp);
		System.out.println("nonce:" + nonce);
		System.out.println("echostr:" + echostr);
		System.out.println("TOKEN:" + TOKEN);
		String[] params = new String[] { TOKEN, timestamp, nonce };
		Arrays.sort(params);
		// 将三个参数字符串拼接成一个字符串进行sha1加密
		String clearText = params[0] + params[1] + params[2];
		String algorithm = "SHA-1";
		String sign = new String(org.apache.commons.codec.binary.Hex
				.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
		// 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
		if (signature.equals(sign)) {
			response.getWriter().print(echostr);
		}
	}

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

配置服务端后,提交下面信息

才能成功提交

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档