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

Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸

 更新時(shí)間:2023年07月18日 09:01:50   作者:桂亭亭  
本文主要介紹了Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

使用Gitee進(jìn)行登陸

1.Gitee準(zhǔn)備工作

進(jìn)入gitee,在設(shè)置中選擇此選項(xiàng)

2. 編碼

依賴 

        <!--        第三方登陸justauth 引入-->
        <dependency>
            <groupId>com.xkcoding.justauth</groupId>
            <artifactId>justauth-spring-boot-starter</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!--        對象轉(zhuǎn)json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>

配置文件

justauth:
  # 是否啟用
  enabled: true
  type:
    # 配置各種類型的登陸
    GITEE:
      # 創(chuàng)建的應(yīng)用的client-id
      client-id: xx
      client-secret: xx
      # 自己寫的回調(diào)地址
      redirect-uri: http://127.0.0.1:8081/Auth/gitee/callback
  cache:
    type: default   

 接口編寫

package com.scm.myblog.controller;
import com.alibaba.fastjson.JSON;
import com.xkcoding.justauth.AuthRequestFactory;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;
@RestController
@RequestMapping("/Auth")
@Slf4j
public class UserAuthController {
    @Autowired
    private AuthRequestFactory factory;
    @GetMapping("/login/{type}")
    public void toLogin(@PathVariable String type, HttpServletResponse response) throws IOException {
        AuthRequest authRequest = factory.get(type);
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }
    @GetMapping("/{type}/callback")
    public AuthResponse loginBack(@PathVariable String type, AuthCallback callback) {
        AuthRequest authRequest = factory.get(type);
        log.info(JSON.toJSONString(callback));
        AuthResponse response = authRequest.login(callback);
        log.info(JSON.toJSONString(response));
        return response;
    }
}

如果有spring security的話,還要打開這兩個(gè)接口的訪問權(quán)限為所有人都可以訪問。

沒有的可忽略

package com.scm.myblog.config.securityconfig;
public class ApiConfig {
    //無需權(quán)限即可訪問的Api接口地址
    public static String [] NoAuthApi=new String[] {
            // 第三方登陸
            "/Auth/**",
    };
}
=------------------------------------=
package com.scm.myblog.config.securityconfig;
import com.scm.myblog.common.ExceptionLancer.MyAuthenticationException;
import com.scm.myblog.filter.AuthFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
//開啟權(quán)限管理系統(tǒng)
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private AuthFilter af;
    @Autowired
    private MyAuthenticationException myAuthenticationException;
    //密碼加密解密
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Override
    @Order(1)
    protected void configure(HttpSecurity http) throws Exception {
        //設(shè)置無需權(quán)限即可訪問的
        for (String n: ApiConfig.NoAuthApi){
            http.authorizeRequests().antMatchers(n).permitAll();
        }
        http
                //關(guān)閉csrf
                .csrf().disable()
                //不通過session獲取security上下文
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //其他的所有接口都需要帶token認(rèn)證
                .anyRequest().authenticated()
                .and().exceptionHandling().authenticationEntryPoint(myAuthenticationException);
        //配置自定義的過濾器在何處執(zhí)行
        //在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(af, UsernamePasswordAuthenticationFilter.class);
        //配置跨域請求
        http.cors();
    }
    //用于進(jìn)行用戶驗(yàn)證
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

啟動(dòng)測試

訪問:

http://localhost:8081/Auth/login/gitee

同意授權(quán)之后,會自動(dòng)跳轉(zhuǎn)到這里,這里有我們登陸成功后的信息

3.建立數(shù)據(jù)表

CREATE TABLE `oauth_platform` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) DEFAULT NULL COMMENT '平臺名稱',
  `description` varchar(100) DEFAULT NULL,
  `is_delete` int(11) DEFAULT NULL,
  `status` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='第三方認(rèn)證平臺信息表'
CREATE TABLE `oauth_user_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` varchar(20) DEFAULT NULL COMMENT 'OAuth用戶唯一的id',
  `username` varchar(30) DEFAULT NULL COMMENT 'OAuth用戶名',
  `avatar` varchar(120) DEFAULT NULL COMMENT 'OAuth平臺的頭像url',
  `oauth_token` varchar(50) DEFAULT NULL COMMENT '給的token',
  `oauth_expireIn` int(11) DEFAULT NULL COMMENT 'oauth的過期時(shí)間',
  `oauth_platform_id` int(11) DEFAULT NULL COMMENT '平臺id',
  `is_delete` int(11) DEFAULT NULL,
  `status` int(11) DEFAULT NULL COMMENT '狀態(tài)',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶第三方登陸信息表'
 

在代碼中將需要的信息插入表格,并把用戶的uid存入redis即可登陸成功!

