编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

springBoot + mysql + redis实现扫码登录

wxchong 2024-11-17 16:45:27 开源技术 17 ℃ 0 评论

实现扫码登录系统涉及多个组件和步骤。以下是一个使用Spring Boot、MySQL和Redis实现扫码登录的详细原理和代码示例。

原理概述

  1. 生成二维码:服务器端生成一个唯一的登录请求ID(例如UUID),并与用户标识(如用户名或邮箱)关联存储在Redis中。使用这个唯一ID生成二维码,返回给客户端展示。
  2. 用户扫码:用户使用移动设备扫描二维码,获取到唯一登录请求ID。移动设备发送这个ID以及用户的确认登录请求到服务器。
  3. 验证登录:服务器根据接收到的唯一ID查找关联的用户标识,验证用户登录信息(如密码、二次确认等)。如果验证成功,生成JWT(JSON Web Token)或Session信息,标记用户为已登录,并返回成功响应。
  4. 客户端处理登录结果:客户端根据服务器返回的登录结果,进行相应的处理(如跳转到首页或显示登录成功信息)。

1. 引入依赖

在pom.xml中添加相关依赖:

<dependencies>  
    <!-- Spring Boot Starter Web -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  
  
    <!-- Spring Boot Starter Data JPA -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-data-jpa</artifactId>  
    </dependency>  
  
    <!-- MySQL Connector -->  
    <dependency>  
        <groupId>mysql</groupId>  
        <artifactId>mysql-connector-java</artifactId>  
    </dependency>  
  
    <!-- Spring Boot Starter Data Redis -->  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-data-redis</artifactId>  
    </dependency>  
  
    <!-- QR Code Generation (Zxing) -->  
    <dependency>  
        <groupId>com.google.zxing</groupId>  
        <artifactId>core</artifactId>  
        <version>3.4.1</version>  
    </dependency>  
    <dependency>  
        <groupId>com.google.zxing</groupId>  
        <artifactId>javase</artifactId>  
        <version>3.4.1</version>  
    </dependency>  
  
    <!-- JWT -->  
    <dependency>  
        <groupId>io.jsonwebtoken</groupId>  
        <artifactId>jjwt</artifactId>  
        <version>0.9.1</version>  
    </dependency>  
</dependencies>

2. 配置Redis

在application.properties文件中配置Redis:

spring.redis.host=localhost  
spring.redis.port=6379  
spring.redis.timeout=6000

3. 生成二维码

创建QrCodeGenerator类生成二维码:

import com.google.zxing.BarcodeFormat;  
import com.google.zxing.WriterException;  
import com.google.zxing.client.j2se.MatrixToImageWriter;  
import com.google.zxing.common.BitMatrix;  
import com.google.zxing.qrcode.QRCodeWriter;  
  
import java.io.IOException;  
import java.nio.file.FileSystems;  
import java.nio.file.Path;  
import java.util.UUID;  
  
public class QrCodeGenerator {  
    public static String generateQrCode(String text, String filePath) throws WriterException, IOException {  
        QRCodeWriter qrCodeWriter = new QRCodeWriter();  
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 350, 350);  
          
        Path path = FileSystems.getDefault().getPath(filePath);  
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);  
          
        return path.toString();  
    }  
  
    public static void main(String[] args) throws WriterException, IOException {  
        String uuid = UUID.randomUUID().toString();  
        String filePath = "qrcode.png";  
        generateQrCode(uuid, filePath);  
        System.out.println("QR Code generated: " + filePath);  
    }  
}

4. 存储和验证扫码信息

使用Redis存储和验证扫码请求:

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.data.redis.core.StringRedisTemplate;  
import org.springframework.stereotype.Service;  
  
import java.util.concurrent.TimeUnit;  
  
@Service  
public class QrCodeService {  
    @Autowired  
    private StringRedisTemplate redisTemplate;  
  
    // 存储扫码请求(UUID -> 用户ID)  
    public void saveQrCodeRequest(String uuid, String userId) {  
        redisTemplate.opsForValue().set(uuid, userId, 10, TimeUnit.MINUTES); // 10分钟有效期  
    }  
  
    // 验证扫码请求  
    public String validateQrCodeRequest(String uuid) {  
        return redisTemplate.opsForValue().get(uuid);  
    }  
  
    // 删除扫码请求  
    public void deleteQrCodeRequest(String uuid) {  
        redisTemplate.delete(uuid);  
    }  
}

5. 控制层处理扫码登录

创建控制器处理扫码登录请求:

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.*;  
  
@RestController  
@RequestMapping("/qrlogin")  
public class QrLoginController {  
    @Autowired  
    private QrCodeService qrCodeService;  
  
    @PostMapping("/generate")  
    public String generateQrCode(@RequestParam String userId) {  
        String uuid = UUID.randomUUID().toString();  
        qrCodeService.saveQrCodeRequest(uuid, userId);  
        // 生成二维码并返回其路径(实际项目中会通过WebSocket等方式推送)  
        try {  
            return QrCodeGenerator.generateQrCode(uuid, "qrcode.png");  
        } catch (Exception e) {  
            e.printStackTrace();  
            return "Error generating QR code";  
        }  
    }  
  
    @PostMapping("/scan")  
    public String scanQrCode(@RequestParam String uuid, @RequestParam String confirmation) {  
        // 在实际应用中,confirmation可以是用户输入的验证码、密码等  
        String userId = qrCodeService.validateQrCodeRequest(uuid);  
        if (userId != null && "correct_confirmation".equals(confirmation)) { // 简单示例  
            qrCodeService.deleteQrCodeRequest(uuid);  
            // 生成JWT或处理Session  
            return "Login successful, JWT/Session created.";  
        } else {  
            return "Invalid QR code or confirmation.";  
        }  
    }  
}

以上示例展示了如何使用Spring Boot、MySQL和Redis实现扫码登录的基础架构和流程。实际项目中,还需考虑安全性(如HTTPS、JWT签名、验证码)、用户体验(如二维码过期刷新、扫码状态提示)、以及高并发处理等问题。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表