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

一.引用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. 2018-11-13-常用模块1 (time random os sys)

    1.时间模块 time 2.随机数模块 random 3.与操作系统交互模块 os 4.系统模块 sys 在我们真正开始学习之前我们先解决下面几个问题,打好学习模块的小基础,以便更好的学习模块. (1 ...

  2. 谷歌浏览器使用SelectorGadget和Xpath Helper获取xpath和css path

    在上篇文章里,介绍了如何在火狐浏览器中获取网页元素的xpath和css path. 这篇文章将介绍,在谷歌浏览器中使用SelectorGadget和Xpath Helper实现同样功能. 这两个谷歌浏 ...

  3. appium()-The event firing

    原文地址:https://github.com/appium/java-client/blob/master/docs/The-event_firing.md since 4.1.0 The purp ...

  4. hihocoder 挑战赛9 A.好配对(思维题目 防止超时)

    #1123 : 好配对 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个序列a和b,每个序列中可能含有重复的数字. 一个配对(i,j)是一个好配对当从第一个序列中选 ...

  5. LightOJ1214 Large Division —— 大数求模

    题目链接:https://vjudge.net/problem/LightOJ-1214 1214 - Large Division    PDF (English) Statistics Forum ...

  6. HDU3440 House Man (差分约束)

    In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. To ...

  7. 出现”/var/lib/mysql/mysql.sock“不存在的解决方法

    这种情况大多数是因为你的mysql是使用rpm方式安装的,它会自动寻找 /var/lib/mysql/mysql.sock 这个文件,通过unix socket登录mysql.常见解决办法如下:1.创 ...

  8. P4147玉蟾宫——最大子矩阵

    悬线法裸题. 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace ...

  9. Array 对象

    Array的对象用于在单个的变量中存储多个值. constructor 返回对创建此对象的数组函数的引用. demo: let arr=[];  arr.constructor==Array let ...

  10. dumpbin检查Dll

    用dumpbin.exe工具查看DLL,dumpbin.exe是VS自带的工具.版本VS2013,路径是:G:\VS2013\VC\bin\amd64\ 可以看到dumpbin.exe. 使用VS里的 ...