1. 程式人生 > >(十四)SpringBoot開發微信授權支付

(十四)SpringBoot開發微信授權支付

context catch xxx warnings () 情況 warn ack component

前提:配置好域名,在公眾號配置

一.引用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開發微信授權支付