到此這篇關(guān)于Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸的文章就介紹到這了,更多相關(guān)Springboot JustAuth第三方登陸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 從入門到精通Spring Boot中WebSocket常用方法

    從入門到精通Spring Boot中WebSocket常用方法

    本文將從入門到精通,詳細(xì)介紹Spring Boot中WebSocket的常用使用方法,涵蓋協(xié)議特性、消息收發(fā)、點(diǎn)對點(diǎn)通信、認(rèn)證攔截及注解實(shí)現(xiàn)方式,,需要的朋友跟隨小編一起看看吧
    2025-08-08
  • Java?DelayQueue實(shí)現(xiàn)延時(shí)任務(wù)的示例詳解

    Java?DelayQueue實(shí)現(xiàn)延時(shí)任務(wù)的示例詳解

    DelayQueue是一個(gè)無界的BlockingQueue的實(shí)現(xiàn)類,用于放置實(shí)現(xiàn)了Delayed接口的對象,其中的對象只能在其到期時(shí)才能從隊(duì)列中取走。本文就來利用DelayQueue實(shí)現(xiàn)延時(shí)任務(wù),感興趣的可以了解一下
    2022-08-08
  • Java循環(huán)創(chuàng)建對象內(nèi)存溢出的解決方法

    Java循環(huán)創(chuàng)建對象內(nèi)存溢出的解決方法

    在Java中,如果在循環(huán)中不當(dāng)?shù)貏?chuàng)建大量對象而不及時(shí)釋放內(nèi)存,很容易導(dǎo)致內(nèi)存溢出(OutOfMemoryError),所以本文給大家介紹了Java循環(huán)創(chuàng)建對象內(nèi)存溢出的解決方法,需要的朋友可以參考下
    2025-01-01
  • SpringBoot @ControllerAdvice 攔截異常并統(tǒng)一處理

    SpringBoot @ControllerAdvice 攔截異常并統(tǒng)一處理

    這篇文章主要介紹了SpringBoot @ControllerAdvice 攔截異常并統(tǒng)一處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java多線程中的ThreadPoolExecutor解讀

    Java多線程中的ThreadPoolExecutor解讀

    這篇文章主要介紹了Java多線程中的ThreadPoolExecutor解讀,線程池中的核心線程數(shù),當(dāng)提交一個(gè)任務(wù)時(shí),線程池創(chuàng)建一個(gè)新線程執(zhí)行任務(wù),直到當(dāng)前線程數(shù)等于corePoolSize;如果當(dāng)前線程數(shù)為corePoolSize,繼續(xù)提交的任務(wù)被保存到阻塞隊(duì)列中,等待被執(zhí)行,需要的朋友可以參考下
    2023-09-09
  • Java結(jié)合Spark的數(shù)據(jù)清洗場景及對應(yīng)的實(shí)現(xiàn)方法

    Java結(jié)合Spark的數(shù)據(jù)清洗場景及對應(yīng)的實(shí)現(xiàn)方法

    在大數(shù)據(jù)處理中,數(shù)據(jù)清洗是非常重要的一步,數(shù)據(jù)清洗可以幫助我們?nèi)コK數(shù)據(jù)、處理缺失值、規(guī)范數(shù)據(jù)格式等,以確保數(shù)據(jù)質(zhì)量和準(zhǔn)確性,在本文中,我們將介紹如何使用Java結(jié)合Spark框架來實(shí)現(xiàn)數(shù)據(jù)清洗,需要的朋友可以參考下
    2025-05-05
  • Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門教程

    Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門教程

    這篇文章主要介紹了Spring Boot 集成Shiro的多realm實(shí)現(xiàn)以及shiro基本入門,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù)

    基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù)

    這篇文章主要介紹了基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Cache 最佳實(shí)踐總結(jié)

    Spring Cache 最佳實(shí)踐總結(jié)

    SpringCache是Spring框架提供的聲明式緩存抽象層,通過少量注解即可為應(yīng)用添加緩存能力,無需侵入業(yè)務(wù)代碼,本文給大家介紹Spring Cache 最佳實(shí)踐總結(jié),感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • JAVA控制流程break?continue的示例代碼

    JAVA控制流程break?continue的示例代碼

    JAVA流程控制中有相關(guān)代碼可以終止整個(gè)流程的進(jìn)程,他們就是(break和continue),本文通過實(shí)例代碼介紹下JAVA控制流程break?continue的相關(guān)知識,感興趣的朋友一起看看吧
    2022-03-03

最新評論

安仁县| 沙洋县| 张家港市| 凤阳县| 焦作市| 贞丰县| 阿城市| 凭祥市| 出国| 板桥市| 鹤壁市| 应用必备| 航空| 融水| 曲阜市| 玛沁县| 成安县| 昌图县| 同仁县| 苗栗市| 六盘水市| 芜湖县| 临湘市| 宣汉县| 安塞县| 富源县| 丽水市| 红桥区| 溆浦县| 札达县| 固镇县| 来宾市| 黎平县| 上高县| 灵石县| 阜城县| 聊城市| 比如县| 东宁县| 衡东县| 田东县|