Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸
使用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常用方法
本文將從入門到精通,詳細(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ù)的示例詳解
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)中不當(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)一處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
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基本入門,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù)
這篇文章主要介紹了基于mybatis batch實(shí)現(xiàn)批量提交大量數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

