WeChat official account webpage authorization code optimization process (3)

In the last blog , the function of obtaining openid has been completed, but there are also problems left. In this article, we will improve it.

Changes to Version 1

In this version (version 2), we will use @ModelAttribute, a relatively rare annotation in SpringMVC. At least I have not used it before, nor have I paid much attention to it, but after a little understanding, I found that it was marked on the method. Equivalent to the @Before annotation in Junit, the method annotated with @Test will execute the method annotated with @Before before execution. Of course, I have a vague feeling that @ModelAttribute has other uses...

In version 2 we dropped the GotoController class and its name was too unfriendly. But we then introduce two new classes:

  • There is only one method in the WxOAuth2Controller class, beforeH5Page(), which is annotated by @ModelAttribute, so - they are inherited by the business controller, and our existing GoodsController will instead inherit WxOAuth2Controller. The principle is that when we request /goods/list, beforeH5Page() will be executed first. In this method, the work to be processed in the original GotoController#to() will be processed. It first determines whether there is an openid in the session, and if so, it will not be done. Processing, if not, read the link to be sent to WeChat authorization and authentication (the original redirect_uri in the link should point to the callback() method of WxRedirectController), read it out and save it in the session, and then call the WxRedirectController#toOAuth2() method to send the authorization and authentication request.
  • WxRedirectController
    • The method in this class has a method called toOAuth2(), which reads out the link in the session with WxOAuth2Controller#beforeH5Page() and redirects it to WeChat. After WeChat is processed, it will request the redirect_uri configured in WxOAuth2Controller#beforeH5Page(), and this path corresponds to the second method callback() of WxRedirectController.
  • The callback() method receives the code parameter, and requests WeChat to obtain the openid according to the code, and then puts the openid in the session.

The above may be a bit too much, and some people may consider why WxOAuth2Controller and WxRedirectController cannot be combined into one class. In reality, the WxOAuth2Controller class cannot be combined with the WxRedirectController class. When I first tried, I combined the two classes, resulting in callback( ) can never be executed, because if combined into a class, the beforeH5Page() method marked by @ModelAttribute will be executed before callback() is executed, resulting in an infinite loop, ^_^.

run, there is a problem

Also send "domain name/weixin-oauth-code-optimization-demo/goods/list" to the official account, and then click the link to find that the page is loaded multiple times (more than 2 times, the slower the network, the more obvious), see GoodsController#list( ) method, found that the openid was printed twice, the first time the openid was empty, the second time it was normal, we clicked the upper right corner to refresh in the pop-up layer, and found that only one openid was printed this time, and the problem in version 1 This is solved (you can review the problem left in version 1). The reason is also well explained. After the method marked by @ModelAttribute is executed, it will be executed down to the GoodsController#list() method. At this time, there is no openid in the session, so there is a null. We are here to demonstrate that there is no problem. Yes, but this openid is usually used in business to query the database. When the openid is empty, it is unnecessary to query the database.

Anyway, we took a step forward and solved a problem, although another problem has arisen... There will be improvements below, but the code will change, so there will be two copies of code A and B, first provide the first one serving https://gitee.com/valuetodays/weixin-oauth-code-optimization-demo/tree/tag-2.0-a.

improve proposals

The way I improved at that time was that the other solution was from a low solution to another low solution, that is, at the beginning of the GoodsController#list() method, first determine whether there is an openid in the session, and if so, conduct business, if not. Just do the authorization and authentication; at this time, WxOAuth2Controller#beforeH5Page() will not send any more authorization and authentication requests, it just puts the link in the session. This is the second copy of Code B at https://gitee.com/valuetodays/weixin-oauth-code-optimization-demo/tree/tag-2.0-b.

Then the program runs just fine.

Add new modules...

So far, the system is working fine. But this time an order list page was added. It has the same simple logic as the product list page (our first page is called the product list page, although it does not display a single product information). At this time, we only need to make a copy of GoodsController and modify the request path and jump page. At this time, the list() of the two functions is the same, as follows:

    @GetMapping("list")
    public String list(HttpServletRequest request, Model model) {
        Object openidTokenObj = request.getSession().getAttribute(WxRedirectController.WX_OPENID_TOKEN);
        if (openidTokenObj == null) {
            wxRedirectController.toOAuth2();
        } else {
            LOG.debug("openid: " + openidTokenObj);
            model.addAttribute("openid", openidTokenObj);
            return "goods/list";
        }
        return "error";
    }

It looks fine, no problem, but if I add another product detail page, I have to add a detail() method in GoodsController, but this method also has to use openid, and then, the long-lost code appears as follows Fragment:

        Object openidTokenObj = request.getSession().getAttribute(WxRedirectController.WX_OPENID_TOKEN);
        if (openidTokenObj == null) {
            wxRedirectController.toOAuth2();
        } else {
            ......
        }

If you add another detail page to the order module, then add a personal center module, and then... Don't think about it anymore. The above approach is not very friendly. From this, I was thinking about how to solve this problem, that is, there is no need to judge whether there is an openid in the business code, but the program will definitely have it when it comes here. I posted a query on csdn hoping to find a solution, and finally found a solution - use Servlet's filter or SpringMVC's Interceptor.

code hacking

I don't know if you have seen it. The b version of version 2 can run well, but it has a big problem. The Controller class in the business code should inherit WxOAuth2Controller and use WxRedirectController. These two classes should have nothing to do with business. , only related to the architecture. What's exciting is that in version three we're going to make the code more decent and less intrusive, not letting the code inherit this and not letting you use that, and of course the two classes WxOAuth2Controller and WxRedirectController will have to be removed of.

That's it for this article.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121099&siteId=291194637