1. 程式人生 > >微信--高效解決token及授權使用者openid的持久化處理辦法

微信--高效解決token及授權使用者openid的持久化處理辦法

複製程式碼
package com.fdc.home.dec.wx.controller;

import com.fdc.home.dec.service.inter.service.DecWxService;
import com.fdc.home.dec.wx.service.CheckUserInfo;
import com.fdc.home.dec.wx.utils.JSONHelper;
import com.fdc.home.dec.wx.utils.WxUtils;
import com.fdc.home.dec.wx.vo.token.WeixinOauth2Token;
import com.fdc.home.dec.wx.vo.user.WeixinUserInfo;
import com.fdc.platform.common.yfutil.PropertyReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; /** * Created by pl on 2017/1/13. */ @Controller public class OAuth2Controller { @Autowired private DecWxService decWxService; //判斷使用者是否登入的公用方法
CheckUserInfo checkUserInfo = new CheckUserInfo(); //從配置檔案獲取appid public static String appid = PropertyReader.getValue("appid"); //從配置檔案獲取appsecret public static String appsecret = PropertyReader.getValue("appsecret"); //從配置檔案獲取主域名 public static String indexUrl = PropertyReader.getValue("indexUrl"); public static String wtoken = PropertyReader.getValue("wholetoken"); /** * 組裝授權url * @param request * @param resultUrl * @return */ @RequestMapping(value ="/oauth2Api") public String oauth2API(HttpServletRequest request, @RequestParam String resultUrl) { String redirectUrl = ""; if (resultUrl != null) { String backUrl =indexUrl+"/oauth2MeUrl?oauth2url="+resultUrl; //組裝授權url redirectUrl = WxUtils.oAuth2Url(appid, backUrl); } return "redirect:" + redirectUrl; } /** * 獲取使用者資訊 * @param request * @param response * @param code * @param oauth2url * @return * @throws IOException */ @RequestMapping(value = "/oauth2MeUrl") public String oauth2MeUrl(HttpServletRequest request,HttpServletResponse response, @RequestParam String code, @RequestParam String oauth2url) throws IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); HttpSession session = request.getSession(); session.setAttribute("code",code); // 使用者同意授權 if (!"authdeny".equals(code)) { // 獲取網頁授權access_token WeixinOauth2Token weixinOauth2Token = WxUtils.getOauth2AccessToken(appid, appsecret, code); // 網頁授權介面訪問憑證 String accessToken = weixinOauth2Token.getAccessToken(); // 使用者標識 String openId = weixinOauth2Token.getOpenId(); String wholetoken = decWxService.getToken(wtoken); //獲取微信使用者openid儲存在cookie中的資訊 Cookie userCookie=new Cookie("openId",openId); userCookie.setMaxAge(-1); userCookie.setPath("/"); response.addCookie(userCookie); WeixinUserInfo weixinUserInfo = WxUtils.getWeixinUserInfo(wholetoken, openId); //將使用者資訊寫入redis decWxService.setToken(openId, JSONHelper.beanToJson(weixinUserInfo)); }else { return "redirect:"+indexUrl+"/error404"; } return "redirect:" + oauth2url; } }
複製程式碼