今天给大家介绍qq互联第三方登录的一个例子,其中我研究了一下挺坑的,所以今天介绍给大家注意点。
最近我买了一款非常实用实惠的简易木鞋架https://item.taobao.com/item.htm?spm=a230r.1.14.156.eU36q7&id=548041120080&ns=1&abbucket=5#detail
如果大家需要可以购买很实惠的
@Controller
public class QQController extends BaseController {
@Resource(name="userService")
private UserService userService;
@RequestMapping(value="qq/toAuthorize",method = RequestMethod.GET)
public void toAuthorize(HttpServletRequest request ,HttpServletResponse response){
response.setContentType("text/html;charset=utf-8");
try {
logBefore(logger, "引导到授权页面");
PageData pd = this.getPageData();
logBefore(logger,"----------------->更新前"+QQConnectConfig.getValue("redirect_URI"));
String callBackUrl = "http://www.xxx.com/qq/qqCallBack.html"+"?page="+pd.getString("redirectUrl");//你在qq互联申请的qq回调地址
callBackUrl=URLEncoder.encode(callBackUrl,"UTF-8");//主要是这里需要重新编码一次
QQConnectConfig.updateProperties("redirect_URI",callBackUrl);
logBefore(logger,"----------------->更新后的"+QQConnectConfig.getValue("redirect_URI"));
String auth = new Oauth().getAuthorizeURL(request);
logBefore(logger,"----------------->最后跳转地址"+auth);
response.sendRedirect(auth);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value="qq/qqCallBack")
public void callBack(HttpServletRequest request,HttpServletResponse response){
try {
logBefore(logger, "进入qqCallBack回调aciton");
response.setContentType("text/html; charset=utf-8");
String decodeCallBackUrl=URLDecoder.decode(QQConnectConfig.getValue("redirect_URI"),"UTF-8");//解码
QQConnectConfig.updateProperties("redirect_URI",decodeCallBackUrl);
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
String accessToken=null,openID= null,avatar=null,nickName=null;
long tokenExpireIn = 0L;
if (accessTokenObj.getAccessToken().equals("")) {
// 我们的网站被CSRF攻击了或者用户取消了授权
// 做一些数据统计工作
System.out.print("没有获取到响应参数");
} else {
accessToken = accessTokenObj.getAccessToken();
tokenExpireIn = accessTokenObj.getExpireIn();
request.getSession().setAttribute("qq_access_token", accessToken);
request.getSession().setAttribute("qq_token_expirein", String.valueOf(tokenExpireIn));
// 利用获取到的accessToken 去获取当前用的openid -------- start
OpenID openIDObj = new OpenID(accessToken);
openID = openIDObj.getUserOpenID();
request.getSession().setAttribute("qq_openid", openID);
// 利用获取到的accessToken 去获取当前用户的openid --------- end
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
avatar=userInfoBean.getAvatar().getAvatarURL100();//获取100*100的头像
nickName=userInfoBean.getNickname();//获取昵称
if(!StringUtils.isEmpty(nickName)){
nickName = URLEncoder.encode(nickName,"UTF-8");
}
logBefore(logger, "------------->accessToken"+accessToken);
logBefore(logger, "------------->openID"+openID);
logBefore(logger, "------------->avatar"+avatar);
logBefore(logger, "------------->nickName"+nickName);
//查找是否在数据库存在绑定用户
PageData pd = this.getPageData();
String page = pd.getString("page");
pd.put("openId", openID);
pd.put("bind_type", "0");
PageData userPd = userService.findUserByOpenId(pd);
if(userPd!=null&&userPd.size()>0){//存在绑定用户
logBefore(logger, "------------->存在绑定用户");
super.getSession().setAttribute("userInfo", userPd);
response.sendRedirect(page);
}else{//不存在绑定用户
logBefore(logger, "------------->不存在绑定用户");
StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
logBefore(logger, "------------->tempContextUrl"+tempContextUrl);
response.sendRedirect(tempContextUrl+"user/toRegister.html?openId="+openID+"&accessToken="+accessToken+"&bind_type=0&redirectUrl="+page+"&nickname="+nickName+"&figureurl="+avatar);
}
}
} catch (QQConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}