一、首先去腾讯开放平台申请相应的appid等,完成后在yml文件配置如下:

constants:
    # QQ
    qqAppId: *****
    qqAppSecret: *****
    qqRedirectUrl: *****
    #WECHAT
    weChatAppId: *****
    weChatAppSecret: *****
    weChatRedirectUrl: *****

建立配置类Constants:

/*** 常量配置类*/
@Configuration
@ConfigurationProperties(prefix = "constants")
public class Constants {@NotEmptyprivate String qqAppId;@NotEmptyprivate String qqAppSecret;@NotEmptyprivate String qqRedirectUrl;@NotEmptyprivate String weChatAppId;@NotEmptyprivate String weChatAppSecret;@NotEmptyprivate String weChatRedirectUrl;public String getQqAppId() {return qqAppId;}public void setQqAppId(String qqAppId) {this.qqAppId = qqAppId;}public String getQqAppSecret() {return qqAppSecret;}public void setQqAppSecret(String qqAppSecret) {this.qqAppSecret = qqAppSecret;}public String getQqRedirectUrl() {return qqRedirectUrl;}public void setQqRedirectUrl(String qqRedirectUrl) {this.qqRedirectUrl = qqRedirectUrl;}public String getWeChatAppId() {return weChatAppId;}public void setWeChatAppId(String weChatAppId) {this.weChatAppId = weChatAppId;}public String getWeChatAppSecret() {return weChatAppSecret;}public void setWeChatAppSecret(String weChatAppSecret) {this.weChatAppSecret = weChatAppSecret;}public String getWeChatRedirectUrl() {return weChatRedirectUrl;}public void setWeChatRedirectUrl(String weChatRedirectUrl) {this.weChatRedirectUrl = weChatRedirectUrl;}
}

二、返回前端登录调用地址:

根据不同的登录类型返回相应的地址,前端直接使用新窗口或者悬浮窗口打开:

