com.wx.spring.controller.OAuth2Controller.java Source code

Java tutorial

Introduction

Here is the source code for com.wx.spring.controller.OAuth2Controller.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wx.spring.controller;

import com.wx.util.AccessTokenGen;
import com.wx.util.QiYeUtil;
import com.wx.util.Result;
import java.io.UnsupportedEncodingException;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/** 
 * OAuth2 ? 
 * @author Sunlight 
 * 
 */
@Controller
public class OAuth2Controller {
    /** 
     * ???API?? 
     *  
     * @param index 
     * @return 
     */
    @RequestMapping(value = { "/oauth2.do", "/oauth2" })
    public String Oauth2API(HttpServletRequest request, @RequestParam String resultUrl) {
        // ??????id?  
        String CropId = "wxe706b25abb1216c0";
        String redirectUrl = "";
        if (resultUrl != null) {
            //String reqUrl =request.getLocalAddr();  
            //TODO ?"reqUrl"?URL
            String reqUrl;
            reqUrl = "hhxxmm.nat123.net/QiyeProject";

            String backUrl = "http://" + reqUrl + "/oauth2url.do?oauth2url=" + resultUrl;
            System.out.println("backUrl=" + backUrl);
            redirectUrl = oAuth2Url(CropId, backUrl);
        }
        return "redirect:" + redirectUrl;
    }

    /** 
     * ?code?Userid???? 
     *  
     * @param request 
     * @param code 
     *            ???URLcode? 
     * @param oauth2url 
     *            ?? 
     * @return 
     */
    @RequestMapping(value = { "/oauth2url.do" })
    public String Oauth2MeUrl(HttpServletRequest request, @RequestParam String code,
            @RequestParam String oauth2url) {

        HttpSession session = request.getSession();

        String Userid = getMemberGuidByCode(AccessTokenGen.GetToken(), code, 1);
        if (Userid != null) {
            session.setAttribute("UserId", Userid);
        }

        // ??,session  
        return "redirect:" + oauth2url;
    }

    /** 
     * ?URL 
     *  
     * @param corpid 
     *            ?id 
     * @param redirect_uri 
     *            ?????urlencode? 
     * @param state 
     *            ???state???a-zA-Z0-9? 
     * @return 
     */
    private String oAuth2Url(String corpid, String redirect_uri) {
        try {
            redirect_uri = java.net.URLEncoder.encode(redirect_uri, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String oauth2Url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + corpid + "&redirect_uri="
                + redirect_uri + "&response_type=code&scope=snsapi_base&state=sunlight#wechat_redirect";
        System.out.println("oauth2Url=" + oauth2Url);
        return oauth2Url;
    }

    /** 
     * ??? 
     *  
     * @param token 
     * @param code 
     * @param agentId 
     * @return 
     * @throws SQLException 
     * @throws RemoteException 
     */
    public String getMemberGuidByCode(String token, String code, int agentId) {
        System.out.println("code==" + code + "\ntoken=" + token + "\nagentid=" + agentId);
        Result<String> result = QiYeUtil.oAuth2GetUserByCode(token, code, agentId);
        System.out.println("result=" + result);
        if (result.getErrcode() == "0") {
            if (result.getObj() != null) {
                // ??codeUserid??  
                return result.getObj();
            }
        }
        return "";
    }

}