微信开发——在拦截器中初始化openid等用户个人信息

微信开发——在拦截器中初始化openid等用户个人信息

微信开发框架最好使用fastweixin做支持

定义微信拦截器
注意:此backUrl中的回调地址是请求微信接口后微信会回调请求的地址,需在拦截器中放行否则会无限循环

/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.thinkgem.jeesite.modules.weixin.utils.Constants;
import com.thinkgem.jeesite.modules.weixin.utils.WxJsSdkUtil;

/**
 * 微信拦截器
 * @author H.S
 * @version 2019-3-28
 */
public class WeChatInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
	HttpSession session = request.getSession();
		System.out.println("openid=====WeChatInterceptor====="+session.getAttribute("openid"));
		if(session.getAttribute("openid")!=null){
			return true;
		}
		try {
			System.out.println("设置微信个人信息...");
			//System.out.println("request.getRequestURL()====="+request.getRequestURL());
			String backUrl = "http://" + Constants.ContextPath + "/a/weChat/initUserData?userAccessUrl="+request.getRequestURL();
			String redirectUrl = WxJsSdkUtil.getOauthAPI().getOauthPageUrl(backUrl, OauthScope.SNSAPI_USERINFO, "11");				
			response.sendRedirect(redirectUrl);
		} catch (Exception e) {
			System.out.println("微信拦截器出现异常!!!");
			e.printStackTrace();
			//跳过拦截器继续执行
			return true;
		}
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}

工具类中获取OauthAPI对象

/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.weixin.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.enums.OauthScope;
import com.github.sd4324530.fastweixin.util.JsApiUtil;
import com.thinkgem.jeesite.common.config.Global;


/**
 * JS-SDK工具类:包括其他参数的获取,SHA1加密,文件流读写操作等等
 * 获取accessToken,jsApiTicket,用户openid等信息
 * @author H.Surge
 * @version 2019年3月25日
 */
public class WxJsSdkUtil {

	public static ApiConfig apiConfig;
	/**
	 * 获取AccessToken
	 * @author H.Surge
	 * @version 2019年3月26日
	 */
	public static String getAccessToken() {
		if(apiConfig==null){
			apiConfig=new ApiConfig(Constants.APP_ID, Constants.APP_SECRET, true);
		}
		return apiConfig.getAccessToken();
	}
	
	/**
	 * 获取OauthAPI对象
	 * @author H.Surge
	 * @version 2019年3月26日
	 */
	public static OauthAPI getOauthAPI() {
		if(apiConfig==null){
			apiConfig=new ApiConfig(Constants.APP_ID, Constants.APP_SECRET, true);
		}
		return new OauthAPI(apiConfig);
	}

}

定义一个通用的controller写好initUserData请求方法,这里实现真正的在session中绑定openid

/**
	 * 绑定微信用户信息
	 * @author H.Surge 
	 * @version 2019年3月26日
	 */
	@RequestMapping(value =  "initUserData" )
	public String initUserData(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			@RequestParam(value = "code", required = false) String code,
			@RequestParam(value = "userAccessUrl", required = false) String userAccessUrl)
					throws UnsupportedEncodingException {
		//System.out.println("code========"+code);
		//System.out.println("userAccessUrl-----------***-----------=" + userAccessUrl);
		OauthAPI oapi = WxJsSdkUtil.getOauthAPI();
		OauthGetTokenResponse otr = oapi.getToken(code);
		String openid = otr.getOpenid();
		session.setAttribute("openid", openid);
		//System.out.println("userAccessUrl111111-------***---------------=" + userAccessUrl);
		return "redirect:" + userAccessUrl;
	}

这样,每次用户请求时都会在拦截器中判断session是否绑定了openid,拦截器执行完毕以后就可以在真正的请求方法中直接获取openid了。由于开发任务急,先以实现为主,以后我有时间会优化一下这块的代码。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值