微信H5授权登陆

最后都变了- 提交于 2019-12-05 13:45:59
Controllerpackage 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=wx89c30d0db583ae38&secret=87629847f9f55684d37588bc629c5a12&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;        }    }}
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!