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授权登陆的更多相关文章

  1. 微信登陆,微信SDK授权登陆经验分享

    From:http://www.eoeandroid.com/thread-547012-1-1.html 最近因为项目需要做了微信登陆,好像也是微信最近才放出来的接口.还需要申请才能有权限实现授权. ...

  2. 微信H5授权登录,公众平台,开放平台

    首先,特别不喜欢做微信开发,各种设置,各种文档,各种坑. 最近做一个H5网页,微信扫码打开,需要使用微信登录,获取用户的基本信息,自动保存,自动登录. 1.先去微信公众平台https://mp.wei ...

  3. laravel 5.6接入微信第三方授权登陆的主要步骤

    https://yq.aliyun.com/articles/590435 摘要: 这方面,php已很成熟了, 综合下面这个链接,基本上调试一下就可以搞定了. 这方面,php已很成熟了, 综合下面这个 ...

  4. ASP微信服务号H5客户登陆,且获取客户授权的用户基本信息

    ASP微信服务号H5客户登陆,且获取客户授权的用户基本信息是需要客户授权,下面讲解详细步骤: 第一步:客户点击登录页,自动跳转到微信服务器端获取code 第二步:用第一步获取的code去获取客户的ac ...

  5. 微信授权登陆接入第三方App(步骤总结)Android

    微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录

  6. 一个基于thinkphp的微信授权登陆功能

    共享一份基于thinkphp开发的用户授权登陆的功能代码,本实例使用thinkphp的第三方微信公众平台PHP-SDK,地址https://github.com/dodgepudding/wechat ...

  7. 微信小程序开发 - 用户授权登陆

    准备:微信开发者工具下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 微信小程序开发文档:htt ...

  8. 微信h5支付“网站域名ICP备案主体与商户号主体不一致”的解决方法,H5微信支付 授权函下载

    如下图所示: 微信h5支付“网站域名ICP备案主体与商户号主体不一致”: 需提交H5微信支付 授权函 下载地址:https://download.csdn.net/download/a72400815 ...

  9. 服务号使用微信网页授权(H5应用等)

    获取授权准备 AppId 服务号已经认证且获取到响应接口权限 设置网页授权域名 公众号设置 - 功能设置 - 网页授权域名.注意事项: 回调页面域名或路径需使用字母.数字及"-"的 ...

随机推荐

  1. Spring Boot 集成Mybatis实现多数据源

    静态的方式 我们以两套配置方式为例,在项目中有两套配置文件,两套mapper,两套SqlSessionFactory,各自处理各自的业务,这个两套mapper都可以进行增删改查的操作,在这两个主MYS ...

  2. SQL LIKE 运算符

    SQL LIKE 运算符 在WHERE子句中使用LIKE运算符来搜索列中的指定模式. SQL LIKE 操作符 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式. 有两个通配符与LIKE运 ...

  3. docker哪些平台技术(3)

    容器平台技术 容器核心技术使得容器能够在单个 host 上运行.而容器平台技术能够让容器作为集群在分布式环境中运行. 容器平台技术包括容器编排引擎.容器管理平台和基于容器的 PaaS. 容器编排引擎 ...

  4. hive调用MapReduce之后遇到kill command之后卡住或者一直开在MapReduce之前

    https://blog.csdn.net/weixin_42158422/article/details/88876943

  5. PAT_A1048#Find Coins

    Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...

  6. csv 基本操作, 报错解决(UnicodeEncodeError: 'utf-8' codec can't encode characters in position 232-233: surrogates not allowed)

    最常用的一种方法,利用pandas包 import pandas as pd #任意的多组列表 a = [1,2,3] b = [4,5,6] #字典中的key值即为csv中列名 dataframe ...

  7. wordpress 上传图片时提示“无法建立目录wp-content/uploads/2019/03。有没有上级目录的写权限?”

    查一下网站目录下wp-content目录的权限, # ls -l drwxr-xr-x  5 nobody 65534  4096 Feb  3  2016 wp-content 修改wp-conte ...

  8. CSS3:FlexBox的详解

    Flexbox是Flexible box 的简称(灵活的盒子容器),是CSS3引入的新的布局模式.它决定了元素如何在页面上排列,使它们能在不同的屏幕尺寸和设备下可预测地展现出来. 它之所以被称为 Fl ...

  9. <随便写>数据库调优的几种方式

    1.创建索引 要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引 在经常需要进行检索的字段上创建索引,比如要按照表字段username进行检索,那么就应该在姓名字段 ...

  10. canvas 画一条折线

    设置画布对象 canvas id="myCanvas" ref="canvas" //获取Canvas对象(画布) var canvas = document. ...