(十四)SpringBoot开发微信授权支付

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

一.引用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调用接口[这一步具体情况看项目]

原文地址:https://www.cnblogs.com/yui66/p/9646982.html