四 SoringCloud -微信獲取用戶信息

1、項目介紹

四 SoringCloud -微信獲取用戶信息

文章插圖
2、微信公眾平臺 和 微信開放文檔2.1 微信公眾平臺2.1.1 網址鏈接https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
2.1.2 測試號信息
四 SoringCloud -微信獲取用戶信息

文章插圖
2.1.3 微信掃描關注測試公眾號
四 SoringCloud -微信獲取用戶信息

文章插圖
2.1.4 授權回調頁面域名2.1.4.1 網頁服務->網頁賬號->修改
四 SoringCloud -微信獲取用戶信息

文章插圖
2.1.4.2 填寫 授權回調頁面域名
四 SoringCloud -微信獲取用戶信息

文章插圖
2.1.4.3 內網穿透 NATAPP2.1.4.3.1 使用教程NATAPP1分鐘快速新手圖文教程: https://natapp.cn/article/natapp_newbie下載: https://natapp.cn/#download使用本地配置文件config.ini: https://natapp.cn/article/config_ini2.1.4.3.2 authtoken
四 SoringCloud -微信獲取用戶信息

文章插圖
2.1.4.3.3授權回調頁面域名
四 SoringCloud -微信獲取用戶信息

文章插圖
2.2微信開放文檔2.2.1 網址鏈接https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#2
2.2.2 官方 基本步驟教程1 第一步:用戶同意授權,獲取code
2 第二步:通過 code 換取網頁授權access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用戶信息(需 scope 為 snsapi_userinfo)
5 附:檢驗授權憑證(access_token)是否有效
四 SoringCloud -微信獲取用戶信息

