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

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

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

个人博客

https://nateshao.gitee.io

https://nateshao.github.io

本次操作流程如下:

创建Spring Boot项目

导入依赖

修改Client ID,Client Secret,redirectUri

启动项目–完成

创建SpringBoot初始化项目
打开IEDA,点击File-New-Project-Spring Initializr,接着添加spring-boot-starter-web,spring-boot-devtools,lombok。

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



4.0.0
org.springframework.boot spring-boot-starter-parent 2.3.0.RELEASE
com.stj
nateshao
0.0.1-SNAPSHOT
nateshao
Demo project for Spring Boot

 <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>

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

cn.hutool hutool-http 5.2.5
org.apache.httpcomponents httpclient 4.5.12
com.squareup.okhttp3 okhttp 4.4.1

编写controller

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 ID,Client Secret,redirectUri

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

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

声明:来自程序员千羽,仅代表创作者观点。链接:https://eyangzhen.com/7674.html

程序员千羽的头像程序员千羽

相关推荐

添加微信
添加微信
Ai学习群
返回顶部