写这篇文章时,参考了一位大佬的文章,算是把大佬文章里一些不详细的部分补充完整了,但是核心的代码没有什么改动,只是抛弃掉了那个重定向的工具类,以下是大佬文章的链接:http://t.csdn.cn/0L7T4
准备
1、登录gitee
官网:https://gitee.com/
2、注册或登录账号
3、进入设置》第三方应用
中
4、创建应用
5、客户端id和密钥
6、至此准备工作就结束了
开始
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 网络请求 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- alibaba的fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
2、application.yml
spring:
thymeleaf:
cache: false
gitee:
oauth:
clientid: 5094679ccb97db605a5d9bc2843768521cefffb59cd9ed21a8eda086a69b5aa4
clientsecret: 82dbd8916d16169e0770ae3a28ce651e5792ded24fa78af4c0ad963ef8361300
callback: http://localhost:8080/callback
3、GiteeHttpClient
package com.t1.config;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class GiteeHttpClient {
/**
* 获取Access Token
* post
*/
public static JSONObject getAccessToken(String url) throws IOException {
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (null != entity) {
String result = EntityUtils.toString(entity, "UTF-8");
return JSONObject.parseObject(result);
}
httpPost.releaseConnection();
return null;
}
/**
* 获取用户信息
* get
*/
public static JSONObject getUserInfo(String url) throws IOException {
JSONObject jsonObject = null;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSONObject.parseObject(result);
}
httpGet.releaseConnection();
return jsonObject;
}
}
4、GiteeController
package com.t1.controller;
import com.alibaba.fastjson.JSONObject;
import com.t1.config.GiteeHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.UUID;
@Controller
public class GiteeController {
/**
* gitee授权中提供的 appid 和 appkey
*/
@Value("${gitee.oauth.clientid}")
public String CLIENTID;
@Value("${gitee.oauth.clientsecret}")
public String CLIENTSECRET;
@Value("${gitee.oauth.callback}")
public String URL;
/**
* 请求授权页面
*/
@GetMapping(value = "/auth")
public String qqAuth(HttpSession session) {
// 用于第三方应用防止CSRF攻击
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
session.setAttribute("state", uuid);
// Step1:获取Authorization Code
String url = "https://gitee.com/oauth/authorize?response_type=code" +
"&client_id=" + CLIENTID +
"&redirect_uri=" + URLEncoder.encode(URL) +
"&state=" + uuid +
"&scope=user_info";
//因为使用的是thymeleaf模板引擎,所以是无法解析一个网址的,只能重定向
return "redirect:"+url;
}
/**
* 授权回调
*/
@GetMapping(value = "/callback")
public String qqCallback(HttpServletRequest request) throws Exception {
HttpSession session = request.getSession();
// 得到Authorization Code
String code = request.getParameter("code");
// 我们放在地址中的状态码
String state = request.getParameter("state");
String uuid = (String) session.getAttribute("state");
// 验证信息我们发送的状态码
if (null != uuid) {
// 状态码不正确,直接返回登录页面
if (!uuid.equals(state)) {
return "index";
}
}
// Step2:通过Authorization Code获取Access Token
String url = "https://gitee.com/oauth/token?grant_type=authorization_code" +
"&client_id=" + CLIENTID +
"&client_secret=" + CLIENTSECRET +
"&code=" + code +
"&redirect_uri=" + URL;
JSONObject accessTokenJson = GiteeHttpClient.getAccessToken(url);
// Step3: 获取用户信息
url = "https://gitee.com/api/v5/user?access_token=" + accessTokenJson.get("access_token");
JSONObject jsonObject = GiteeHttpClient.getUserInfo(url);
/**
* 获取到用户信息之后,就该写你自己的业务逻辑了
*/
System.out.println(jsonObject);
return "hello";
}
}
5、index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a th:href="@{/auth}" class="link" title="Gitee登录">gitee登录</a>
</body>
</html>
6、hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>登陆成功,欢迎</h1>
</body>
</html>
7、测试
文章来源:https://www.toymoban.com/news/detail-522197.html
8、至此gitee的第三方登录就到此结束了。文章来源地址https://www.toymoban.com/news/detail-522197.html
到了这里,关于Gitee第三方登录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!