前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot整合JustAuth,实现第三方登录

Spring Boot整合JustAuth,实现第三方登录

作者头像
千羽
发布2021-01-14 15:07:16
1.5K0
发布2021-01-14 15:07:16
举报
文章被收录于专栏:程序员千羽程序员千羽

关于JustAuth

史上最全的整合第三方登录的开源库(官方是这样说的)

项目源码已放到GitHub: https://github.com/nateshao/SpringBoot-test

  • 个人博客
    • https://nateshao.gitee.io/
    • https://nateshao.github.io/

本次操作流程如下:

  • 创建Spring Boot项目
  • 导入依赖
  • 修改Client IDClient SecretredirectUri
  • 启动项目--完成

创建SpringBoot初始化项目

打开IEDA,点击File-New-Project-Spring Initializr,接着添加spring-boot-starter-web,spring-boot-devtools,lombok。

创建完成后的pom.xml,如下:

代码语言:javascript
复制
 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.3.0.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>com.stj</groupId>
     <artifactId>nateshao</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>nateshao</name>
     <description>Demo project for Spring Boot</description>
 
     <properties>
         <java.version>1.8</java.version>
     </properties>
 
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-devtools</artifactId>
             <scope>runtime</scope>
             <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
             <exclusions>
                 <exclusion>
                     <groupId>org.junit.vintage</groupId>
                     <artifactId>junit-vintage-engine</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
         <dependency>
             <groupId>me.zhyd.oauth</groupId>
             <artifactId>JustAuth</artifactId>
             <version>1.15.4-alpha</version>
         </dependency>
       
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
 
 </project>
 

然后再添加hutool-http,httpclient,okhttp。如下:

代码语言:javascript
复制
 <dependency>
             <groupId>cn.hutool</groupId>
             <artifactId>hutool-http</artifactId>
             <version>5.2.5</version>
         </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.5.12</version>
         </dependency>
         <dependency>
             <groupId>com.squareup.okhttp3</groupId>
             <artifactId>okhttp</artifactId>
             <version>4.4.1</version>
         </dependency>
     </dependencies>

编写controller

代码语言:javascript
复制
 package com.stj.controller;
 
 import me.zhyd.oauth.config.AuthConfig;
 import me.zhyd.oauth.model.AuthCallback;
 import me.zhyd.oauth.request.AuthGiteeRequest;
 import me.zhyd.oauth.request.AuthRequest;
 import me.zhyd.oauth.utils.AuthStateUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 
 /**
  * @author shaotongjie
  * @date 2020/6/12 10:51
  */
 @RestController
 @RequestMapping("/oauth")
 public class JustAuthController {
 
     /**
      * 获取授权链接并跳转到第三方授权页面
      *
      * @param response response
      * @throws IOException response可能存在的异常
      */
     @RequestMapping("/render/{source}")
     public void renderAuth(HttpServletResponse response) throws IOException {
         AuthRequest authRequest = getAuthRequest();
         String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
         response.sendRedirect(authorizeUrl);
    }
 
     /**
      * 用户在确认第三方平台授权(登录)后, 第三方平台会重定向到该地址,并携带code、state等参数
      *
      * @param callback 第三方回调时的入参
      * @return 第三方平台的用户信息
      */
     @RequestMapping("/callback/{source}")
     public Object login(AuthCallback callback) {
         AuthRequest authRequest = getAuthRequest();
         return authRequest.login(callback);
    }
 
     /**
      * 获取授权Request
      *
      * @return AuthRequest
      */
     private AuthRequest getAuthRequest() {
         return new AuthGiteeRequest(AuthConfig.builder()
                .clientId("clientId")           //clientId
                .clientSecret("clientSecret")   //clientSecret
                .redirectUri("redirectUri")     //redirectUri
                .build());
    }
 
 }

到了这里,就登录gitee,创建第三方应用

然后修改Client IDClient SecretredirectUri

代码语言:javascript
复制
 private AuthRequest getAuthRequest() {
         return new AuthGiteeRequest(AuthConfig.builder()
                .clientId("6a46c8bbe31b34dd4a5c54e73c828a4e3d3d022c1532cbfe652b11959538b0ce")
                .clientSecret("2d327f4c7e7081c9e9b1ee71828b9ffde9729bed3ef1def4eb25c0ff620a5dac")
                .redirectUri("http://localhost:8080/oauth/callback/gitee")
                .build());
    }

最后访问:http://localhost:8080/oauth/render/gitee

出现下面界面,说明成功啦!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 千羽的编程时光 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关于JustAuth
  • 创建SpringBoot初始化项目
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档