前提:配置好域名,在公众号配置

一.引用jar包,在pom.xml文件加入依赖

<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.7.0</version>
</dependency>

  

二.在application.yml 加入配置

wechat:
mpAppId: wxd898fcb01713c658
mpAppSecret: 47ccc303338cee6e62894fxxxxxxxxxxx

  mpAppId ,mpAppSecret可以从公众号上取到

三.账户属性值注入

新建一个WechatAccountConfig.java类

package cn.edu.jxnu.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import java.util.Map; /**
* 微信账号配置 读取属性文件值
*
* @author yux
* @version V1.0
* @time 2018年4月13日
*/
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig { /**
* 公众平台id
*/
private String mpAppId; /**
* 公众平台密钥
*/
private String mpAppSecret; }

  

四.微信公众号配置

package cn.edu.jxnu.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; /**
* 微信公众平台配置
*
* @author yux
* @version V1.0
* @time 2018年4月13日
*/
@Component
public class WechatMpConfig { @Autowired
private WechatAccountConfig accountConfig; /**
* 微信公众号服务层 bean注册
*
* @time 下午6:08:13
* @version V1.0
* @return WxMpService
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
} /**
* 微信公众号配置 bean注册
*
* @time 下午6:08:41
* @version V1.0
* @return WxMpConfigStorage
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
// 设置开发者的id和密钥
wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}

  

五.控制器

package cn.edu.jxnu.controller;

import java.net.URLEncoder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import cn.edu.jxnu.config.ProjectUrlConfig;
import cn.edu.jxnu.enums.ResultEnum;
import cn.edu.jxnu.exception.SellException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; /**
* 微信授权,使用github的微信sdk
*
* @author yx
* @version V1.0
* @time 2018年4月17日
*/
@Controller // 需要重定向的时候不能使用RestController
@RequestMapping("/wechat")
@Slf4j
public class WechatController { @Autowired
private WxMpService wxMpService; //@Autowired
//private WxMpService wxOpenService; /**第一步:请求CODE,必要参数return
* 此方法实现请求授权
* @time 下午6:17:37
* @version V1.0
* @param returnUrl
* @return 重定向 string
*/
@SuppressWarnings("deprecation")
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl) {
// 1. 配置
// 2. 调用方法
String url = /域名/+/项目名/wechat/userInfo";
// OAUTH2_SCOPE_BASE 默认直接授权
String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE,
URLEncoder.encode(returnUrl));// 重定向到回调接口地址 redirectUrl,必须编码url
log.info("授权:{}", redirectUrl);
return "redirect:" + redirectUrl;
} /**第二步:通过code获取access_token
* 上面的authorize方法重定向到这个方法获取用户信息
* 用户允许授权后,将会重定向到redirect_uri的网址上,并且带上code和state参数
* @time 下午6:17:59
* @version V1.0
* @param code
* @param returnUrl
* @return 重定向 string
*/
@GetMapping("/userInfo")
public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) {
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
try { //通过code获取access_token
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
log.error("【微信网页授权】{}", e);
// 继续抛出
throw Exception();
}
// 拿到openid 到这一步重点已经完成
String openId = wxMpOAuth2AccessToken.getOpenId(); log.info("获得openid:{}", openId);
// 这个接口前端和后端的开发文档规定的,视情况而定
return "redirect:" + returnUrl + "?openid=" + openId;
}
}

  

总结:

1、配置开发者id和密钥

2、设置微信回调接口,并在项目中设置,注入

3、注册微信授权bean【 WxMpConfigStorage, WxMpService】 前者是设置配置文件,后者是服务,里面有授权封装

4、编写控制器,

第一步:请求CODE   【authoeize方法】

第二步:通过code获取access_token  【userInfo方法】

第三步:通过access_token调用接口[这一步具体情况看项目]

