First we look at the API documentation micro letter.
Micro-letter web authorization, to obtain the user's micro-channel official API documentation Address: https://open.weixin.qq.com/
Click Resource Center, log on to view micro-channel document
Three-way handshake
micro-channel certification process (referred to my own three-way handshake):
1, user consent, access code
2, the authorization code in exchange for access_token through web pages, and other information users openId
3, to obtain the user information through the user and the user's openId access_token
Login to third micro-channel interfaces Flowchart:
Users to scan two-dimensional code
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
After the user is authorized to allow redirect_uri, redirect_uri will be redirected to the website, and to bring the state parameter code and
redirect_uri?code=CODE&state=STATE
If the authorized user is prohibited, then the redirect will not bring the code parameters, only parameters will bring state
redirect_uri?state=STATE
/ ** * The method of the micro-channel into the lead page * @return * / @RequestMapping ( "/ loginByWeiXin" ) public String loginByWeiXin (the HttpServletRequest Request, the Map <String, Object> Map) { // Get code parameters and state 2 String code request.getParameter = ( "code" ); String State = request.getParameter ( "State" ); System.out.println ( "code -------" + + code ", State ------ - "+ State); IF (code =! null &&!" " .equals (code)) { // successfully authorized,Get the user token and openID OAuthInfo authInfo = WeiXinUtil.getAccess_token (code); String OpenID = authInfo.getOpenid (); String the access_token = authInfo.getAccess_token (); IF (the access_token == null ) { // Code used exceptions System.out.println ( "Code used exceptions ..... " ); return " the redirect: "+ jump path; } // query signal is bound to third micro internet SYSUSER SYSUSER = weiXinService.getUserByWeiXinID (OpenID); iF (SYSUSER == null ) { / / Get random string of length 57 StringUtil.getRandomString randomStr = String (57 ); request.getSession () setAttribute (openid, randomStr);. // is not bound yet account System.out.println ( "Binding account yet ....." ); return " redirect: /index.jsp openid = "+ openid +" & State = "+? randomStr; } userController.doSomeLoginWorkToHomePage (sysUser.getMcid (), the Map); // successful login return " homePage " ; } // unauthorized return " redirect: "+ path; }
(User information back to the micro-channel package entity classes OAuthInfo) The token acquisition code
public static OAuthInfo getAccess_token(String code){ String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code "; authUrl= authUrl.replace("APPID", Param.APPID); authUrl = authUrl.replace("SECRET", Param.SECRET); authUrl = authUrl.replace("CODE", code); String jsonString = HTTPRequestUtil.sendPost(authUrl,""); System.out.println("jsonString: " + jsonString); OAuthInfo auth = null; try { auth = (OAuthInfo) JacksonUtil.parseJSONToObject(OAuthInfo.class, jsonString); } catch (Exception e) { e.printStackTrace(); } return auth; }
Return user information format:
{ "The access_token": "ACCESS_TOKEN" , "expires_in": 7200 , "refresh_token": "refresh_token" , "openid": "OPENID" , "scope": "SCOPE" , "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" // Customer authorizes this field will be after }