springboot集成微信和QQ第三方登录

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

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

建立配置类Constants:

/**
 * 常量配置类
 */
@Configuration
@ConfigurationProperties(prefix = "constants")
public class Constants {

	@NotEmpty
	private String qqAppId;

	@NotEmpty
	private String qqAppSecret;

	@NotEmpty
	private String qqRedirectUrl;

	@NotEmpty
	private String weChatAppId;

	@NotEmpty
	private String weChatAppSecret;

	@NotEmpty
	private 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())) {
			// QQ
			return "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
	 */
	@Override
	public 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;
	}

 

以下是一个使用 Spring Boot 集成微信登录的样例: 1. 在微信开放平台上创建应用并获取 AppID 和 AppSecret。 2. 在 Spring Boot 项目的 pom.xml 文件中添加如下依赖: ```xml <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-tools</artifactId> <version>3.3.0</version> </dependency> ``` 3. 创建一个微信登录的控制器: ```java @RestController @RequestMapping("/wechat") public class WechatLoginController { @Autowired private WxMpService wxMpService; @GetMapping("/login") public String login() { String redirectUrl = "http://yourdomain.com/wechat/callback"; String state = UUID.randomUUID().toString(); String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, WxConsts.OAuth2Scope.SNSAPI_USERINFO, state); return "redirect:" + url; } @GetMapping("/callback") public String callback(@RequestParam("code") String code, @RequestParam("state") String state) { try { WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code); WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null); // 在这里可以将用户信息保存到数据库中 return "success"; } catch (WxErrorException e) { e.printStackTrace(); return "error"; } } } ``` 4. 在 application.yml 文件中配置微信的 AppID 和 AppSecret: ```yaml weixin: mp: appId: your_app_id secret: your_app_secret ``` 5. 启动项目,访问 http://yourdomain.com/wechat/login 即可开始微信登录流程。 以上是一个简单的 Spring Boot 集成微信登录的样例,具体实现还需要根据自己的需求进行调整。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值