微信H5授权登陆
Controller
package com.iimscloud.auth.provider.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject;
import com.iimscloud.auth.provider.WechatUtils;
import com.iimscloud.common.exception.OpenAlertException;
import com.iimscloud.common.utils.StringUtils; import lombok.extern.slf4j.Slf4j; /**
* @author wjc
* @description
* @date 2019/8/30
*/
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WechatController { @Autowired
private RestTemplate restTemplate; @Value("${iimscloud.auth.wechat.token}")
private String token; /**
* @description 微信公众平台基本配置-token
* @author wjc
* @date 2019/8/30
*/
@GetMapping("/token")
public String token(@RequestParam(value = "signature") String signature,
@RequestParam(value = "timestamp") String timestamp,
@RequestParam(value = "nonce") String nonce,
@RequestParam(value = "echostr") String echostr){
if (!WechatUtils.checkSignature(token, signature, timestamp, nonce)){
log.error("匹配微信token失败");
return null;
}
return echostr;
} @GetMapping("/getH5AccessToken")
public void getH5AccessToken(String code){
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=XXX&secret=XXX&code="+code+"&grant_type=authorization_code";
String result = restTemplate.getForObject(url, null);
JSONObject jsonObject = JSONObject.parseObject(result);
String openid = jsonObject.getString("openid");
String access_token = jsonObject.getString("access_token");
log.info("openid: " + openid);
log.info("access_token: " + access_token);
} @GetMapping("/getWechatAccessToken")
public String getAccessToken(String appid, String appSecret){
StringBuffer url = new StringBuffer("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential");
url.append("appid=").append(appid).append("appSecret=").append(appSecret);
String result = restTemplate.getForObject(url.toString(), null); JSONObject jsonObject = JSONObject.parseObject(result);
String access_token = jsonObject.getString("access_token");
if(StringUtils.isNotBlank(access_token)){
throw new OpenAlertException("获取access_token失败");
}
// redisTemplate.opsForValue().set("access_token", access_token, 7200);
return access_token;
} }
WechatUtils
package com.iimscloud.auth.provider; import com.alibaba.fastjson.JSONObject;
import com.iimscloud.common.exception.OpenAlertException;
import com.iimscloud.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate; import java.security.MessageDigest;
import java.util.Arrays; /**
* @author wjc
* @description 微信相关工具类
* @date 2019/8/30
*/
public class WechatUtils { @Autowired
private static RedisTemplate redisTemplate;
@Autowired
private static RestTemplate restTemplate;
@Value("${iimscloud.auth.wechat.appid}")
private String appid;
@Value("${iimscloud.auth.wechat.appSecret}")
private String appSecret; /**
* @description 校验微信参数
* @author wjc
* @date 2019/8/30
*/
public static boolean checkSignature(String token, String signature, String timestamp, String nonce){
String [] arr = {token, timestamp, nonce};
Arrays.sort(arr);
StringBuffer sbf = new StringBuffer();
for (String str : arr){
sbf.append(str);
}
String getSignature = getSha1(sbf.toString());
return signature.equals(getSignature);
} public static String getSha1(String str){
if(str==null || str.length()==0){
return null;
}
char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
try {
MessageDigest mdTemp=MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("UTF-8"));
byte[] md=mdTemp.digest();
int j=md.length;
char buf[]=new char[j*2];
int k=0;
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;
}
} }
微信H5授权登陆的更多相关文章
- 微信登陆,微信SDK授权登陆经验分享
From:http://www.eoeandroid.com/thread-547012-1-1.html 最近因为项目需要做了微信登陆,好像也是微信最近才放出来的接口.还需要申请才能有权限实现授权. ...
- 微信H5授权登录,公众平台,开放平台
首先,特别不喜欢做微信开发,各种设置,各种文档,各种坑. 最近做一个H5网页,微信扫码打开,需要使用微信登录,获取用户的基本信息,自动保存,自动登录. 1.先去微信公众平台https://mp.wei ...
- laravel 5.6接入微信第三方授权登陆的主要步骤
https://yq.aliyun.com/articles/590435 摘要: 这方面,php已很成熟了, 综合下面这个链接,基本上调试一下就可以搞定了. 这方面,php已很成熟了, 综合下面这个 ...
- ASP微信服务号H5客户登陆,且获取客户授权的用户基本信息
ASP微信服务号H5客户登陆,且获取客户授权的用户基本信息是需要客户授权,下面讲解详细步骤: 第一步:客户点击登录页,自动跳转到微信服务器端获取code 第二步:用第一步获取的code去获取客户的ac ...
- 微信授权登陆接入第三方App(步骤总结)Android
微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录
- 一个基于thinkphp的微信授权登陆功能
共享一份基于thinkphp开发的用户授权登陆的功能代码,本实例使用thinkphp的第三方微信公众平台PHP-SDK,地址https://github.com/dodgepudding/wechat ...
- 微信小程序开发 - 用户授权登陆
准备:微信开发者工具下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 微信小程序开发文档:htt ...
- 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载
如下图所示: 微信h5支付“网站域名ICP备案主体与商户号主体不一致”: 需提交H5微信支付 授权函 下载地址:https://download.csdn.net/download/a72400815 ...
- 服务号使用微信网页授权(H5应用等)
获取授权准备 AppId 服务号已经认证且获取到响应接口权限 设置网页授权域名 公众号设置 - 功能设置 - 网页授权域名.注意事项: 回调页面域名或路径需使用字母.数字及"-"的 ...
随机推荐
- 在三台服务器,搭建redis三主三从集群
一.资源准备 1.准备三台服务器H1.H2.H3 172.26.237.83 H1 172.26.237.84 H2 172.26.237.85 H3 二.配置服务器 1.在H1服务器设置SSH免密登 ...
- KeepLived + nginx 高可用
. 环境准备 1. VMware; 2. 4台CentOs7虚拟主机:192.168.122.217, 192.168.122.165 3. 系统服务:LVS, Keepalived 4. Web服务 ...
- 搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
http://blog.chinaunix.net/uid-20639775-id-154497.html
- 自己写的一些Delphi常用函数
今天在整理以前写过的代码,发现有些函数还是挺实用的,决定将其贴到Blog上,与众多好友一起分享.{*************************************************** ...
- Template within template: why “`>>' should be `> >' within a nested template argument list” 解决方法
如果直接这样写: std::vector<boost::shared_ptr<int>> intvec; gcc编译器会把">>"当成opera ...
- Comet OJ - Contest #4 B题 奇偶性
题目链接:https://www.cometoj.com/contest/39/problem/B?problem_id=1577 题意:给你一个数列,求L 到 R 区间内 所有数列 (ƒn mod ...
- 5、cesium点击面高亮事件
cesium点击面高亮事件 主要功能:比如你加载了json.geojson或者topojson的数据.加载出来后,分为很多个面,现在要实现点击一个面,这个面变颜色:再点击另一个面,另一个面高亮,之前的 ...
- 进程共享变量#pragma data_seg用法
#pragma data_seg介绍 用#pragma data_seg建立一个新的数据段并定义共享数据,其具体格式为: #pragma data_seg ("shareddata" ...
- 0 ‘+new Array(017)’ 输出? js+相当于Number()类型转换
网站搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/20/new-array017-%E8%BE%93%E5%87%BA%EF%BC%9F- ...
- Java多线程sleep和wait的区别,总结得非常好。
我们都知道sleep是让线程休眠,到时间后会继续执行,wait是等待,需要唤醒再继续执行,那么这两种方法在多线程中的表现形态,它们各有什么区别呢? 可以总结为以下几点. 使用上 从使用角度看,slee ...