现在的位置: 首页 > 综合 > 正文

微信OAuth验证 java 版本

2018年05月22日 ⁄ 综合 ⁄ 共 2386字 ⁄ 字号 评论关闭
/**
 * @author JackZhang
 *
 */
public class OAuthAPI {

    public static final String APP_ID = "ABC";
    public static final String APP_SECRET = "CDE";
    public static final String DOMAIN = "WWW.ABC.COM";
    

    public static void OAuthIfNesscary(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        HttpSession session = request.getSession();
        boolean isValidCode = true;
        String serviceUrl = URLEncoder.encode(
                "http://" + DOMAIN + request.getRequestURI(), "utf-8");
        //检查是否已验证或者验证是否通过
        if (code == null || code.equals("authdeny")) {
            isValidCode = false;
        }
        //如果session未空或者取消授权,重定向到授权页面
        if ((!isValidCode) && session.getAttribute("user") == null) {
            StringBuilder oauth_url = new StringBuilder();
            oauth_url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
            oauth_url.append("appid=").append(APP_ID);
            oauth_url.append("&redirect_uri=").append(serviceUrl);
            oauth_url.append("&response_type=code");
            oauth_url.append("&scope=snsapi_userinfo");
            oauth_url.append("&state=1#wechat_redirect");
            response.sendRedirect(oauth_url.toString());
            return;
        }
        //如果用户同意授权并且,用户session不存在,通过OAUTH接口调用获取用户信息
        if (isValidCode && session.getAttribute("user") == null) {
                Member member = null;
                JSONObject obj = OAuthAPI.getAccessToken(OAuthAPI.APP_ID,OAuthAPI.APP_SECRET, code);
                String token = obj.getString("access_token");
                String openid = obj.getString("openid");
                JSONObject user = OAuthAPI.getUserInfo(token, openid);
                MemberService memberService = (MemberService) WebAppContext.getObject("memberService");
                member = memberService.saveOrUpdateIfNesscary(user);
                session.setAttribute("user", member);
        }
    }


    /**
     * 获取授权令牌
     * */
    public static JSONObject getAccessToken(String appid, String secret,
            String code) {
        StringBuilder url = new StringBuilder();
        url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
        url.append("appid=" + appid);
        url.append("&secret=").append(secret);
        url.append("&code=").append(code);
        url.append("&grant_type=authorization_code");
        return HttpClientUtils.getJson(url.toString());
    }

    //获取用户信息
    public static JSONObject getUserInfo(String token, String openid) {
        StringBuilder url = new StringBuilder();
        url.append("https://api.weixin.qq.com/sns/userinfo?");
        url.append("access_token=" + token);
        url.append("&openid=").append(openid);
        url.append("&lang=zh_CN");
        return HttpClientUtils.getJson(url.toString());
    }

}

$(document).ready(function(){
	if("${param.code}"=="authdeny")
	{
		$("body").css("display","none");
		document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
			WeixinJSBridge.call('closeWindow');
		});
	}

抱歉!评论已关闭.