Spring Boot implements WeChat and QQ binding login

Spring Boot implements WeChat and QQ binding login

This article will introduce how to use Spring Boot to implement the binding login function of WeChat and QQ. We'll explain how to implement login integration for both social platforms through simple steps and code examples.

Preparation

Before you begin, make sure you have completed the following preparations:

  1. Register on the WeChat Open Platform and QQ Open Platform and obtain the corresponding AppID and AppSecret.
  2. Configure a callback URL for your application and ensure that the URL can be accessed externally.
  3. Install Spring Boot and its related dependencies.

Implement WeChat login

1. Add dependencies

pom.xmlAdd the following dependencies to the file :

<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. Configure WeChat login

application.propertiesAdd the following configuration to the file :

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

WebSecurityConfigAdd the following code in the class :

@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. Implement binding login

WeixinControllerAdd the following code in the class :

@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 "绑定成功";
    }
}

Implement QQ login

1. Add dependencies

pom.xmlAdd the following dependencies to the file :

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

2. Configure QQ login

application.propertiesAdd the following configuration to the file :

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

WebSecurityConfigAdd the following code in the class :

@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. Implement binding login

QQControllerAdd the following code in the class :

@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 "绑定成功";
    }
}

Summarize

Through the introduction of this article, we have learned how to use Spring Boot to implement the binding login function of WeChat and QQ. Now you can integrate the login of these two social platforms into your application to provide users with a more convenient login method. Of course, the above code is only an example, and you need to make corresponding adjustments and optimizations according to actual needs.

Guess you like

Origin blog.csdn.net/orton777/article/details/131480642