`

QQ登录

    博客分类:
  • Java
阅读更多
package com.qq;

import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.strongit.ec.common.service.ICommonService;
import com.strongit.member.bo.TippMemberInfo;
import com.strongit.template.engine.AbstractDataHandler;
import com.strongit.template.util.TencentqqAccessToken;
import com.strongit.template.util.TencentqqOauthApiUtil;

public class QQLogin extends AbstractDataHandler {

protected String execute(HttpServletRequest request,
HttpServletResponse response) throws IOException {

//初始化容器
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(request.getSession()
.getServletContext());
//取得bean实例
         ICommonService commonManager = (ICommonService) context
.getBean("ipp.ec.service.CommonService");
        
         //取得bean实例
         QQVuserGenerator qqGen = (QQVuserGenerator)context.getBean("ipp.qq.qqlogin");
        
// TODO Auto-generated method stub
// 获取Authorization Code
String authorization_code = request.getParameter("code");

System.out.println("...................code = "+authorization_code);

//下一步
// 使用Authorization Code换取Access Token
        TencentqqAccessToken accessToken = TencentqqOauthApiUtil
.getAccessTokenAndOpenId(authorization_code);
       
        System.out.println("..............accessToken = "+accessToken);
       

//再下一步取得Accesstoken
    accessToken = TencentqqOauthApiUtil.getLoginUser(accessToken);

//获取返回的Openid
    String openid = accessToken.getOpenid();
   
    System.out.println("openid = "+openid);

        //获取用户的昵称
    String screenname = accessToken.getScreenName();

    //根据Openid获取用户信息
TippMemberInfo tippMemberInfo = commonManager.getMemberInfoByOpenId(openid);

//根据openid获取的用户信息行进判断如果不存在就进行入库保存、否则就行跳转到用户登录的请求。
//TippMemberInfo memberInfo = null;
if(null == tippMemberInfo)
{
tippMemberInfo = qqGen.generateMemberInfo(openid,"2",screenname);
}

//用户名
String username = tippMemberInfo.getUsername();
// 登录类型
String logintype = tippMemberInfo.getLogintype();
//密码
String password = tippMemberInfo.getUserpassword();

//处理中文乱码 add by mengxianwei 2011-12-26
screenname = URLEncoder.encode(screenname, "utf-8");
//String location = "/User_Reg_1.shtml?username="+username+"&logintype=2&password=222222";
String location = "/QQ.shtml?username="+username+"&screenname="+screenname+"&logintype="+logintype+"&password=222222";
response.sendRedirect(location);
return null;
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics