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

SpringBoot實(shí)現(xiàn)微信及QQ綁定登錄的示例代碼

 更新時(shí)間:2023年07月03日 09:55:37   作者:orton777  
本文主要介紹了SpringBoot實(shí)現(xiàn)微信及QQ綁定登錄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文將介紹如何使用 Spring Boot 實(shí)現(xiàn)微信和 QQ 的綁定登錄功能。我們將通過簡單的步驟和代碼示例來說明如何實(shí)現(xiàn)這兩種社交平臺(tái)的登錄集成。

準(zhǔn)備工作

在開始之前,確保你已經(jīng)完成以下準(zhǔn)備工作:

  • 注冊(cè)微信開放平臺(tái)和 QQ 開放平臺(tái),獲取相應(yīng)的 AppID 和 AppSecret。
  • 為你的應(yīng)用配置回調(diào) URL,并確保該 URL 能夠被外部訪問。
  • 安裝 Spring Boot 及其相關(guān)依賴。

實(shí)現(xiàn)微信登錄

1. 添加依賴

在 pom.xml 文件中添加以下依賴:

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-core</artifactId>
    <version>1.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-config</artifactId>
    <version>1.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-security</artifactId>
    <version>1.1.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-weixin</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>

2. 配置微信登錄

在 application.properties 文件中添加以下配置:

spring.social.weixin.app-id=你的微信AppID
spring.social.weixin.app-secret=你的微信AppSecret

在 WebSecurityConfig 類中添加以下代碼:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
? ? @Autowired
? ? private WeixinConnectionFactory weixinConnectionFactory;
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception {
? ? ? ? http
? ? ? ? ? ? .apply(springSocialConfigurer())
? ? ? ? ? ? .and()
? ? ? ? ? ? // 其他配置
? ? ? ? ;
? ? }
? ? @Bean
? ? public SpringSocialConfigurer springSocialConfigurer() {
? ? ? ? SpringSocialConfigurer configurer = new SpringSocialConfigurer();
? ? ? ? configurer.signupUrl("/signup");
? ? ? ? return configurer;
? ? }
? ? @Bean
? ? public ConnectionFactoryLocator connectionFactoryLocator() {
? ? ? ? ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
? ? ? ? registry.addConnectionFactory(weixinConnectionFactory);
? ? ? ? return registry;
? ? }
? ? @Bean
? ? public UsersConnectionRepository usersConnectionRepository() {
? ? ? ? return new InMemoryUsersConnectionRepository(connectionFactoryLocator());
? ? }
? ? @Bean
? ? public WeixinConnectionFactory weixinConnectionFactory() {
? ? ? ? return new WeixinConnectionFactory(
? ? ? ? ? ? environment.getProperty("spring.social.weixin.app-id"),
? ? ? ? ? ? environment.getProperty("spring.social.weixin.app-secret"));
? ? }
}

3. 實(shí)現(xiàn)綁定登錄

在 WeixinController 類中添加以下代碼:

@RestController
@RequestMapping("/weixin")
public class WeixinController {
? ? @Autowired
? ? private ConnectionFactoryLocator connectionFactoryLocator;
? ? @Autowired
? ? private UsersConnectionRepository usersConnectionRepository;
? ? @GetMapping("/signin")
? ? public String signin(HttpServletRequest request) {
? ? ? ? Connection<Weixin> connection = new OAuth2ConnectionFactory<Weixin>(
? ? ? ? ? ? (WeixinConnectionFactory) connectionFactoryLocator.getConnectionFactory(Weixin.class))
? ? ? ? ? ? .createConnection(new AccessGrant(request.getParameter("code")));
? ? ? ? // 綁定登錄邏輯
? ? ? ? // ...?
? ? ? ? return "綁定成功";
? ? }
}

實(shí)現(xiàn) QQ 登錄

1. 添加依賴

在 pom.xml 文件中添加以下依賴:

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-qq</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>

2. 配置 QQ 登錄

在 application.properties 文件中添加以下配置:

spring.social.qq.app-id=你的QQAppID
spring.social.qq.app-secret=你的QQAppSecret

在 WebSecurityConfig 類中添加以下代碼:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
? ? @Autowired
? ? private QQConnectionFactory qqConnectionFactory;
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception {
? ? ? ? http
? ? ? ? ? ? .apply(springSocialConfigurer())
? ? ? ? ? ? .and()
? ? ? ? ? ? // ? ? ? ? ? ?// 其他配置
? ? ? ? ;
? ? }
? ? @Bean
? ? public SpringSocialConfigurer springSocialConfigurer() {
? ? ? ? SpringSocialConfigurer configurer = new SpringSocialConfigurer();
? ? ? ? configurer.signupUrl("/signup");
? ? ? ? return configurer;
? ? }
? ? @Bean
? ? public ConnectionFactoryLocator connectionFactoryLocator() {
? ? ? ? ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
? ? ? ? registry.addConnectionFactory(qqConnectionFactory);
? ? ? ? return registry;
? ? }
? ? @Bean
? ? public UsersConnectionRepository usersConnectionRepository() {
? ? ? ? return new InMemoryUsersConnectionRepository(connectionFactoryLocator());
? ? }
? ? @Bean
? ? public QQConnectionFactory qqConnectionFactory() {
? ? ? ? return new QQConnectionFactory(
? ? ? ? ? ? environment.getProperty("spring.social.qq.app-id"),
? ? ? ? ? ? environment.getProperty("spring.social.qq.app-secret"));
? ? }
}