public String getLoginUrl(User user) {if ("1".equals(user.getLoginType())) {// QQreturn "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="+ constants.getQqAppId()+ "&redirect_uri="+ constants.getQqRedirectUrl()+ "&state="+ user.getAddress();} else {// 微信try {return "https://open.weixin.qq.com/connect/qrconnect?appid="+ constants.getWeChatAppId()+ "&redirect_uri="+ constants.getWeChatRedirectUrl()+ "&response_type=code&scope=snsapi_login&state="+ URLEncoder.encode(URLEncoder.encode(user.getAddress(), "utf-8"),"utf-8") + "#wechat_redirect";} catch (UnsupportedEncodingException e) {e.printStackTrace();return null;}}}

三、前端打开后会出现登录的二维码等,扫描之后会跳转到我们配置的redirect_uri并在url后面返回code参数和我们自定义的state参数

四、根据code拿到accessToken和openid

这一步稍有区别,qq必须先通过code获取accesstoken,再通过accesstoken获取openid;微信则通过code直接获取accesstoken 和openid

qq:

public String getAccessToken(String code) {String url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id="+ constants.getQqAppId()+ "&client_secret="+ constants.getQqAppSecret()+ "&code="+ code+ "&redirect_uri=" + constants.getQqRedirectUrl();String resp = RestTemplateUtil.getRestTemplate().getForObject(url,String.class);if (resp != null && resp.contains("access_token")) {Map<String, String> map = RestTemplateUtil.getParam(resp);String access_token = map.get("access_token");return access_token;}return null;}
public String getOpenId(String accessToken) {String url = "https://graph.qq.com/oauth2.0/me?access_token="+ accessToken;String resp = RestTemplateUtil.getRestTemplate().getForObject(url,String.class);if (resp != null && resp.contains("openid")) {JSONObject jsonObject = RestTemplateUtil.convertToJson(resp);String openid = jsonObject.getString("openid");return openid;}return null;}

微信:

public AccessToken getAccessToken(String code) {String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ constants.getWeChatAppId() + "&secret="+ constants.getWeChatAppSecret() + "&code=" + code+ "&grant_type=authorization_code";String resp = RestTemplateUtil.getRestTemplate().getForObject(url,String.class);if (resp != null && resp.contains("access_token")) {AccessToken accessToken = JSON.parseObject(resp, AccessToken.class);return accessToken;}return null;}

AccessToken 对象为:

/*** 接口调用凭证*/private String access_token;/*** access_token接口调用凭证超时时间,单位(秒)*/private Integer expires_in;/*** 用户刷新access_token*/private String refresh_token;/*** 授权用户唯一标识*/private String openid;/*** 用户授权的作用域,使用逗号(,)分隔*/private String scope;/*** 当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段。*/private String unionid;

五:根据accesstoken 和openid获取用户信息

qq:

public String getUserInfo(String accessToken, String openId) {String url = "https://graph.qq.com/user/get_user_info?access_token="+ accessToken + "&oauth_consumer_key=" + constants.getQqAppId()+ "&openid=" + openId;String resp = RestTemplateUtil.getRestTemplate().getForObject(url,String.class);return resp;}

微信:

/*** 获取用户信息* * @param accessToken* @param openId* @param lang*            非必须(国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语,默认为zh-CN)* @return*/@Overridepublic String getUserInfo(String accessToken, String openId, String lang) {String url = "https://api.weixin.qq.com/sns/userinfo?access_token="+ accessToken + "&openid=" + openId;if (lang != null && !"".equals(lang)) {url += "&lang=" + lang;}String resp = RestTemplateUtil.getRestTemplate().getForObject(url,String.class);return resp;}

springboot集成微信和QQ第三方登录相关推荐

  1. 微信、QQ第三方登录

    微信登录需要使用EasyWechat 登录后需要绑定手机号 控制器中引入类库 config 配置添加app_id.secret use EasyWeChat\Factory; //微信授权登陆publ ...

  2. SpringBoot 集成 微信绑定 微信登录

    SpringBoot 集成 微信登录 重写一个认证逻辑 实现AuthenticationProvider import com.hzjtcl.commons.security.service.Hzjt ...

  3. 微信QQ第三方登录分享和支付宝微信支付

    登录是使用的是友盟第三方登录:APPID 和APPKEY都是成对出现的,改一个都要改不然出错. QQ第三方登录: APPID ,APPKEY: 微信第三方登录:WEIXINID, WEIXINKEY ...

  4. iOS QQ第三方登录

    今天做了QQ第三方登录,由于公司需要,没用集成的ShareSDK或友盟,走了些弯路,说一下我遇到的问题,以及解决的方式 1. pod 导入QQ的SDK pod 'TencentOpenApiSDK', ...

  5. 一步一步实现iOS QQ第三方登录

    前言 最近在公司应用中集成QQ登录的时候遇到一些波折(坑点),觉得还是有必要记录一下. 一.集成SDK 1.集成官方Framework 首先下载官网SDK iOS_SDK下载,目前来说最新的包是V3. ...

  6. ShareSDK集成微信、QQ、微博分享

    1.前言 为什么要使用第三方的作为集成分享的工具呢?而不去用官方的呢?有什么区别么? 一个字"快",如果你使用官方的得一个个集成他们的SDK,相信这是一个痛苦的过程. 2.准备需要 ...

  7. php如何实现qq第三方登录,PHP实现qq第三方登录

    除了qq第三方登录外.还有微博,微信等第三方登录 qq第三方登录,遵循oauth2.0协议 这里是说明http://www.cnblogs.com/yx520zhao/p/6616686.html q ...

  8. Springboot 集成邮件服务 QQ企业邮箱

    笔者之前写过 Springboot 集成邮件服务 QQ邮箱 和本文档相似度高,其中本文档中部分内容和笔者的 Springboot 集成邮件服务 QQ邮箱 完全一样,笔者未重写,直接参考即可. 1.开通 ...

  9. 公司网站如何让用户使用QQ第三方登录

    我们做了一个中农物联网系统,为了简化用户使用门槛,并保证用户资料安全,准备在自身的用户管理基础上加入第三方登录,主要是QQ登录.微信二维码扫描登录.手机微信登录等.     QQ第三方登录,用的是QQ ...

最新文章

  1. AI量身定制:如何打造符合“中国特色教育”的内容推荐体系?
  2. ACM——模拟(hard) 刷题总结
  3. 微信分享转发功能「PHP版」
  4. python自学攻略-你是如何自学 Python 的?
  5. 【VAB】获取库文件地址
  6. web.xml与index.html
  7. Matlab移植到Eigen用到的词条
  8. kali linux 清华源_Kali Linux系统更新安装国外源教程:
  9. 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
  10. 在ubuntu13.04上安装Mac主题
  11. 使用getApplication()作为上下文的对话框抛出“无法添加窗口-令牌null不适用于应用程序”
  12. vue 日期面板_VUE项目中如何方便的转换日期和时间
  13. 邱锡鹏:为什么相比于CV,NLP领域的发展要缓慢?
  14. 关于mysql性能优化_关键的十个MySQL性能优化技巧
  15. 数据库表自动生成word文档
  16. HTML led字体包
  17. 实用的bi报表工具--Smartbi报表软件
  18. kali linux Live Usb Encrypted Persistence配置教程
  19. 亲测好用的屏幕录制软件:camtasia2021 mac中文版
  20. VBA编程图表(二十一)

热门文章

  1. 地理坐标定位和计算距离
  2. 第二篇第五章防火防烟分区于分隔
  3. FRP搭建内网穿透(亲测有效)
  4. 信息系统项目管理师核心考点(六十五)信息安全基础知识网络安全
  5. ggplot2_coor_xxx()坐标系变换
  6. http://fonts.googleapis.com/css?打开很慢解决方案
  7. navicat 使用ssh连接腾讯云主机mysql数据库(保姆级教程)
  8. ESP8266-01实战一——带OLED显示屏电子时钟
  9. hoolilaw解读:在美国如何应对交通罚单
  10. 曾经排名第一的安全软件,为啥会变成流氓软件?