How do I complete the verification code function in the open source system?

Written in front:

Recently, I had an idea to make a programmer mentoring system. Because I was very confused when I was learning java during college, I couldn't find my own direction, and I didn't have an experienced senior in society to guide me, so I took a lot of detours. Later, I started working and wanted to share my experience of avoiding pitfalls with others, but found that there were experienced developers around me, and there was no opportunity to share my ideas, so Fugui wanted to build a mentoring system for programmers. Adhering to the purpose that apprentices can be taught to avoid detours, and masters can be everywhere, so I started to build this master-student system, and I will also update the technology used in the system and share it with everyone as a tutorial. I hope everyone can pay attention to it. .

It is recommended to use gitee.com/ele-admin/E...  as a tool to realize the verification code function, because it is easy to use, simple, and can be used as a verification module of the system

The first step is to import the jar package

 
 

sql

copy code

<!--Redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--Redis--> <!--验证码--> <dependency> <groupId>com.github.whvcse</groupId> <artifactId>easy-captcha</artifactId> <version>1.6.2</version> </dependency> <!--验证码-->

The reason for using redis is to store verification codes

The second step is to write the redis configuration file

 
 

sql

copy code

package com.wangfugui.apprentice.common.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.concurrent.TimeUnit; /** * RedisUtils:redis工具类 */ @Component public class RedisUtils { @Autowired @Qualifier("getRedisTemplate") private RedisTemplate redisTemplate; @Autowired @Qualifier("getStringRedisTemplate") private StringRedisTemplate stringRedisTemplate; /** * 设置键值 * * @Param: [key, value] * @return: boolean * @Author: MaSiyi * @Date: 2021/11/20 */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 设置键值(string) * * @Param: [key, value] * @return: boolean * @Author: MaSiyi * @Date: 2021/11/20 */ public boolean setStr(final String key, String value) { boolean result = false; try { ValueOperations<String, String> operations = stringRedisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 设置键值(string) * * @Param: [key, value] * @return: boolean * @Author: MaSiyi * @Date: 2021/11/20 */ public boolean setStrEx(final String key, String value,long timeout, TimeUnit unit) { boolean result = false; try { stringRedisTemplate.opsForValue().set(key, value, timeout, unit); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存设置失效时间 * * @param key * @param value * @return */ public boolean setEx(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 设置key-value * <p> * 注: 若已存在相同的key, 那么原来的key-value会被丢弃 * * @param key key * @param value key对应的value * @param timeout 过时时长 * @param unit timeout的单位 * @date 2020/3/8 15:40:59 */ public void setEx(String key, String value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } /** * 批量删除对应的value */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 删除对应的value */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存当中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 根据key,获取到对应的value值 * * @param key key-value对应的key * @return 该key对应的值。 * 注: 若key不存在, 则返回null。 */ public Object get(String key) { return redisTemplate.opsForValue().get(key); } /** * 根据key,获取到对应的value(string)值 * * @param key key-value对应的key * @return 该key对应的值。 * 注: 若key不存在, 则返回null。 */ public String getStr(String key) { return stringRedisTemplate.opsForValue().get(key); } }

The reason for injecting two templates here is to distinguish the value of string type and object

The third step is to write the configuration class

 
 

sql

copy code

package com.wangfugui.apprentice.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { /** * 重写Redis序列化方式,使用Json方式: * 当我们的数据存储到Redis的时候,我们的键(key)和值(value) * 都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer, * StringRedisTemplate默认使用的是StringRedisSerializer。 * Spring Data JPA为我们提供了下面的Serializer: * GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、 * JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。 * 在此我们将自己配置RedisTemplate并定义Serializer。 * * @param redisConnectionFactory * @return */ @Bean("getRedisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); /** * 配置连接工厂 */ redisTemplate.setConnectionFactory(redisConnectionFactory); /** * 使用FJackson2JsonRedisSerializer序列化工具 */ FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); /** * 指定要序列化的域Field、set、get,以及修饰符范围 * ANY是都有,包括private、public */ objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); /** * 指定序列化输入的类型,类必须是非final修饰的, * final修饰的类,比如 * public final class User implements Serializable{},会包异常 */ objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); /** *设置键(key)的序列化采用StringRedisSerializer。 */ redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); /** * 设置值(value)的序列化采用jackson2JsonRedisSerializer。 */ redisTemplate.setValueSerializer(fastJsonRedisSerializer); redisTemplate.setHashValueSerializer(fastJsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } /** * 配置stringRedisTemplate序列化方式 * * @param redisConnectionFactory * @return */ @Bean("getStringRedisTemplate") @ConditionalOnMissingBean(StringRedisTemplate.class) public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }

Two templates are set here in the configuration class, corresponding to the template requirements of the second step

The fourth step is to write the controller class

 
 

sql

copy code

package com.wangfugui.apprentice.controller; import com.wangfugui.apprentice.common.util.RedisUtils; import com.wangfugui.apprentice.common.util.ResponseUtils; import com.wf.captcha.SpecCaptcha; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.UUID; import java.util.concurrent.TimeUnit; @Api(tags = "验证码") @RestController @RequestMapping("captcha") public class CaptchaController { @Autowired private RedisUtils redisUtil; @ResponseBody @ApiOperation("生成验证码") @GetMapping("/makeCaptcha") public ResponseUtils makeCaptcha() { SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); String verCode = specCaptcha.text().toLowerCase(); String key = UUID.randomUUID().toString(); // 存入redis并设置过期时间为30分钟 redisUtil.setStrEx(key, verCode, 30, TimeUnit.MINUTES); // 将key和base64返回给前端 HashMap<String, Object> map = new HashMap<>(); map.put("key", key); map.put("image", specCaptcha.toBase64()); return ResponseUtils.success(map); } @ResponseBody @ApiOperation("校验验证码") @PostMapping("/getCaptcha") public ResponseUtils getCaptcha(String verCode,String verKey){ // 获取redis中的验证码 String redisCode = redisUtil.getStr(verKey); // 判断验证码 if (verCode==null || !redisCode.equals(verCode.trim().toLowerCase())) { return ResponseUtils.error("验证码不正确"); } return ResponseUtils.success("验证成功"); } }

After that, just test it, generate a style example: 

say after

I will keep updating the mentoring system, because it is an open source project, so I also hope that more friends will join in! ! This is the address of the Programmer Mentoring Management System:  Programmer Mentoring Management System 

Guess you like

Origin blog.csdn.net/BASK2312/article/details/131359650