展開
步驟:
一、內網穿透申請二級域名(有服務器和域名者可略過)
二、申請微信公眾平臺測試號(有已認證的微信服務號者可略過)
三、搭建 Spring Boot 2.0 項目實現獲取openId
一、內網穿透:
因為要直接用內網本機開發調試,微信網頁授權在回調時要訪問本機,所以直接做個內網穿透,可以直接在外網訪問到本機,做法如下:
1、登錄 https://natapp.cn/ (我用的natapp.cn,你可以用其他類似的,個人感覺這個不錯)
2、購買隧道:
購買前需要認證一下,不要用免費的,因為免費的是隨機分配域名的,每次都會變,之前VIP_1型是5元,現在漲到9元了,自行選擇(官方9折優惠碼709ABD4F),我買的是VIP_2型,購買後如下圖:
購買後使用方式:
https://natapp.cn/article/natapp_newbie
使用後會得到natapp分配的網址,如 xxx.natapp.cn,這個地址就可以訪問到開發本機。
微信公眾平臺測試申請地址:
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
打開後點擊“登錄”出來個二維碼,直接微信掃一掃即可,登錄後如下:
上面的appID 和 appsecret 就是後面我們要用到的。然後掃下面的測試號二維碼關注:
上面的 xxx.natapp.cn 填寫你上面獲取到的域名
三、搭建 Spring Boot 2.0 項目
IDEA新建項目:
為方便,選了 Lombok
搭建後如下:
pom.xml (依賴了 weixin-java-mp 2.9.0)
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.wechat
auth
0.0.1-SNAPSHOT
jar
auth
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
com.github.binarywang
weixin-java-mp
2.9.0
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.yml (原為 application.properties,直接改為 .yml,簡潔方便)
裡面兩個值就填寫上面獲取到的 mpAppId 和 mpAppSecret
wechat:
mpAppId: xxxxxxxxxxxx
mpAppSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxx
WechatAccountConfig.java (將上面的配置轉換為Bean)
package com.wechat.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
private String mpAppId;
private String mpAppSecret;
}
WeChatMpConfig.java (配置WxMpService Bean)
package com.wechat.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;
@Component
public class WeChatMpConfig {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Bean
public WxMpService wxMpService(){
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
@Bean
public WxMpConfigStorage wxMpConfigStorage(){
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
return wxMpConfigStorage;
}
}
WechatController.java (將xxx.natapp.cn改為用natapp獲取到的域名)
package com.wechat.controller;
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;
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 java.net.URLEncoder;
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {
@Autowired
private WxMpService wxMpService;
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl){
String url = "http://xxx.natapp.cn/wechat/userInfo";
String redirectURL = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
log.info("【微信網頁授權】獲取code,redirectURL={}", redirectURL);
return "redirect:" + redirectURL;
}
@GetMapping("/userInfo")
public String userInfo(@RequestParam("code") String code,
@RequestParam("state") String returnUrl) throws Exception {
log.info("【微信網頁授權】state={}", returnUrl);
WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
try {
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
} catch (WxErrorException e) {
throw new Exception(e.getError().getErrorMsg());
}
String openId = wxMpOAuth2AccessToken.getOpenId();
log.info("【微信網頁授權】openId={}", openId);
return "redirect:" + returnUrl + "?openid=" + openId;
}
}
啟動Spring Boot 工程:
用微信訪問
xxx.natapp.cn/wechat/authorize?returnUrl='回調的地址' 即可看到控制檯打印出了 openId,回調後操作自己的業務即可。
應要求放到Gitee上了:
https://gitee.com/antma/SpringBootGetOpenId.git