1,到 https://connect.qq.com QQ互联管理中心的"应用管理"申请QQ的app_id和app_key,提前提交你需要回调的地址.
2,配置申请下来的的appid和appkey,及回调地址
3,获取配置文件中appid和appkey,机会掉地址,代码如下:
package com.yst.wallet.common; import java.util.Properties; import org.apache.log4j.Logger; /** private static final Logger logger = Logger.getLogger(Config.class.getName()); public static String qq_app_id; /** /** logger.info(" qq_app_id=" + qq_app_id); |
4,编写controller代码(回调地址),通过appid,appkey和回调地址获取access_token,open_id然后获取qq的用户信息
/** * 采用QQ三方登陆之后的回调地址 * @param login_token * @return * @throws Exception */ @RequestMapping(value="/isajax/OAuth2Login/QQLoginResponseProcess") public ModelAndView qqLoginCallBack(@RequestParam(required=false) String code, @RequestParam(required=false) String state){ String path = CLASS_PATH + "/" + Thread.currentThread().getStackTrace()[1].getMethodName(); LogUtils.putRequestLog("qq第三方登录的回调", path,this.getPageData()); ModelAndView mv = this.getModelAndView(); if(StringUtils.isNotBlank(code)){ try { //1,获取access_token String token = QQLoginUtil.getAccessToken(code); //2,获取openId String openId = QQLoginUtil.getOpenid(token); if(StringUtils.isNotBlank(openId)){ //3.根据openid判断是不是绑定了,没有绑定,跳转到绑定页面 PageData pd = new PageData(); pd.put("login_type", "05");// 登录类型 05 国美在线 pd.put("login_name", openId);// 登录名 pd = new Epay_api_security().user_login(pd); if (Const.RETURN_SUCCESS.equals(pd.get("code"))) {// 请求成功,登录shiro,跳转到index页面 UserInfo user = (UserInfo) pd.get("data"); if (user != null) { user.setLast_login_time(DateFormatUtil.to_normal(user.getLast_login_time())); // 把用户信息放session中 Jurisdiction.getSession().setAttribute(Const.SESSION_USER, user); Jurisdiction.getSession().setAttribute(Const.LOGIN_TOKEN, user.getLogin_token()); //shiro加入身份验证 Subject subject = SecurityUtils.getSubject(); CaptchaUsernamePasswordToken logintoken = new CaptchaUsernamePasswordToken(token, token,"","","","",token,openId,""); try { subject.login(logintoken); } catch (Exception e){ e.printStackTrace(); mv.setViewName("redirect:/login_toLogin"); } mv.setViewName("redirect:/index");//跳转到首页 }else{ mv.setViewName("error"); mv.addObject("msg","用户信息为空,请稍后重试"); } }else{ String userInfo = QQLoginUtil.getQQUser(token, openId); mv.setViewName("redirect:/gomebangding");//跳转到首页 Jurisdiction.getSession().setAttribute(Const.GOME_OPENID, openId); Jurisdiction.getSession().setAttribute(Const.GOME_USER, userInfo); } }else{//{"error_code":3019,"error_description":"code值错误"} mv.setViewName("error"); mv.addObject("msg","腾讯QQ授权登录返回信息:"+openId); } } catch (ClientProtocolException e) { e.printStackTrace(); mv.setViewName("error"); mv.addObject("msg",e.getMessage()); } catch (IOException e) { e.printStackTrace(); mv.setViewName("error"); mv.addObject("msg",e.getMessage()); } }else{ mv.setViewName("error"); mv.addObject("msg","code值为空"); } return mv; } |
5,html页面调取qq的链接
<li class="qq_sign"><span title="QQ账号" href=""
οnclick="javascript:document.location='https://graph.qq.com/oauth/show?which=ConfirmPage&display=pc&response_type=code&client_id=${qq_app_id}&redirect_uri=${qq_redirect_uri}&state=test'"></span></li>
6,QQLoginUtil.java
package com.yst.wallet.common.util; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import com.yst.wallet.common.Config; public class QQLoginUtil { //获得AccessToken的url |