解决,微信网页开发,网页授权域名数量不足问题
背景:相信很多開發(fā)微信公眾號的小伙伴都碰到過這個問題,微信限制我們配置網(wǎng)頁授權域名數(shù)量,但是大部分團隊肯定不止一個業(yè)務需要微信網(wǎng)頁登錄的,我們這里可以利用nginx作為代理,實現(xiàn)只配置一個授權域名,完成團隊各種業(yè)務的微信網(wǎng)頁登錄。
配置流程:
? ? ? ? 第一步:設置解析一個二級域名專門用于微信網(wǎng)頁授權。
????????第二步:解析到的服務器安裝并配置nginx,詳細配置如下:
#############微信授權接口#######################server{listen 80;#該域名就是微信公眾號上配置的網(wǎng)頁授權域名server_name wx.aouth.master.com;location / {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#如果是測試環(huán)境,則重定向到內(nèi)網(wǎng)穿透的域名if ( $arg_evn = test) {rewrite ^/(.*)$ http://$arg_pix.test.frp.master.net/$1 permanent;break;}#如果是生產(chǎn)環(huán)境則重定向到生產(chǎn)環(huán)境的h5地址域名if ( $arg_evn = prod){rewrite ^/(.*)$ https://$arg_pix.h5.master.com/$1 permanent;break;}}#配置微信的域名驗證文件location =/MP_verify_3wrrmjc6T88dAWW5.txt {root /usr/local/nginx/html/h5;}}? 其中:wx.aouth.master.com就是筆者的微信網(wǎng)頁授權域名,在微信公眾號上配置這個即可,由于筆者的團隊本地都用FRP軟件做了內(nèi)網(wǎng)穿透,所以在nginx中再做一個環(huán)境識別,測試環(huán)境則跳轉(zhuǎn)他們各自的內(nèi)網(wǎng)穿透域名,生產(chǎn)環(huán)境跳轉(zhuǎn)各自業(yè)務域名。
參數(shù):evn,用于識別環(huán)境;
參數(shù):pix,用于設置不同的域名前綴;
這倆個參數(shù)都是在生成鏈接時候在redirect_uri的參數(shù)中指定的。
第三步:生成網(wǎng)頁授權鏈接
由于生成鏈接中的redirect_uri需要做UrlCode,筆者貼一個編碼之前和編碼之后的,方便對比。
編碼之前的地址:
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx123456789a&redirect_uri=http://wx.aouth.master.com/views/open/wx/oauth/?evn=test&pix=master&response_type=code&scope=snsapi_base&state=test#wechat_redirect";? ?編碼之后地址:
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx123456789a&redirect_uri=http%3A%2F%2Fwx.aouth.master.com%2Fviews%2Fopen%2Fwx%2Foauth%2F%3Fevn%3Dtest%26pix%3Dmaster&response_type=code&scope=snsapi_base&state=test#wechat_redirect"?第四步:配置完成
配置完成,筆者這里是后端生成這個鏈接給前端用,這里貼上后端生成連接的代碼:
/*** 獲取公眾號網(wǎng)頁授權鏈接* @param id 自動注入客戶ID* @param backPath 跳回小程序的頁面PATH* @return*/@ApiOperation(value = "獲取公眾號網(wǎng)頁授權鏈接",notes = "返回一個鏈接地址,請使用web-view組件在微信內(nèi)打開<br>待處理完成后跳轉(zhuǎn)到小程序指定的頁面")@GetMapping(value = "/courier/gzh/oauth/url")public APIEntity<WeChatJsConfig> getGzhOauthUrl(@RequestAttribute("JwtUserId") Integer id,@ApiParam(value = "跳回小程序的頁面路徑", required = true,example = "/pages/index/index") @RequestParam String backPath) {String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid={}&redirect_uri={}&response_type=code&scope=snsapi_base&state=test#wechat_redirect";String redirectUri;try {redirectUri= URLEncoder.encode(StrUtil.format("http://wx.aouth.master.com/views/open/wx/oauth/?backPath={}&id={}&evn=prod&pix=share",backPath,id),"UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();return APIEntity.fail("地址解碼失敗");}return APIEntity.success(StrUtil.format(url,WxApp.GZH_TCJS.getAppId(),redirectUri));}筆者這里的業(yè)務是小程序打開網(wǎng)頁完成授權登錄后再跳回小程序,所以redirect_uri還加了一些其他業(yè)務上的參數(shù),小伙伴們可以根據(jù)自己的情況增減。
總結(jié)
以上是生活随笔為你收集整理的解决,微信网页开发,网页授权域名数量不足问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: crm订单管理系统有哪些?
- 下一篇: 22 信息系统安全管理