您好,欢迎来到99网。
搜索
您的当前位置:首页Springboot项目实现第三方qq登录

Springboot项目实现第三方qq登录

来源:99网
Springboot项⽬实现第三⽅qq登录

1、qq互联申请开发者账号

进⾏⾝份审核,⾝份审核不容易通过应⽤管理——⽹站应⽤——创建应⽤

2、pom.xml相关依赖

org.springframework.boot spring-boot-starter

org.springframework.boot spring-boot-starter-test test

org.springframework.boot spring-boot-devtools true

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot spring-boot-starter-web

org.apache.commons commons-io 1.3.2

org.apache.commons commons-lang3

org.apache.httpcomponents httpclient

com.alibaba fastjson 1.2.38

3、配置⽂件信息

填写QQ互联的AppID,key和回调地址

constants.qqAppId=101513767

constants.qqAppSecret=b1d978cefcf4053883d8e686d307b0constants.qqRedirectUrl=http://127.0.0.1:8080/QQLogin

4、读取配置⽂件信息常量类(⾃⾏⽣成set get⽅法)

import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Configuration;/**

* qq 登陆常量配置类 */

@Configuration

public class Constants {

@Value(\"${constants.qqAppId}\") private String qqAppId;

@Value(\"${constants.qqAppSecret}\") private String qqAppSecret;

@Value(\"${constants.qqRedirectUrl}\") private String qqRedirectUrl; 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; }}

5、qq实体类(⾃⾏⽣成set get⽅法)

public class QQUserInfo {

private Integer ret; private String msg; private Integer is_lost; private String nickname; private String gender; private String province; private String city; private String year;

private String constellation; private String figureurl; private String figureurl_1; private String figureurl_2; private String figureurl_qq; private String figureurl_qq_1; private String figureurl_qq_2; private String is_yellow_vip; private String vip;

private String yellow_vip_level; private String level;

private String is_yellow_year_vip;}

6、http⼯具类 HttpClientUtils.java

import java.io.IOException;

import java.net.SocketTimeoutException;

import java.security.GeneralSecurityException;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.List;import java.util.Map;

import java.util.Map.Entry;import java.util.Set;

import javax.net.ssl.SSLContext;import javax.net.ssl.SSLException;import javax.net.ssl.SSLSession;import javax.net.ssl.SSLSocket;

/**

* Created by Administrator on 2018/10/30/030. */

import org.apache.commons.io.IOUtils;

import org.apache.commons.lang3.StringUtils;import org.apache.http.Consts;

import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.config.RequestConfig.Builder;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;

import org.apache.http.conn.ConnectTimeoutException;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.conn.ssl.SSLContextBuilder;import org.apache.http.conn.ssl.TrustStrategy;

import org.apache.http.conn.ssl.X509HostnameVerifier;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.message.BasicNameValuePair;@SuppressWarnings(\"all\")public class HttpClientUtils {

public static final int connTimeout = 10000; public static final int readTimeout = 10000; public static final String charset = \"UTF-8\"; private static HttpClient client = null;

static {

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(128);

cm.setDefaultMaxPerRoute(128);

client = HttpClients.custom().setConnectionManager(cm).build(); }

public static String postParameters(String url, String parameterStr)

throws ConnectTimeoutException, SocketTimeoutException, Exception {

return post(url, parameterStr, \"application/x-www-form-urlencoded\ }

public static String postParameters(String url, String parameterStr, String charset, Integer connTimeout,

Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { return post(url, parameterStr, \"application/x-www-form-urlencoded\ }

public static String postParameters(String url, Map params) throws ConnectTimeoutException, SocketTimeoutException, Exception { return postForm(url, params, null, connTimeout, readTimeout); }

public static String postParameters(String url, Map params, Integer connTimeout,

Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { return postForm(url, params, null, connTimeout, readTimeout); }

public static String get(String url) throws Exception { return get(url, charset, null, null); }

public static String get(String url, String charset) throws Exception { return get(url, charset, connTimeout, readTimeout); }

/**

* 发送⼀个 Post 请求, 使⽤指定的字符集编码. *

* @param url

* @param body RequestBody

* @param mimeType 例如 application/xml \"application/x-www-form-urlencoded\" * a=1&b=2&c=3 * @param charset 编码

* @param connTimeout 建⽴链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒.

* @return ResponseBody, 使⽤指定的字符集编码.

* @throws ConnectTimeoutException 建⽴链接超时异常 * @throws SocketTimeoutException 响应超时 * @throws Exception */

public static String post(String url, String body, String mimeType, String charset, Integer connTimeout,

Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { HttpClient client = null;

HttpPost post = new HttpPost(url); String result = \"\";

try {

if (StringUtils.isNotBlank(body)) {

HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset)); post.setEntity(entity); }

// 设置参数

Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) {

customReqConf.setConnectTimeout(connTimeout); }

if (readTimeout != null) {

customReqConf.setSocketTimeout(readTimeout); }

post.setConfig(customReqConf.build());

HttpResponse res;

if (url.startsWith(\"https\")) { // 执⾏ Https 请求.

client = createSSLInsecureClient(); res = client.execute(post); } else {

// 执⾏ Http 请求.

client = HttpClientUtils.client; res = client.execute(post); }

result = IOUtils.toString(res.getEntity().getContent(), charset); } finally {

post.releaseConnection();

if (url.startsWith(\"https\") && client != null && client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } }

return result; }

/**

* 提交form表单 *

* @param url

* @param params

* @param connTimeout * @param readTimeout * @return

* @throws ConnectTimeoutException * @throws SocketTimeoutException * @throws Exception */

public static String postForm(String url, Map params, Map headers, Integer connTimeout, Integer readTimeout)

throws ConnectTimeoutException, SocketTimeoutException, Exception {

HttpClient client = null;

HttpPost post = new HttpPost(url); try {

if (params != null && !params.isEmpty()) {

List formParams = new ArrayList(); Set> entrySet = params.entrySet(); for (Entry entry : entrySet) {

formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); }

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); post.setEntity(entity); }

if (headers != null && !headers.isEmpty()) {

for (Entry entry : headers.entrySet()) { post.addHeader(entry.getKey(), entry.getValue()); } }

// 设置参数

Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) {

customReqConf.setConnectTimeout(connTimeout); }

if (readTimeout != null) {

customReqConf.setSocketTimeout(readTimeout); }

post.setConfig(customReqConf.build()); HttpResponse res = null; if (url.startsWith(\"https\")) { // 执⾏ Https 请求.

client = createSSLInsecureClient(); res = client.execute(post); } else {

// 执⾏ Http 请求.

client = HttpClientUtils.client; res = client.execute(post); }

return IOUtils.toString(res.getEntity().getContent(), \"UTF-8\"); } finally {

post.releaseConnection();

if (url.startsWith(\"https\") && client != null && client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } } }

/**

* 发送⼀个 GET 请求 *

* @param url

* @param charset

* @param connTimeout 建⽴链接超时时间,毫秒. * @param readTimeout 响应超时时间,毫秒. * @return

* @throws ConnectTimeoutException 建⽴链接超时 * @throws SocketTimeoutException 响应超时 * @throws Exception */

public static String get(String url, String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception { HttpClient client = null;

HttpGet get = new HttpGet(url); String result = \"\"; try {

// 设置参数

Builder customReqConf = RequestConfig.custom(); if (connTimeout != null) {

customReqConf.setConnectTimeout(connTimeout); }

if (readTimeout != null) {

customReqConf.setSocketTimeout(readTimeout); }

get.setConfig(customReqConf.build()); HttpResponse res = null;

if (url.startsWith(\"https\")) { // 执⾏ Https 请求.

client = createSSLInsecureClient(); res = client.execute(get); } else {

// 执⾏ Http 请求.

client = HttpClientUtils.client; res = client.execute(get); }

result = IOUtils.toString(res.getEntity().getContent(), charset); } finally {

get.releaseConnection();

if (url.startsWith(\"https\") && client != null && client instanceof CloseableHttpClient) { ((CloseableHttpClient) client).close(); } }

return result; }

/**

* 从 response ⾥获取 charset *

* @param ressponse * @return */

@SuppressWarnings(\"unused\")

private static String getCharsetFromResponse(HttpResponse ressponse) { // Content-Type:text/html; charset=GBK

if (ressponse.getEntity() != null && ressponse.getEntity().getContentType() != null && ressponse.getEntity().getContentType().getValue() != null) {

String contentType = ressponse.getEntity().getContentType().getValue(); if (contentType.contains(\"charset=\")) {

return contentType.substring(contentType.indexOf(\"charset=\") + 8); } }

return null; } /**

* 创建 SSL连接 *

* @return

* @throws GeneralSecurityException */

private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException { try {

SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; }

}).build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() { @Override

public boolean verify(String arg0, SSLSession arg1) { return true; }

@Override

public void verify(String host, SSLSocket ssl) throws IOException { }

@Override

public void verify(String host, X509Certificate cert) throws SSLException { }

@Override

public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { } });

return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (GeneralSecurityException e) { throw e; } }

public static void main(String[] args) { try {

String str = post(\"https://localhost:443/ssl/test.shtml\ \"application/x-www-form-urlencoded\ // String str=

// get(\"https://localhost:443/ssl/test.shtml?name=12&page=34\ /*

* Map map = new HashMap(); map.put(\"name\ * \"111\"); map.put(\"page\

* postForm(\"https://localhost:443/ssl/test.shtml\ */

System.out.println(str);

} catch (ConnectTimeoutException e) { e.printStackTrace();

} catch (SocketTimeoutException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }}

7、url转码⼯具类 URLEncodeUtil.java

import java.io.UnsupportedEncodingException;

public class URLEncodeUtil {

private final static String ENCODE = \"UTF-8\"; /**

* URL 解码 */

public static String getURLDecoderString(String str) { String result = \"\"; if (null == str) { return \"\"; } try {

result = java.net.URLDecoder.decode(str, ENCODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }

return result; } /**

* URL 转码 */

public static String getURLEncoderString(String str) { String result = \"\"; if (null == str) { return \"\"; } try {

result = java.net.URLEncoder.encode(str, ENCODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }

return result; }}

8、Controller层实现登录

import java.util.HashMap;import java.util.Map;

import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON;

import com.example.demo.Utlis.HttpClientUtils;import com.example.demo.Utlis.URLEncodeUtil;import com.example.demo.entity.Constants;import com.example.demo.entity.QQUserInfo;/**

* qq登录 *

* @author wangsong

* @date 2019年6⽉18⽇ 下午8:04:15 */

@Controller

public class QQController {

/**

* QQ :读取Appid相关配置信息静态类 */

@Autowired

private Constants constants; /**

* 登录页 */

@GetMapping(\"/\") public String login() { return \"login\"; }

/**

* 获得跳转到qq登录页的url,前台直接a连接访问 *

* @author wangsong

* @date 2019年6⽉18⽇ 下午8:29:20 * @param session * @return

* @throws Exception */

@GetMapping(\"/getQQCode\")

public String getCode(HttpSession session, Model model) throws Exception { // 拼接url

StringBuilder url = new StringBuilder();

url.append(\"https://graph.qq.com/oauth2.0/authorize?\"); url.append(\"response_type=code\");

url.append(\"&client_id=\" + constants.getQqAppId()); // 回调地址 ,回调地址要进⾏Encode转码

String redirect_uri = constants.getQqRedirectUrl(); // 转码

url.append(\"&redirect_uri=\" + URLEncodeUtil.getURLEncoderString(redirect_uri));

url.append(\"&state=ok\");

// HttpClientUtils.get(url.toString(), \"UTF-8\"); System.out.println(url.toString()); model.addAttribute(\"url\ return \"login\"; }

/**

* 开始登录 *

* @param code * @param

* @param 实际业务:token过期调⽤刷新 token重新获取token信息 * @param 数据库字段: accessToken,expiresIn,refreshToken,openId * @return

* @throws Exception */

@GetMapping(\"/QQLogin\") @ResponseBody

public QQUserInfo QQLogin(String code, Model model) throws Exception { if (code != null) {

System.out.println(code); }

//获取tocket

Map qqProperties = getToken(code); //获取openId(每个⽤户的openId都是唯⼀不变的) String openId = getOpenId(qqProperties); qqProperties.put(\"openId\

//tocket过期刷新token

//Map refreshToken = refreshToken(qqProperties); //获取数据

QQUserInfo userInfo = getUserInfo(qqProperties); return userInfo; }

/**

* 获得token信息(授权,每个⽤户的都不⼀致) --> 获得token信息该步骤返回的token期限为⼀个⽉ *

* @param (保存到Map qqProperties) * @author wangsong * @return

* @throws Exception

* @date 2019年6⽉18⽇ 下午8:56:45 */

public Map getToken(String code) throws Exception { StringBuilder url = new StringBuilder();

url.append(\"https://graph.qq.com/oauth2.0/token?\"); url.append(\"grant_type=authorization_code\");

url.append(\"&client_id=\" + constants.getQqAppId());

url.append(\"&client_secret=\" + constants.getQqAppSecret()); url.append(\"&code=\" + code); // 回调地址

String redirect_uri = constants.getQqRedirectUrl(); // 转码

url.append(\"&redirect_uri=\" + URLEncodeUtil.getURLEncoderString(redirect_uri)); // 获得token

String result = HttpClientUtils.get(url.toString(), \"UTF-8\"); System.out.println(\"url:\" + url.toString()); // 把token保存

String[] items = StringUtils.splitByWholeSeparatorPreserveAllTokens(result, \"&\"); String accessToken = StringUtils.substringAfterLast(items[0], \"=\");

Long expiresIn = new Long(StringUtils.substringAfterLast(items[1], \"=\")); String refreshToken = StringUtils.substringAfterLast(items[2], \"=\"); //token信息

Map qqProperties = new HashMap(); qqProperties.put(\"accessToken\

qqProperties.put(\"expiresIn\ qqProperties.put(\"refreshToken\ return qqProperties; }

/**

* 刷新token 信息(token过期,重新授权) *

* @return

* @throws Exception */

@GetMapping(\"/refreshToken\")

public Map refreshToken(Map qqProperties) throws Exception {

// 获取refreshToken

String refreshToken = (String) qqProperties.get(\"refreshToken\");

StringBuilder url = new StringBuilder(\"https://graph.qq.com/oauth2.0/token?\"); url.append(\"grant_type=refresh_token\");

url.append(\"&client_id=\" + constants.getQqAppId());

url.append(\"&client_secret=\" + constants.getQqAppSecret()); url.append(\"&refresh_token=\" + refreshToken); System.out.println(\"url:\" + url.toString());

String result = HttpClientUtils.get(url.toString(), \"UTF-8\"); // 把新获取的token存到map中

String[] items = StringUtils.splitByWholeSeparatorPreserveAllTokens(result, \"&\"); String accessToken = StringUtils.substringAfterLast(items[0], \"=\");

Long expiresIn = new Long(StringUtils.substringAfterLast(items[1], \"=\")); String newRefreshToken = StringUtils.substringAfterLast(items[2], \"=\"); //重置信息

qqProperties.put(\"accessToken\

qqProperties.put(\"expiresIn\ qqProperties.put(\"refreshToken\ return qqProperties; }

/**

* 获取⽤户openId(根据token) *

* @param 把openId存到map中 * @return

* @throws Exception */

public String getOpenId(Map qqProperties) throws Exception { // 获取保存的⽤户的token

String accessToken = (String) qqProperties.get(\"accessToken\"); if (!StringUtils.isNotEmpty(accessToken)) { // return \"未授权\"; }

StringBuilder url = new StringBuilder(\"https://graph.qq.com/oauth2.0/me?\"); url.append(\"access_token=\" + accessToken);

String result = HttpClientUtils.get(url.toString(), \"UTF-8\");

String openId = StringUtils.substringBetween(result, \"\\\"openid\\\":\\\"\ return openId; }

/**

* 根据token,openId获取⽤户信息 */

public QQUserInfo getUserInfo(Map qqProperties) throws Exception { // 取token

String accessToken = (String) qqProperties.get(\"accessToken\"); String openId = (String) qqProperties.get(\"openId\");

if (!StringUtils.isNotEmpty(accessToken) || !StringUtils.isNotEmpty(openId)) { return null; }

//拼接url

StringBuilder url = new StringBuilder(\"https://graph.qq.com/user/get_user_info?\"); url.append(\"access_token=\" + accessToken);

url.append(\"&oauth_consumer_key=\" + constants.getQqAppId()); url.append(\"&openid=\" + openId); // 获取qq相关数据

String result = HttpClientUtils.get(url.toString(), \"UTF-8\"); Object json = JSON.parseObject(result, QQUserInfo.class); QQUserInfo userInfo = (QQUserInfo) json; return userInfo; }}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 99spj.com 版权所有 湘ICP备2022005869号-5

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务