文章目录
1.需求背景2.Docker急速部署Redis3.搭建SpringBoot项目4.开发图形验证码
1.需求背景
很多人都用手机注册一些网站的验证了,比如手机验证码。先填手机号,然后发一条验证码过去,输入验证码,完成验证,注册成功。为了避免自己的网站被刷,增加图形验证码。当然为了防止网站被刷还不止这一种办法。2.Docker急速部署Redis
#创建文件夹mkdir -p /usr/local/data/redis/datadocker run -itd --name redis -p 6379:6379 -v /usr/local/data/redis/data:/data redis:6.2.4 --requirepass 123456
3.搭建SpringBoot项目
(1)添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> <scope>compile</scope> </dependency> </dependencies>
(2)创建启动主类
@SpringBootApplicationpublic class KaptchaApplication { public static void main(String[] args) { SpringApplication.run(KaptchaApplication.class, args); }}
(3)创建yml
server: port: 8012spring: application: name: kaptcha-server
(4)启动验证
4.开发图形验证码
(1)添加Maven依赖
<!--redis客户端--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!--kaptcha依赖包--> <dependency> <groupId>com.baomidou</groupId> <artifactId>kaptcha-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency>
(2)yml配置Redis
# 配置redis redis: # redis类型,jedis和lettuce client-type: jedis host: 192.168.139.101 password: 123456 port: 6379 jedis: pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 100 # 连接池中的最大空闲连接 max-idle: 100 # 连接池中的最小空闲连接 min-idle: 100 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: 60000
(3)配置CaptchaConfig
/** * 图形验证码配置类 * @author lixiang */@Configurationpublic class CaptchaConfig { /** * 验证码配置 * Kaptcha配置类名 * @return */ @Bean @Qualifier("captchaProducer") public DefaultKaptcha kaptcha() { DefaultKaptcha kaptcha = new DefaultKaptcha(); Properties properties = new Properties(); //properties.setProperty(Constants.KAPTCHA_BORDER, "yes"); //properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "220,220,220"); //properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "38,29,12"); //properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "147"); //properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "34"); //properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "25"); //properties.setProperty(Constants.KAPTCHA_SESSION_KEY, "code"); //验证码个数 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); //properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Courier"); //字体间隔 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8"); //干扰线颜色 //properties.setProperty(Constants.KAPTCHA_NOISE_COLOR, "white"); //干扰实现类 properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise"); //图片样式 properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple"); //文字来源 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789"); Config config = new Config(properties); kaptcha.setConfig(config); return kaptcha; }}
(4)配置RedisTemplate
/** * redisTemplate配置类 * @author lixiang */@Configurationpublic class RedisTemplateConfiguration { @Bean public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //配置序列化规则 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); //设置key-value的序列化规则 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); return redisTemplate; }}
(5)编写Controller测试
@RestController@RequestMapping("/kaptcha")@Slf4jpublic class KaptchaController { @Autowired private StringRedisTemplate redisTemplate; @Autowired private Producer captchaProducer; /** * 设置过期时间10min */ private static final long CAPTCHA_CODE_EXPIRED = 60 * 1000; /** * 获取图形验证码 * @param request * @param response */ @GetMapping("/get_captcha") public void getCaptcha(HttpServletRequest request, HttpServletResponse response){ //创建验证码内容 String captchaText = captchaProducer.createText(); log.info("图形验证码内容为:{}",captchaText); redisTemplate.opsForValue().set(getCaptchaKey(request),captchaText,CAPTCHA_CODE_EXPIRED, TimeUnit.MILLISECONDS); //生成图片 BufferedImage captchaImage = captchaProducer.createImage(captchaText); try(ServletOutputStream outputStream = response.getOutputStream()){ ImageIO.write(captchaImage,"jpg",outputStream); }catch (IOException e){ log.error("获取流出错:"+e.getMessage()); } } /** * 获取图形验证码的缓存key * @param request * @return */ private String getCaptchaKey(HttpServletRequest request) { String userAgent = request.getHeader("User-Agent"); return "account:captcha:"+ userAgent; }}
查看redis中的key