(十四)SpringBoot开发微信授权支付的更多相关文章

  1. php开发微信APP支付接口

    之前在开发APP中用到了微信支付,因为是第一次用,所以中途也遇到了好多问题,通过查看文档和搜集资料,终于完成了该功能的实现.在这里简单分享一下后台php接口的开发实例. 原文地址:代码汇个人博客 ht ...

  2. Vue/小程序/小程序云+Node+Mongo开发微信授权、支付和分享

    大家好,我是河畔一角,今天给大家介绍我的第三门实战课程:基于微信开发的H5.小程序和小程序云的授权.支付和分享专项课程. 一.这一次为什么会选择微信支付和分享的课题呢? 金庸的小说中曾提到:有人的地方 ...

  3. 微信公众平台开发——微信授权登录(OAuth2.0)

    1.OAuth2.0简介 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户 ...

  4. html 微信开发——微信授权

    微信JS-SDK说明文档 链接地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 微信web开发:http: ...

  5. 【第二十篇】C#微信H5支付 非微信内浏览器H5支付 浏览器微信支付

    微信开发者文档 微信H5支付官方文档   请阅读清楚  最起码把所有参数看一遍 这个地方也可以看看 微信案例 http://wxpay.wxutil.com/mch/pay/h5.v2.php,请在微 ...

  6. 微信小程序开发问答《五十四》同步请求授权 & 用户拒绝授权,重新调起授权 ... ...

    1.同步请求授权 需求分析: 1.在小程序首次打开的时候,我需要同时请求获取多个权限,由用户逐一授权. (['scope.userInfo','scope.userLocation','scope.a ...

  7. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十四║ Vuex + JWT 实现授权验证登录

    壹周回顾 哈喽,又是元气满满的一个周一,又与大家见面了,周末就是团圆节了,正好咱们的前后端也要团圆了,为什么这么说呢,因为以后的开发可能就需要前后端一起了,两边也终于会师了,还有几天Vue系列就基本告 ...

  8. (转)php-curl响应慢(开发微信授权登陆时碰到的问题)

    最近在做一个php小项目的时候,发现curl调用微信的授权api.weixin.qq.com,经常是需要等待很久,但是有时候却很快. 刚开始以为是网络慢问题,没去注意.后面通过打上时间日志观察发现,慢 ...

  9. 开发微信App支付

    1.首先到官方下载Demo,地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1 下载后的目录结构如下:

随机推荐

  1. vim实现代码缩进和可视区域的字符串替换

    今天2014年9月12号,实现了vim下的代码自动缩进和替换可视区域的字符串,之前一直在用vim这个强大的编辑器,它的强大只有用了的人才知道,现在把这两个很强大的功能展示出来,有个这两个功能,即使你写 ...

  2. SIP学习笔记 -- RFC 3261

    1.SDP (rfc 4566)    1)用于交换参数    2)内容分三部分Session description, Time description and Media description ...

  3. leetcode 792. Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  4. Python序列——序列操作

    Python中的序列包括,字符串.列表.元组.本文介绍序列的通用操作. 1. 切片中的None >>> s = 'abcdefg' >>> for i in ran ...

  5. Emscripten实现把C/C++文件转成wasm,wast(wasm的可读形式),llvm字节码(bc格式),ll格式(llvm字节码的可读形式)并执行wasm

    <一>˙转换 Emscripten实现把C/C++文件转成wasm,wast(wasm的可读形式),llvm字节码(bc格式),ll格式(llvm字节码的可读形式)的步骤: 最新版本的Em ...

  6. hdu Integer Inquiry 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1047 题目意思:就是求大整数加法.有多个案例,每个案例之间要输出一个空格. #include < ...

  7. 关于使用response.addHeader下载中文名乱码问题

    介绍下我项目中遇到的问题:在数据库导出Excel文件的过程中,导出文件中文名始终异常,最终结果发现需要在response.addHeader 中的 filename = "xxxx" ...

  8. Linux-Nginx和NFS

    1 虚拟化 查看系统信息 cat /proc/meninfo cat /proc/cpuinfo 其中 flags里面的信息可以查看该cpu是否支持虚拟化 flags上有vmx svm等表示可以虚拟化 ...

  9. 51Nod - 1295:XOR key (可持久化Trie求区间最大异或)

    给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求ALL 至 ARR 这R - L + 1个数中,与X 进行异或运算(Xor),得到的最大值 ...

  10. codevs 3269 混合背包(复习混合背包)

    传送门 [题目大意]给出物品的数量.-1为无限个. [思路]混合背包.... [code] #include<iostream> #include<cstdio> #inclu ...