WeChat Official Account Webpage Authorization Code Optimization Process (4)

The last blog post is fully functional and working well. But we feel that there is still room for improvement in the code, so we start to improve it. As mentioned before, the improved code uses SpringMVC's interceptor Interceptor or Servlet's filter Filter, here we use the former for two reasons:

  • Filters need to be configured in web.xml. One more configuration or more code increases the possibility of problems. There is a saying that "there will be no bugs if you don't write code."
  • In the project framework, we have used SpringMVC. In order to complete the function, we just use the functions provided by itself. Anyway, the function is there, and it is not used if it is not used.

Understand and use interceptors

If you look at the source code entry class of SpringMVC org.springframework.web.servlet.DispatcherServlet, you will find that the program will be processed by the interceptor before it is executed to the Controller class. The interceptor can decide whether to proceed to the next step or return false to interrupt the request. To write an interceptor, you can implement the HandlerInterceptor interface, or inherit the HandlerInterceptorAdapter abstract class. The difference is that to implement the interface, you must rewrite all its methods, while inheriting the abstract class only needs to rewrite the required methods. The functions are the same, depending on the individual. Like to use, I tend to use HandlerInterceptorAdapter. The HandlerInterceptor interface has three methods:

  • boolean preHandle() is executed before the Controller is requested
  • void postHandle() is executed after the Controller is requested, but before the DispatcherServlet renders the page view
  • void afterCompletion() Execution of DispatcherServlet rendering page view

All we need is one method: preHandle(). Suppose our interceptor is named WxOAuth2Interceptor, which inherits the HandlerInterceptorAdapter abstract class and rewrites the preHandle() method. The main problem of this method is to determine whether there is an openid in the session, and then let it go, if not, first determine the request Whether there is a code parameter, if there is, go to get the openid and save it to the session before releasing, if not, go to WeChat authorization authentication. After that, in the page code such as GoodsController.list(), you only need to take out the openid from the session and use it, and there is no need to judge whether the openid is empty.

It is important to note that the redirect_uri during WeChat authorization and authentication has been replaced with the current request path. The previous version jumped to WxRedirectController#callback().

configure

When the code is written, you need to configure it in the SpringMVC configuration file. This file is the SpringMVC configuration file, not the Spring configuration file, that is, configure org.springframework.web.servlet.DispatcherServlet in web.xml The contextConfigLocation parameter, in this case springMVC-servlet.xml. The configuration is very simple, as follows:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/wxmpBind/**"/>
            <bean class="com.billy.weixinoauthcodeoptimization.WxOAuth2Interceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

mvc:mapping indicates which paths the interceptor wants to intercept. Just use mvc:exclude-mapping to exclude the paths that you don't want to intercept. We can't intercept this request from the WeChat binding server, so we exclude it.

The code address of version 3 is as follows: Code of version 3

Guess you like

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