org.chos.transaction.passport.controller.TencentPassportController.java Source code

Java tutorial

Introduction

Here is the source code for org.chos.transaction.passport.controller.TencentPassportController.java

Source

/*
 * @(#)WeiboPassportController.java   1.0 2015-4-9 ?11:08:14
 *
 * Copyright 2008 WWW.YHD.COM. All rights reserved.
 *      YIHAODIAN PROPRIETARY/CONFIDENTIAL. 
 *       Use is subject to license terms.
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the WWW.YHD.COM License is distributed on an "AS 
 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either ex-
 * press or implied. See the License for the specific language govern-
 * ing permissions and limitations under the License.
 */
package org.chos.transaction.passport.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Pattern;

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

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpException;
import org.chos.servlet.http.HttpTemplate;
import org.chos.transaction.User;
import org.chos.transaction.UserService;
import org.chos.transaction.passport.OAuthSession;
import org.chos.transaction.passport.oauth.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * 
 * 
 * @author ada
 * @version 1.0  2015-4-9 ?11:08:14
 * @since 1.0
 */
@Controller
public class TencentPassportController {

    @Autowired
    private UserService userService;

    @Autowired
    private SessionService sessionService;

    private String getHttpRequestUrl(String url, Map<String, String> params) {
        if (params == null) {
            return url;
        }
        StringBuilder sb = new StringBuilder(url);
        if (url.indexOf("?") == -1) {
            sb.append("?");
        }
        for (Entry<String, String> entry : params.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        url = sb.toString();
        url = url.substring(0, url.length() - 1);
        return url;
    }

    @RequestMapping(value = "/qq")
    public void logininput(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OAuthSession session = new OAuthSession();
        sessionService.createSession(session);

        Map<String, String> params = new HashMap<String, String>();
        params.put("response_type", "code");
        params.put("client_id", "101207948");
        params.put("redirect_uri", URLEncoder.encode("http://chos2009.eicp.net/qq/login.shtml", "utf-8"));
        params.put("state", session.getState());
        params.put("scope", "get_user_info");
        params.put("display", "mobile");
        //params.put("language", "en");
        //response.sendRedirect("https://api.weibo.com/oauth2/authorize");
        String url = getHttpRequestUrl("https://graph.qq.com/oauth2.0/authorize", params);
        response.sendRedirect(url);
    }

    public Map<String, Object> parseParam(String param) {
        String[] params = param.split("&");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        for (String p : params) {
            String ps[] = p.split("=");
            paramMap.put(ps[0], ps[1]);
        }
        return paramMap;
    }

    @RequestMapping(value = "/qq/login")
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String code = request.getParameter("code");
        String state = request.getParameter("state");
        OAuthSession session = sessionService.getSession(state);
        if (session == null) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        if (state == null) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        if (!state.equals(session.getState())) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        Map<String, String> param = new HashMap<String, String>();
        param.put("client_id", "101207948");
        param.put("client_secret", "9ecb9b104ff172c2d0724840bd118e81");
        param.put("grant_type", "authorization_code");
        param.put("code", code);
        param.put("redirect_uri", "http://chos2009.eicp.net");
        HttpTemplate template = new HttpTemplate();
        String resp = null;
        try {
            resp = template.post("https://graph.qq.com/oauth2.0/token", param);
        } catch (HttpException e) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        Map<String, Object> paramMap = parseParam(resp);
        String ak = (String) paramMap.get("access_token");

        param = new HashMap<String, String>();
        param.put("access_token", ak);
        try {
            resp = template.post("https://graph.qq.com/oauth2.0/me", param);
        } catch (HttpException e) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        //callback( {"client_id":"101207948","openid":"1901C7D0BCBE69A661738BF19F9660F8"} );
        int indexs = resp.indexOf("{");
        int indexe = resp.indexOf("}");
        resp = resp.substring(indexs, indexe + 1);
        JSONObject json = JSONObject.fromObject(resp);
        String openId = json.getString("openid");

        param = new HashMap<String, String>();
        param.put("access_token", ak);
        param.put("oauth_consumer_key", "101207948");
        param.put("openid", openId);
        try {
            resp = template.post("https://graph.qq.com/user/get_user_info", param);
        } catch (HttpException e) {
            response.sendRedirect("http://chos2009.eicp.net/login.shtml");
        }
        json = JSONObject.fromObject(resp);
        String nickname = json.getString("nickname");

        User user = userService.create(openId + "@qq", null, "13120984792", "13120984792@qq.com", true, request,
                response);
        response.sendRedirect("http://chos2009.eicp.net/mindex.htm");
    }
}