github:Github.com/huyufan1995…

Important to note at the beginning:

Want to further study wechat related: small program background, public number, etc., it is recommended to see the wechat document to understand

WXJAVA third-party SDK

Want to understand the principle of wechat login best look at the oAUTH2.0 protocol. Understand after see my document + code you will feel!!

In addition, the callback address used in the development and debugging of wechat must be able to access HTTP. I used natApp configuration

Specific NATApp configuration debugging wechat steps I will not be wordy, readers can baidu. Below put general steps, specific code please

gitHub

 

Mind maps

 

1. First, go to wechat open platform to obtain the test account

Mp.weixin.qq.com/debug/cgi-b…

Project structure drawing

2 Below is the main configuration of the code where the configuration to get various information has been written application.yml

Spring: MVC: view: # page default prefix directory prefix: /WEB-INF/ JSP / # response page default suffix:.jsp appId: wx60C1C374FD9379A2 Secret: B6e4ebc42a5fa9fd1ea7ea440e52d648 redirectUri: http://moran1995.natapp1.cc/callback # # # generate WeChat authorizedUrl authorization: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsap I_userinfo &state= state #wechat_redirect ### After obtaining code, request the following link to obtain access_token access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code Snsapi_userinfo = snsapi_userinfo = snsapi_userinfo https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CNCopy the code

3 Controller layer code

package oauthWeixin.oauth; import javax.servlet.http.HttpServletRequest; import oauthWeixin.utils.HttpClientUtils; import oauthWeixin.utils.WeiXinUtils; import oauthWeixin.base.BaseApiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.alibaba.fastjson.JSONObject; @Controller public class OauthController extends BaseApiService { @Autowired private WeiXinUtils weiXinUtils; private String errorPage = "errorPage"; @requestMapping ("/authorizedUrl") public String authorizedUrl() {return "redirect:" + weiXinUtils.getAuthorizedUrl(); } @requestMapping (" callback") public String callback(String code, HttpServletRequest Request) {// 1 Use of a Code to obtain access_token String accessTokenUrl = weiXinUtils. GetAccessTokenUrl (Code); JSONObject resultAccessToken = HttpClientUtils.httpGet(accessTokenUrl); boolean containsKey = resultAccessToken.containsKey("errcode"); If (containsKey) {request.setAttribute("errorMsg", "system error!" ); return errorPage; } / / 2. Use the access_token for user information String accessToken = resultAccessToken. Get String (" access_token "); String openid = resultAccessToken.getString("openid"); Snsapi_userinfo String userInfoUrl = weixinutils.getUserInfo (accessToken, openID); // 3. JSONObject userInfoResult = HttpClientUtils.httpGet(userInfoUrl); System.out.println("userInfoResult:" + userInfoResult); request.setAttribute("nickname", userInfoResult.getString("nickname")); request.setAttribute("city", userInfoResult.getString("city")); request.setAttribute("headimgurl", userInfoResult.getString("headimgurl")); return "info"; }}Copy the code

However, the official requirement is that the request must be made in the wechat environment

The input project access path will have the following

So use wechat development tools

 

 

 

Request the item below

 

The next step after releasing the breakpoint is as follows: wechat development tools require user confirmation

After clicking ok, we will request the background according to our configured callback address

 

 

 

The success screen is as follows: