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. bypass_safedog

    1.SQL注入 手工bypass要点 先通过破坏关键字测试出拦截规则 之后进行针对性绕过 1.Mysql 1.1.联合注入 0x01 and绕过 直接 and 1=1 直接就会被拦截 在数值的前面加特 ...

  2. Sqlserver复杂查询

    --联表修改 update xyzrb set xyzrb.xy_card=tablsb.card from xyzrb left join tablsb on xyzrb.xybh=tablsb.x ...

  3. ubuntu 下gcc的编译运行

    一些基本的操作 $gcc test.c //将test.c预处理.汇编.编译并链接形成可执行文件test $gcc test.c -o test //-o用来指定输出文件的文件名 $gcc -E te ...

  4. noip历年试题

      noip2018 铺设道路 货币系统 赛道修建 一眼贪心.随便实现. 旅行 环套树枚举删除环上哪条边. 填数游戏 找规律,这谁会啊. 保卫王国 动态Dp,去问这位神仙.   noip2017 小凯 ...

  5. 获取客户端IP地址-----以及--------线上开启redis扩展

    /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */ function get_cl ...

  6. vbs 之 excel 使用VBScript 操作excel

    打开excel及新建工作薄 '' 2. Method ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 2.1 CreateO ...

  7. Asia Hong Kong Regional Contest 2019

    A. Axis of Symmetry B. Binary Tree n 的奇偶性决定胜负. C. Constructing Ranches 路径上点权之和大于,极大值两倍,这是路径上点能拼出多边形的 ...

  8. springcloud中config启动时候报错Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"

    -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jm ...

  9. JS的面向对象与原型

    原型 const yoshi = { skulk: true }; const hattori = { sneak: true }; const kuma = { creep: true }; ⇽-- ...

  10. Linux fork创建子进程

    1.  pid_t fork(void); 功能:创建父子进程 参数:无 返回值:成功:在父进程中:返回值为子进程的PID 在子进程中:返回值为0 失败:-1 注意: 1)fork函数是用来创建进程的 ...