Springboot整合第三方登录功能的实现示例

 更新时间:2022年01月24日 10:23:49   作者:洛阳泰山  
本文主要介绍了Springboot整合第三方登录功能的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

springboot 项目的pom文件引入依赖

<dependency>
    <groupId>me.zhyd.oauth</groupId>
    <artifactId>JustAuth</artifactId>
    <version>{latest-version}</version>
</dependency>

代码

登录端点(controller)

 
import com.tarzan.cms.common.properties.SocialProperties;
import com.tarzan.cms.utils.SocialUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
/**
 * 第三方登陆端点
 *
 * @author tarzan
 */
 
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("auth")
@ConditionalOnProperty(value = "social.enabled", havingValue = "true")
@Api(value = "第三方登陆", tags = "第三方登陆端点")
public class BladeSocialEndpoint {
 
    private final SocialProperties socialProperties;
 
    /**
     * 授权完毕跳转
     */
    @ApiOperation(value = "授权完毕跳转(RequestMapping)")
    @RequestMapping("/oauth/render/{source}")
    public void renderAuth(@PathVariable("source") String source, HttpServletResponse response) throws IOException {
        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
        response.sendRedirect(authorizeUrl);
    }
 
    /**
     * 获取认证信息
     */
    @ApiOperation(value = "获取认证信息(RequestMapping)")
    @RequestMapping("/oauth/callback/{source}")
    public Object login(@PathVariable("source") String source, AuthCallback callback) {
        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
        return authRequest.login(callback);
    }
 
    /**
     * 撤销授权
     */
    @ApiOperation(value = "撤销授权(RequestMapping)")
    @RequestMapping("/oauth/revoke/{source}/{token}")
    public Object revokeAuth(@PathVariable("source") String source, @PathVariable("token") String token) {
        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
        return authRequest.revoke(AuthToken.builder().accessToken(token).build());
    }
 
    /**
     * 续期accessToken
     */
    @ApiOperation(value = "续期令牌(RequestMapping)")
    @RequestMapping("/oauth/refresh/{source}")
    public Object refreshAuth(@PathVariable("source") String source, String token) {
        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
        return authRequest.refresh(AuthToken.builder().refreshToken(token).build());
    }
 
}

工具类代码

 
import com.tarzan.cms.common.properties.SocialProperties;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*;
 
import java.util.Objects;
 
public class SocialUtil {
    public SocialUtil() {
    }
 
    public static AuthRequest getAuthRequest(String source, SocialProperties socialProperties) {
        AuthDefaultSource authSource = (AuthDefaultSource)Objects.requireNonNull(AuthDefaultSource.valueOf(source.toUpperCase()));
        AuthConfig authConfig = (AuthConfig)socialProperties.getOauth().get(authSource);
        if (authConfig == null) {
            throw new AuthException("未获取到有效的Auth配置");
        } else {
            AuthRequest authRequest = null;
            switch(authSource) {
                case GITHUB:
                    authRequest = new AuthGithubRequest(authConfig);
                    break;
                case GITEE:
                    authRequest = new AuthGiteeRequest(authConfig);
                    break;
                case OSCHINA:
                    authRequest = new AuthOschinaRequest(authConfig);
                    break;
                case QQ:
                    authRequest = new AuthQqRequest(authConfig);
                    break;
                case WECHAT_OPEN:
                    authRequest = new AuthWeChatOpenRequest(authConfig);
                    break;
                case WECHAT_ENTERPRISE:
                    authRequest = new AuthWeChatEnterpriseRequest(authConfig);
                    break;
                case WECHAT_MP:
                    authRequest = new AuthWeChatMpRequest(authConfig);
                    break;
                case DINGTALK:
                    authRequest = new AuthDingTalkRequest(authConfig);
                    break;
                case ALIPAY:
                    authRequest = new AuthAlipayRequest(authConfig);
                    break;
                case BAIDU:
                    authRequest = new AuthBaiduRequest(authConfig);
                    break;
                case WEIBO:
                    authRequest = new AuthWeiboRequest(authConfig);
                    break;
                case CODING:
                    authRequest = new AuthCodingRequest(authConfig);
                    break;
                case CSDN:
                    authRequest = new AuthCsdnRequest(authConfig);
                    break;
                case TAOBAO:
                    authRequest = new AuthTaobaoRequest(authConfig);
                    break;
                case GOOGLE:
                    authRequest = new AuthGoogleRequest(authConfig);
                    break;
                case FACEBOOK:
                    authRequest = new AuthFacebookRequest(authConfig);
                    break;
                case DOUYIN:
                    authRequest = new AuthDouyinRequest(authConfig);
                    break;
                case LINKEDIN:
                    authRequest = new AuthLinkedinRequest(authConfig);
                    break;
                case MICROSOFT:
                    authRequest = new AuthMicrosoftRequest(authConfig);
                    break;
                case MI:
                    authRequest = new AuthMiRequest(authConfig);
                    break;
                case TOUTIAO:
                    authRequest = new AuthToutiaoRequest(authConfig);
                    break;
                case TEAMBITION:
                    authRequest = new AuthTeambitionRequest(authConfig);
                    break;
                case PINTEREST:
                    authRequest = new AuthPinterestRequest(authConfig);
                    break;
                case RENREN:
                    authRequest = new AuthRenrenRequest(authConfig);
                    break;
                case STACK_OVERFLOW:
                    authRequest = new AuthStackOverflowRequest(authConfig);
                    break;
                case HUAWEI:
                    authRequest = new AuthHuaweiRequest(authConfig);
                    break;
                case KUJIALE:
                    authRequest = new AuthKujialeRequest(authConfig);
                    break;
                case GITLAB:
                    authRequest = new AuthGitlabRequest(authConfig);
                    break;
                case MEITUAN:
                    authRequest = new AuthMeituanRequest(authConfig);
                    break;
                case ELEME:
                    authRequest = new AuthElemeRequest(authConfig);
                    break;
                case TWITTER:
                    authRequest = new AuthTwitterRequest(authConfig);
            }
 
            if (null == authRequest) {
                throw new AuthException("未获取到有效的Auth配置");
            } else {
                return (AuthRequest)authRequest;
            }
        }
    }
}

