redis实现图形化验证码校验

1.导入依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>
		<!--kaptcha依赖包-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>kaptcha-spring-boot-starter</artifactId>
			<version>1.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

配置文件

server:
  port: 8080
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    #指定redis底层客户端实现
    client-type: lettuce
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        min-idle: 0
        max-wait: -1ms

2.工具类(可不写)
CommonUtil类用于获取ip地址和Md5加密,Ip地址用于组成key的一部分,唯一性。


import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;

public class CommonUtil{
   
    /**
     * 获取ip
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request){
   
        String ipAddress = null;
        try {
   
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我来回答你的问题。首先,我们需要在SpringBoot项目中引入Redis的依赖,可以在pom.xml文件中添加如下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,我们可以在项目中编写一个验证码的生成工具类,例如: ```java @Component public class VerificationCodeGenerator { private static final String CODES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static final int LENGTH = 6; public String generate() { StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < LENGTH; i++) { sb.append(CODES.charAt(random.nextInt(CODES.length()))); } return sb.toString(); } } ``` 该工具类可以生成一个包含数字和字母的6位验证码。 然后,我们可以在SpringBoot的Controller中编写登录的逻辑,例如: ```java @RestController public class LoginController { @Autowired private StringRedisTemplate redisTemplate; @Autowired private VerificationCodeGenerator verificationCodeGenerator; @PostMapping("/login") public String login(@RequestParam String username, @RequestParam String password, @RequestParam String code) { String key = "verification_code:" + username; String expectedCode = redisTemplate.opsForValue().get(key); if (!code.equals(expectedCode)) { return "验证码错误"; } // 验证码正确,继续登录逻辑 // ... return "登录成功"; } @GetMapping("/verificationCode") public String getVerificationCode(@RequestParam String username) { String code = verificationCodeGenerator.generate(); String key = "verification_code:" + username; redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES); return code; } } ``` 在这个例子中,我们通过一个GET请求获取验证码,将其存入Redis中,并设置过期时间为5分钟。然后,通过一个POST请求进行登录时,我们从Redis中获取到该用户对应的验证码,与用户输入的验证码进行比较,如果一致,则登录成功,否则返回错误信息。 以上就是整合Redis实现使用随机验证码登录的示例,希望对你有所帮助。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值