Spring Boot项目怎么集成Gitee登录

一、背景

现在的越来越多的项目,需要集成第三方系统进行登录。今天我们以Spring Boot项目集成Gitee为例,演示一下怎么使用Oauth2协议,集成第三方系统登录。

不了解oauth2的,可以看我之前的文章。Ouath2是怎么实现在第三方应用认证的?_oauth2 怎么向第三方应用备案-CSDN博客

二、项目中引入oauth2的包

 在项目中引入必要的依赖包。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

三、在Gitee上创建应用

Gitee主页 -> 设置 -> 第三方应用-> 创建应用

接下来 就是填写应用信息,可以参照下面的填写。也可以参考官方Gitee OAuth 文档

 其中,应用回调地址按照上面的格式只需要更改ip和端口。http://localhost:8080/login/oauth2/code/gitee

最后,创建应用后,拿到生成的Client IDClient Secret

四、配置Oauth2提供商信息

因为SpringSecurity的OAuth2中CommonOAuth2Provider只是预定义了几个比较知名的提供商:Google,Github,Facebook,Okta,因此对于这几个提供商,可以只配置clientId和clientSecret。

所以Gitee需要我们在application.yml中自己去定义其它的信息。参考官网

spring:
  security:
    oauth2:
      client:
        provider:
          gitee:
            authorization-uri: https://gitee.com/oauth/authorize
            token-uri: https://gitee.com/oauth/token
            user-info-uri: https://gitee.com/api/v5/user
            user-name-attribute: name
        registration:
          gitee:
            client-id: 15e9xxxxxxxxxxxxx2bc5d3274d56b70a90e0be4//替换为你自己的
            client-secret: 53dcxxxxxxxxxxx7c7728f2afca25d53748581da//替换为你自己的
            client-name: Gitee Sample
            scope: user_info
            authorization-grant-type: authorization_code
            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'

 其中比较重要的几个属性:

  • client-id、client-secre:创建应用后获取的。
  • authorization-grant-type:oauth2中有很多模式,我们使用授权码模式authorization_code
  • redirect-uri:回调地址,与创建的应用保持一致。
  • client-name:客户端名称
  • scope:能够获取到哪些信息权限
  • authorization-uri:授权服务器地址
  • token-uri:获取token地址
  • user-info-uri:获取用户信息的地址
  • user-name-attribute:用户信息中的用户名属性

五、编写一个Controller测试

配置好上面的供应商信息client-id、client-secre之后,我们接着来编写一个Controller来测试。

package com.sample.oauth2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
/**
 * REST controller for managing the current user's account.
 */
@RestController
public class AccountResource {
    private final Logger log = LoggerFactory.getLogger(AccountResource.class);

    @GetMapping("/account")
    public String getAccount(Principal principal) {
        log.info("current login user: {}", principal.getName());
        return "您好," + principal.getName();
    }
}

六、测试集成Gitee后的登录

1.启动项目

这个sample项目我会上传到gitee,拿到项目后执行 "mvn spring-boot:run"启动项目

2.访问登录页面

项目启动成功后,访问http://localhost:8080/login进入登录页面,应该可以看到以下页面。

3.点击Gitee Sample进行登录

 首先,会看到跳转到了gitee登录页面,进行登陆。输入自己的gitee账号密码。

最后,输入账号密码验证成功后,会跳出来一个授权页面。

我们点击同意授权。

4.验证是否登录成功 

授权成功后我们来验证一下,访问我们第五步,编写的接口 localhost:8080/account

页面上成功显示用户名

同时log中也打印出用户名

 至此,我们通过集成Gitee已经成功登录,并且拿到了用户的信息。

七、下载源码

上述测试的源码,我已经上传到gitee上面。有需要的自行下载。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值