文章插圖
3、http請求工具類 HttpClient4UtilHttpClient4Util 用來發http請求;
https://www.cnblogs.com/xiaoqigui/p/16839536.html
4、配置文件 和 配置類4.1 配置文件application.yml
#端口server:port: 8096# 自定義微信授權信息wechat:auth:app-id: wxd4e20add67******#appIDapp-secret: a21e97d21d0d6ce408b7a6c******# appsecretcode-uri: https://open.weixin.qq.com/connect/oauth2/authorize# 請求微信官方獲取用戶授權code 的請求地址redirect-uri: http://******.natappfree.cc/wechat/auth/codeBack # 微信官方返回用戶授權code 的回調地址access-token-uri: https://api.weixin.qq.com/sns/oauth2/access_token # 根據微信回調的code值,請求微信官方獲取用戶access_token的請求地址user-info-uri: https://api.weixin.qq.com/sns/userinfo #根據用戶的 accessToken 和openId 拉取用戶信息的請求地址4.2 配置類//自定義微信授權參數信息配置類@Data@Component@ConfigurationProperties(prefix = "wechat.auth")public class WeChatAuthConfig {/*應用id*/private String appId;/*應用密鑰*/private String appSecret;/*請求獲取code的地址*/private String codeUri;/*微信官方回調code的地址*/private String redirectUri;/*** 微信官方獲取access_token地址*/private String accessTokenUri;/*微信官方獲取userInfo地址*/private String userInfoUri;}5、server 層5.1 接口/** * Created On : 28/10/2022. * <p> * Author : huayu * <p> * Description: 微信授權的業務接口 */public interface WeChatAuthService {/*** @author : huayu* @date: 28/10/2022* @param: []* @return : java.lang.String* @description : 生成請求微信官方獲取用戶授權code的請求地址*/String generateWeChatAuthCodeUrl();/*** @author : huayu* @date: 28/10/2022* @param: [wechatAuthCode]* @return : java.lang.String* @description : 根據微信回調的code值,請求微信官方獲取用戶access_token*/String getAccessTokenFromWechatUseCode(String wechatAuthCode);/*** @author : huayu* @date: 28/10/2022* @param: [accessToken, openId]* @return : java.lang.String* @description : 根據用戶的 accessToken 和openId 拉取用戶信息*/String getUserInfoFromWechatUseAccessToken(String accessToken,String openId);}5.2 實現類/** * Created On : 28/10/2022. * <p> * Author : huayu * <p> * Description: 微信授權的業務接口 實現類 */@Service@Slf4jpublic class WeChatAuthServiceImpl implements WeChatAuthService{@Autowiredprivate WeChatAuthConfig weChatAuthConfig;/*** @author : huayu* @date: 29/10/2022* @param: []* @return : java.lang.String* @description : 生成請求微信官方獲取用戶授權code的請求地址*/@Overridepublic String generateWeChatAuthCodeUrl() {//微信官方引導用戶打開授權頁面,獲取code的完整路徑//https://open.weixin.qq.com/connect/oauth2/authorize// ?appid=APPID// &redirect_uri=REDIRECT_URI// &response_type=code// &scope=SCOPE// &state=STATE// #wechat_redirect//尤其注意:由于授權操作安全等級較高,所以在發起授權請求時,微信會對授權鏈接做正則強匹配校驗,如果鏈接的參數順序不對,授權頁面將無法正常訪問//生成請求衛星官方獲取用戶code的完整地址StringBuilder weCharAuthCodeUrl = new StringBuilder(weChatAuthConfig.getCodeUri());weCharAuthCodeUrl.append("?appid=").append(weChatAuthConfig.getAppId()).append("&redirect_uri=").append(weChatAuthConfig.getRedirectUri()).append("&response_type=code")//&scope=snsapi_userinfo&state=STATE.append("&scope=").append("snsapi_userinfo").append("&state=").append("kh96_wechat_auth").append("#wechat_redirect");log.info("------ 請求微信官方授權網站地址:{}------",weCharAuthCodeUrl.toString());//返貨完整的請求地址return weCharAuthCodeUrl.toString();}/*** @author : huayu* @date: 29/10/2022* @param: [wechatAuthCode]* @return : java.lang.String* @description : 根據微信回調的code值 , 請求微信官方獲取用戶access_token*/@Overridepublic String getAccessTokenFromWechatUseCode(String wechatAuthCode) {// 尤其注意:由于公眾號的 secret 和獲取到的access_token安全級別都非常高,必須只保存在服務器,不允許傳給客戶端 。// 請求方法:獲取 code 后,請求以下鏈接獲取access_token:// https://api.weixin.qq.com/sns/oauth2/access_token// ?appid=APPID// &secret=SECRET// &code=CODE// &grant_type=authorization_code// 封裝根據code,請求微信官方獲取access_token的完整地址StringBuilder accessTokenUrl = new StringBuilder(weChatAuthConfig.getAccessTokenUri());accessTokenUrl.append("?appid=").append(weChatAuthConfig.getAppId()).append("&secret=").append(weChatAuthConfig.getAppSecret()).append("&code=").append(wechatAuthCode).append("&grant_type=authorization_code");log.info("------ 根據code , 請求微信官方獲取access_token的完整地址:{} ------", accessTokenUrl.toString());// 根據code,請求微信官方獲取access_token,返回結果是同步返回的,不再是異步回調// 請求是服務器內部發起的,也就是說:在程序中,要根據上面完整的請求地址 , 主動發送請求到微信官方,接口同步會返回一個json格式的字符串結果,程序內要解析獲取的結果// 程序內主動發起http請求,獲取access_tokenreturn HttpClient4Util.getResponse4GetAsString(accessTokenUrl.toString(), "utf-8");}/*** @author : huayu* @date: 29/10/2022* @param: [accessToken, openId]* @return : java.lang.String* @description : 根據用戶的 accessToken 和openId 拉取用戶信息*/@Overridepublic String getUserInfoFromWechatUseAccessToken(String accessToken, String openId) {// 如果網頁授權作用域為snsapi_userinfo , 則此時開發者可以通過access_token和 openid 拉取用戶信息了 。// http:GET(請使用 https 協議):// https://api.weixin.qq.com/sns/userinfo// ?access_token=ACCESS_TOKEN// &openid=OPENID// &lang=zh_CN// 封裝根據accessToken和openId,請求微信官方獲取用戶信息詳情地址StringBuilder userInfoUrl = new StringBuilder(weChatAuthConfig.getUserInfoUri());userInfoUrl.append("?access_token=").append(accessToken).append("&openid=").append(openId).append("&lang=zh_CN");log.info("------ 根據access_token , 請求微信官方獲取userinfo的完整地址:{} ------", userInfoUrl.toString());// 程序內主動發起http請求,獲取用戶詳情return HttpClient4Util.getResponse4GetAsString(userInfoUrl.toString(), "utf-8");}}

推薦閱讀