登录配置类

import com.google.common.collect.Maps;
import lombok.Data;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
 
import java.util.Map;
 
@Data
@ConfigurationProperties(prefix = "social")
public class SocialProperties {
    private Boolean enabled = false;
    private String domain;
    private Map<AuthDefaultSource, AuthConfig> oauth = Maps.newHashMap();
    private Map<String, String> alias = Maps.newHashMap();
}

application.yml配置 以gitee为例

social:
  enabled: true
  domain: http://127.0.0.1
  oauth:
    QQ:
      client-id: xxx
      client-secret: xxx
      redirect-uri: http://127.0.0.1:8443/oauth/gitee/callback
    WECHAT_OPEN:
      client-id: xxxxxx
      client-secret: xxxxxx
      redirect-uri: http://127.0.0.1:8443/oauth/baidu/callback
    GITEE:
      client-id: 5b693811f8229e38146f2c482e3f4e4dfbdf2b496d494698b6308d6f35dcb2e0
      client-secret: 428ff220b5aa5704c55a8cf91f13aa4466258a6e7c357c7e30a5bca1d1cbe4e2
      redirect-uri: http://127.0.0.1/auth/oauth/callback/GITEE
 

gitee开放平台创建应用配置

请求地址

http://127.0.0.1/auth/oauth/render/gitee

跳转gitee登录

 登录后,自动回调,获取用户信息

更多平台的集成教程请看

QQ登录 | JustAuth 

到此这篇关于Springboot整合第三方登录功能的实现示例的文章就介绍到这了,更多相关Springboot 第三方登录内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java序列化和反序列化示例介绍

    Java序列化和反序列化示例介绍

    大家好,本篇文章主要讲的是Java序列化和反序列化示例介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • Java 模拟银行自助终端系统

    Java 模拟银行自助终端系统

    本系统模拟银行用户使用ATM机开户、查询、存款、取款功能,要求使用java语言编程实现。这篇文章主要介绍了Java 模拟银行自助终端系统的相关资料,需要的朋友可以参考下
    2016-10-10
  • 详解SpringBoot+Lucene案例介绍

    详解SpringBoot+Lucene案例介绍

    这篇文章主要介绍了详解SpringBoot+Lucene案例介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Spring Boot 集成Elasticsearch模块实现简单查询功能

    Spring Boot 集成Elasticsearch模块实现简单查询功能

    本文讲解了Spring Boot集成Elasticsearch采用的是ES模板的方式实现基础查询,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-06-06
  • 深入解析Java的设计模式编程中单例模式的使用

    深入解析Java的设计模式编程中单例模式的使用

    这篇文章主要介绍了深入解析Java的设计模式编程中单例模式的使用,一般来说将单例模式分为饿汉式单例和懒汉式单例,需要的朋友可以参考下
    2016-02-02
  • 关于@EnableGlobalMethodSecurity注解的用法解读

    关于@EnableGlobalMethodSecurity注解的用法解读

    这篇文章主要介绍了关于@EnableGlobalMethodSecurity注解的用法解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java实现限定时间CountDownLatch并行场景

    Java实现限定时间CountDownLatch并行场景

    本文将结合实例代码,介绍Java实现限定时间CountDownLatch并行场景,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
    2021-07-07
  • Springboot项目启动时如何用命令动态指定环境

    Springboot项目启动时如何用命令动态指定环境

    这篇文章主要介绍了Springboot项目启动时如何用命令动态指定环境的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • SpringBoot3集成RocketMq场景分析

    SpringBoot3集成RocketMq场景分析

    RocketMQ因其架构简单、业务功能丰富、具备极强可扩展性等特点被广泛应用,比如金融业务、互联网、大数据、物联网等领域的业务场景,这篇文章主要介绍了SpringBoot3集成RocketMq,需要的朋友可以参考下
    2023-08-08
  • 如何在Java中优雅地判空详解

    如何在Java中优雅地判空详解

    这篇文章主要大家介绍了关于如何在Java中优雅地判空的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11

最新评论