解决微信公众号网页授权域名2个的限制
<h3>问题</h3><p>微信进行网页授权时,需要填写授权域名,授权域名只有两个, 但是实际上可能有多个。不利于开发调试,每次都要修改,并且要发布到那个域名底下,相当麻烦</p>
<h3>思路</h3>
<p>准备中间代理域名agent.example<br>微信公众号网页授权上填这个代理域名 agent.example<br>所有 需要微信网页授权的客户端页面都向 agent.example 请求<br>然后由 agent.example 统一向 微信服务器进行发起网页授权请求<br>微信服务器带着code 重定向 agent.example<br>agent.example 将code 拼接真实客户端需要授权页面的地址 为 url<br>agent.example 重定向 url 到客户端<br>客户端可以通过 url 拿到 微信授权过的 code</p>
<h3>流程图</h3>
<p> </p>
<p><img src="https://img2022.cnblogs.com/blog/1327924/202209/1327924-20220908110343072-2053361869.png"></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<h3>代码实现</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.slf4j.Logger;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.slf4j.LoggerFactory;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.web.bind.annotation.GetMapping;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.web.bind.annotation.RequestMapping;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> org.springframework.web.bind.annotation.RestController;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> javax.servlet.http.HttpServletResponse;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.io.IOException;
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> java.text.MessageFormat;
@RestController
@RequestMapping(</span>"/h5"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> WechatAgentController {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Logger logger = LoggerFactory.getLogger(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.getClass());
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 微信网页授权地址, 需要人工点击确认的那种
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String AUTH_URL = "https://open.weixin.qq.com/connect/oauth2/authorize"
+ "?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"<span style="color: rgba(0, 0, 0, 1)">;<br><br> /** * 代理授权域名,换成你自己真实的公网域名!! */
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String DEFAULT_AUTH_HOST = "https://xxxx.com"<span style="color: rgba(0, 0, 0, 1)">;</span>
<span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> String CODE_URL = "retail/h5/wx/code?returnUrl={0}"<span style="color: rgba(0, 0, 0, 1)">;<br>
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 授权
*
* </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> redirectUrl 目标地址--授权完成之后的回调地址
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@NoToken
@GetMapping(</span>"/wx/oauth2"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> openAuth(String appId, String redirectUrl,
HttpServletResponse response) </span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
String middleUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> DEFAULT_AUTH_HOST;
middleUrl </span>= middleUrl.concat("/"<span style="color: rgba(0, 0, 0, 1)">);
middleUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> middleUrl.concat(CODE_URL);
String tmpUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> MessageFormat.format(middleUrl, redirectUrl);
logger.warn(</span>"tmpUrl"+<span style="color: rgba(0, 0, 0, 1)">tmpUrl);
System.out.println(tmpUrl);
String realRedirectUrl </span>=<span style="color: rgba(0, 0, 0, 1)"> MessageFormat.format(AUTH_URL, appId, tmpUrl);
logger.warn(</span>"realRedirectUrl:"+<span style="color: rgba(0, 0, 0, 1)">realRedirectUrl);
System.out.println(realRedirectUrl);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">重定向到 /wx/code 请求</span>
<span style="color: rgba(0, 0, 0, 1)"> response.sendRedirect(realRedirectUrl);
}
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 获取 code
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@NoToken
@GetMapping(</span>"/wx/code"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> code(String code, String returnUrl,
HttpServletResponse response) </span><span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
String redirectUrl;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (returnUrl.contains("?"<span style="color: rgba(0, 0, 0, 1)">)) {
redirectUrl </span>= returnUrl.concat("&code="<span style="color: rgba(0, 0, 0, 1)">).concat(code);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
redirectUrl </span>= returnUrl.concat("?code="<span style="color: rgba(0, 0, 0, 1)">).concat(code);
}
logger.warn(</span>"redirectUrl:"+<span style="color: rgba(0, 0, 0, 1)">redirectUrl);
response.sendRedirect(redirectUrl);
}
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* 在微信公众号后台配置域名可跳过文件验证
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
@GetMapping(</span>"/MP_verifxxxxxxxxxxxx.txt"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String txt() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "xxxxxxxxxx"<span style="color: rgba(0, 0, 0, 1)">;
}
}</span></pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
hello world!!!<br><br>
来源:https://www.cnblogs.com/wang-yaz/p/16668783.html
頁:
[1]