3. 實(shí)現(xiàn)綁定登錄

在 QQController 類中添加以下代碼:

@RestController
@RequestMapping("/qq")
public class QQController {
? ? @Autowired
? ? private ConnectionFactoryLocator connectionFactoryLocator;
? ? @Autowired
? ? private UsersConnectionRepository usersConnectionRepository;
? ? @GetMapping("/signin")
? ? public String signin(HttpServletRequest request) {
? ? ? ? Connection<QQ> connection = new OAuth2ConnectionFactory<QQ>(
? ? ? ? ? ? (QQConnectionFactory) connectionFactoryLocator.getConnectionFactory(QQ.class))
? ? ? ? ? ? .createConnection(new AccessGrant(request.getParameter("code")));
? ? ? ? // 綁定登錄邏輯
? ? ? ? // ...?
? ? ? ? return "綁定成功";
? ? }
}

總結(jié)

通過本文的介紹,我們已經(jīng)學(xué)會(huì)了如何使用 Spring Boot 實(shí)現(xiàn)微信和 QQ 的綁定登錄功能?,F(xiàn)在你可以將這兩種社交平臺(tái)的登錄集成到你的應(yīng)用中,為用戶提供更加便捷的登錄方式。當(dāng)然,以上代碼僅作為示例,你還需要根據(jù)實(shí)際需求進(jìn)行相應(yīng)的調(diào)整和優(yōu)化。

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)微信及QQ綁定登錄的示例代碼的文章就介紹到這了,更多相關(guān)Spring Boot微信QQ綁定登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringBoot?統(tǒng)一后端返回格式的方法

    詳解SpringBoot?統(tǒng)一后端返回格式的方法

    今天我們來聊一聊在基于SpringBoot前后端分離開發(fā)模式下,如何友好的返回統(tǒng)一的標(biāo)準(zhǔn)格式以及如何優(yōu)雅的處理全局異常,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-05-05
  • Java動(dòng)態(tài)腳本Groovy

    Java動(dòng)態(tài)腳本Groovy

    本文介紹了Java動(dòng)態(tài)腳本Groovy,Groovy是用于Java虛擬機(jī)的一種敏捷的動(dòng)態(tài)語言,它是一種成熟的面向?qū)ο缶幊陶Z言,既可以用于面向?qū)ο缶幊蹋挚梢杂米骷兇獾哪_本語言。使用該種語言不必編寫過多的代碼,同時(shí)又具有閉包和動(dòng)態(tài)語言中的其他特性,需要的朋友可以參考一下
    2021-12-12
  • Javadoc 具體使用詳解

    Javadoc 具體使用詳解

    這篇文章主要介紹了Javadoc 具體使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Spring整合Springmvc的相關(guān)介紹

    Spring整合Springmvc的相關(guān)介紹

    今天小編就為大家分享一篇關(guān)于Spring整合Springmvc的相關(guān)介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java中的setting和getting使用方法

    Java中的setting和getting使用方法

    為了保障數(shù)據(jù)的安全性,通常將數(shù)據(jù)成員定義為private(封裝或私有化),這樣外部代碼就無法直接訪問這些數(shù)據(jù),只能通過類提供的公共方法來進(jìn)行訪問,這種方法主要包括setter和getter方法,以及構(gòu)造方法,setter方法用于給私有屬性賦值
    2024-09-09
  • Maven項(xiàng)目外部jar包導(dǎo)入的實(shí)現(xiàn)示例

    Maven項(xiàng)目外部jar包導(dǎo)入的實(shí)現(xiàn)示例

    在Maven項(xiàng)目里,我們經(jīng)常需要導(dǎo)入jar包依賴,本文主要介紹了Maven項(xiàng)目外部jar包導(dǎo)入的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法

    這篇文章主要介紹了springboot中Controller內(nèi)文件上傳到本地及阿里云操作方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12
  • Java多線程之定時(shí)器Timer的實(shí)現(xiàn)

    Java多線程之定時(shí)器Timer的實(shí)現(xiàn)

    定時(shí)/計(jì)劃功能在Java應(yīng)用的各個(gè)領(lǐng)域都使用得非常多,比方說Web層面。本文主要為大家介紹了Java多線程中定時(shí)器Timer的實(shí)現(xiàn),感興趣的小伙伴可以了解一下
    2022-10-10
  • 簡單總結(jié)單例模式的4種寫法

    簡單總結(jié)單例模式的4種寫法

    今天帶大家學(xué)習(xí)java的相關(guān)知識(shí),文章圍繞著單例模式的4種寫法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • IDEA運(yùn)行SpringBoot項(xiàng)目的圖文教程

    IDEA運(yùn)行SpringBoot項(xiàng)目的圖文教程

    本文主要介紹了IDEA運(yùn)行SpringBoot項(xiàng)目的圖文教程,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05

最新評(píng)論