欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年07月18日 09:01:50   作者:桂亭亭  
本文主要介紹了Springboot使用JustAuth實(shí)現(xiàn)各種第三方登陸,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(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>
        <!--        對(duì)象轉(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);
        //配置跨域請(qǐng)求
        http.cors();
    }
    //用于進(jìn)行用戶驗(yàn)證
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

啟動(dòng)測(cè)試

訪問:

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

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

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

CREATE TABLE `oauth_platform` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) DEFAULT NULL COMMENT '平臺(tái)名稱',
  `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)證平臺(tái)信息表'
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平臺(tái)的頭像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 '平臺(tái)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java?ThreadPoolExecutor的拒絕策略

    詳解Java?ThreadPoolExecutor的拒絕策略

    這篇文章主要介紹了Java?ThreadPoolExecutor的拒絕策略,本文對(duì)于線程的池的幾種策略進(jìn)行詳細(xì)的講解,在實(shí)際的生產(chǎn)中需要集合相關(guān)的場(chǎng)景來選擇合適的拒絕策略,需要的朋友可以參考下
    2022-08-08
  • spring boot openfeign從此和httpClient說再見詳析

    spring boot openfeign從此和httpClient說再見詳析

    這篇文章主要給大家介紹了關(guān)于spring boot openfeign從此和httpClient說再見的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2018-06-06
  • 解決IntelliJ IDEA中鼠標(biāo)拖動(dòng)選擇為矩形區(qū)域問題

    解決IntelliJ IDEA中鼠標(biāo)拖動(dòng)選擇為矩形區(qū)域問題

    這篇文章主要介紹了解決IntelliJ IDEA中鼠標(biāo)拖動(dòng)選擇為矩形區(qū)域問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理實(shí)例解析

    Java JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理實(shí)例解析

    這篇文章主要介紹了Java JDK動(dòng)態(tài)代理實(shí)現(xiàn)原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Springboot如何利用攔截器攔截請(qǐng)求信息收集到日志詳解

    Springboot如何利用攔截器攔截請(qǐng)求信息收集到日志詳解

    一些系統(tǒng)經(jīng)常需要關(guān)注用戶請(qǐng)求的具體信息,如用戶信息、請(qǐng)求參數(shù)、響應(yīng)結(jié)果等等,在SpringBoot應(yīng)用中可通過攔截器的方式統(tǒng)一處理,下面這篇文章主要給大家介紹了關(guān)于Springboot如何利用攔截器攔截請(qǐng)求信息收集到日志的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能

    SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能

    這篇文章主要介紹了springboot事件監(jiān)聽,通過利用切面、注解、反射實(shí)現(xiàn),接下來將對(duì)這幾種方式逐一說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2022-07-07
  • 關(guān)于Controller 層返回值的公共包裝類的問題

    關(guān)于Controller 層返回值的公共包裝類的問題

    本文給大家介紹Controller 層返回值的公共包裝類-避免每次都包裝一次返回-InitializingBean增強(qiáng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09
  • 詳解SpringBoot Mongo 自增長(zhǎng)ID有序規(guī)則

    詳解SpringBoot Mongo 自增長(zhǎng)ID有序規(guī)則

    本文主要介紹springboot基于mongodb有序id生成,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java Socket UDP實(shí)例詳解

    java Socket UDP實(shí)例詳解

    這篇文章主要介紹了java Socket UDP實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java如何實(shí)現(xiàn)抽取json文件指定字段值

    java如何實(shí)現(xiàn)抽取json文件指定字段值

    這篇文章主要介紹了java如何實(shí)現(xiàn)抽取json文件指定字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-06-06

最新評(píng)論