最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot整合第三方登錄功能的實(shí)現(xiàn)示例

 更新時(shí)間:2022年01月24日 10:23:49   作者:洛陽(yáng)泰山  
本文主要介紹了Springboot整合第三方登錄功能的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

springboot 項(xiàng)目的pom文件引入依賴

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

代碼

登錄端點(diǎn)(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;
 
/**
 * 第三方登陸端點(diǎn)
 *
 * @author tarzan
 */
 
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("auth")
@ConditionalOnProperty(value = "social.enabled", havingValue = "true")
@Api(value = "第三方登陸", tags = "第三方登陸端點(diǎn)")
public class BladeSocialEndpoint {
 
    private final SocialProperties socialProperties;
 
    /**
     * 授權(quán)完畢跳轉(zhuǎn)
     */
    @ApiOperation(value = "授權(quán)完畢跳轉(zhuǎn)(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);
    }
 
    /**
     * 獲取認(rèn)證信息
     */
    @ApiOperation(value = "獲取認(rèn)證信息(RequestMapping)")
    @RequestMapping("/oauth/callback/{source}")
    public Object login(@PathVariable("source") String source, AuthCallback callback) {
        AuthRequest authRequest = SocialUtil.getAuthRequest(source, socialProperties);
        return authRequest.login(callback);
    }
 
    /**
     * 撤銷(xiāo)授權(quán)
     */
    @ApiOperation(value = "撤銷(xiāo)授權(quán)(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());
    }
 
    /**
     * 續(xù)期accessToken
     */
    @ApiOperation(value = "續(xù)期令牌(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開(kāi)放平臺(tái)創(chuàng)建應(yīng)用配置

請(qǐng)求地址

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

跳轉(zhuǎn)gitee登錄

 登錄后,自動(dòng)回調(diào),獲取用戶信息

更多平臺(tái)的集成教程請(qǐng)看

QQ登錄 | JustAuth 

到此這篇關(guān)于Springboot整合第三方登錄功能的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot 第三方登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC---配置與使用的示例

    SpringMVC---配置與使用的示例

    這篇文章主要介紹了SpringMVC---配置與使用的示例,幫助大家更好的理解和學(xué)習(xí)spring框架,感興趣的朋友可以了解下
    2020-10-10
  • mybatis if test條件判斷語(yǔ)句中的判斷問(wèn)題分析

    mybatis if test條件判斷語(yǔ)句中的判斷問(wèn)題分析

    這篇文章主要介紹了mybatis if test條件判斷語(yǔ)句中的判斷問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • ant打包jar文件腳本分享

    ant打包jar文件腳本分享

    本文介紹的ant腳本是用來(lái)打包jar文件,做完JAVA應(yīng)用一定會(huì)用到這個(gè),需要的朋友可以參考下
    2014-03-03
  • Java中的Lombok使用詳解

    Java中的Lombok使用詳解

    這篇文章主要介紹了Java中的Lombok使用詳解,Lombok是一個(gè)在Java開(kāi)發(fā)過(guò)程中使用注解的方式,用于簡(jiǎn)化JavaBean的編寫(xiě),避免冗余和樣板式代碼的插入,使類的編寫(xiě)更加簡(jiǎn)潔,需要的朋友可以參考下
    2023-08-08
  • MybatisPlus的LambdaQueryWrapper用法詳解

    MybatisPlus的LambdaQueryWrapper用法詳解

    LambdaQueryWrapper<Tag>?是 MyBatis-Plus 框架中的一個(gè)功能強(qiáng)大的查詢構(gòu)造器,它用于構(gòu)建 SQL 查詢條件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-10-10
  • Java list.remove( )方法注意事項(xiàng)

    Java list.remove( )方法注意事項(xiàng)

    這篇文章主要介紹了Java list.remove( )方法注意事項(xiàng),非常簡(jiǎn)單易懂,需要的朋友可以參考下
    2018-08-08
  • Java實(shí)現(xiàn)撲克牌洗牌和發(fā)牌

    Java實(shí)現(xiàn)撲克牌洗牌和發(fā)牌

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)撲克牌洗牌和發(fā)牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 建議你使用LocalDateTime而不是Date哦

    建議你使用LocalDateTime而不是Date哦

    這篇文章主要介紹了建議你使用LocalDateTime而不是Date,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Netty分布式固定長(zhǎng)度解碼器實(shí)現(xiàn)原理剖析

    Netty分布式固定長(zhǎng)度解碼器實(shí)現(xiàn)原理剖析

    這篇文章主要為大家介紹了Netty分布式固定長(zhǎng)度解碼器原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • 解決Maven多模塊編譯慢的問(wèn)題

    解決Maven多模塊編譯慢的問(wèn)題

    這篇文章主要介紹了Maven多模塊編譯慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論

甘谷县| 合山市| 正镶白旗| 星子县| 黔江区| 永登县| 洛阳市| 丰镇市| 武城县| 定结县| 石渠县| 西和县| 延川县| 台东市| 宜兰县| 深水埗区| 娄烦县| 山东| 安国市| 桃园县| 安丘市| 抚州市| 康平县| 英超| 民勤县| 福安市| 杭锦后旗| 玛纳斯县| 那曲县| 达拉特旗| 锦屏县| 枝江市| 通化市| 彩票| 英山县| 张家港市| 蒙自县| 磐石市| 泾川县| 鲜城| 肥东县|