Gitee第三方登录

这篇具有很好参考价值的文章主要介绍了Gitee第三方登录。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

写这篇文章时,参考了一位大佬的文章,算是把大佬文章里一些不详细的部分补充完整了,但是核心的代码没有什么改动,只是抛弃掉了那个重定向的工具类,以下是大佬文章的链接:http://t.csdn.cn/0L7T4


准备

1、登录gitee

官网:https://gitee.com/

2、注册或登录账号

3、进入设置》第三方应用

gitee登录,第三方登录,java,http,https

4、创建应用
gitee登录,第三方登录,java,http,https

5、客户端id和密钥
gitee登录,第三方登录,java,http,https
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、测试
gitee登录,第三方登录,java,http,https


gitee登录,第三方登录,java,http,https


gitee登录,第三方登录,java,http,https
8、至此gitee的第三方登录就到此结束了。文章来源地址https://www.toymoban.com/news/detail-522197.html


到了这里,关于Gitee第三方登录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • spring boot整合第三方微信开发工具 weixin-java-miniapp 实现小程序微信登录

    有时候项目需要用到微信登录或获取用户的手机号码,weixin-java-miniapp是一个好用的第三方工具,不用我们自己写httpcline调用。 导入jar包 添加一个resource.properties文件,写上小程序的appid和secret 添加两个配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序给微信发送消息

    2024年02月16日
    浏览(20)
  • Springboot整合第三方登录

    Springboot整合第三方登录 为什么采用第三方登录 ​ 采用第三方登录可以避免重新注册账号的繁琐,也不需要再为密码和昵称发愁,而第三方登录有一个比较好用的包,里面整合了多种第三方登录,开箱即用,非常方便。就是JustAuth,网址https://www.justauth.cn/。 整合第三方登录

    2024年02月09日
    浏览(20)
  • nginx如何代理转发第三方https网站

    这里准备官方nginx镜像 上诉示例中,如果是https,proxy_ssl_session_reuse ,proxy_ssl_server_name ,这两个没有会报ssl SSL_do_handshake() failed SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:SSL alert number 40) while SSL handshaking to upstream, client: 127.0.0.1, server: 127

    2024年02月13日
    浏览(29)
  • 微信小程序第三方登录

    目录 小程序第三方登录操作流程如下: 1.第一步 2.第二步 2.第三步 4.第四步 5.第五步 注意:如果第一步没打印出来,看看微信模拟器上的Id有没有更改,或则去源码试图,weixinId更改 进入uniapp官网=Api=第三方服务=登录=微信小程序登录 创建一个触发事件,获取头像和名称 调用

    2024年02月13日
    浏览(30)
  • 实现 Google 第三方授权登录

    最近做项目要实现Google的第三方登录,这简单的记录一下。 目前Google的第三方登录有很多方案,且官方提供SDK方便接入。但是我这个项目同时要实现网页和客户端。所以选择了 Google OAuth 2.0 的方案。 当然,也可以 《实现 Facebook 第三方授权登录》 Google 的 OAuth 2.0 流程 首先

    2024年02月09日
    浏览(19)
  • 请求第三方Https地址忽略SSL证书校验

    说明:个人使用记录 需要在请求之前忽略ssl协议,这里是直接使用静态方法初始化时就执行了 也需要在请求接口之前忽略SSL

    2024年04月10日
    浏览(23)
  • PHP Twitter 推特 第三方登录

    twitter登录文档 开发者平台 申请成为开发流程按引导操作就可以了, 但是要注意信息填写要真实完善的信息, 否则容易被拒绝(被拒绝可能不能二次申请, 之前不能, 现在不知道能不能) 目前推特一个号只能开通一个免费应用, 可付费开通多个 设置基础信息 User authentication setting

    2024年04月16日
    浏览(18)
  • 若依 关于 springsecurity 不用密码登录,自定义第三方登录、免登录

    用的是若依的前后端分离的版本,项目接口是给小程序用 openid 直接免登录 找到登录方法 他这是根据用户名和密码进行比对、由于密码没办法转换成明文 只能改成如下方法免登录

    2024年02月07日
    浏览(28)
  • 使用开源项目JustAuth完成第三方登录

    JustAuth项目源码地址:https://github.com/justauth/JustAuth JustAuth文档地址:https://justauth.wiki/guide/quickstart/oauth/ 此demo的项目地址:xfeng520/JustAuthDemo (gitee.com) 开发者 指使用 JustAuth 的开发者 第三方 指开发者对接的第三方网站,比如:QQ平台、微信平台、微博平台 用户 指最终服务的真实

    2023年04月22日
    浏览(62)
  • uniapp 对接谷歌第三方登录

    1.登录谷歌开发者后台 https://console.developers.google.com/ 2.添加凭证 3.拿到客户端id后,项目中配置google登录:  示例代码:

    2024年04月29日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包