實例詳解SpringBoot實戰(zhàn)之Redis緩存登錄驗證碼

本章簡單介紹redis的配置及使用方法,本文示例代碼在前面代碼的基礎(chǔ)上進行修改添加,實現(xiàn)了使用redis進行緩存驗證碼,以及校驗驗證碼的過程。

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)與策劃設(shè)計,瀘州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:瀘州等地區(qū)。瀘州做網(wǎng)站價格咨詢:18982081108

1、添加依賴庫(添加redis庫,以及第三方的驗證碼庫)

       <dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-redis</artifactId> 
</dependency> 
<dependency> 
  <groupId>cn.apiclub.tool</groupId> 
  <artifactId>simplecaptcha</artifactId> 
  <version>1.2.2</version> 
</dependency> 

2、在application.properties中添加redis的配置信息

spring.redis.database=4 
spring.redis.host=hostname 
spring.redis.password=password 
spring.redis.port=6379 
spring.redis.timeout=2000 
spring.redis.pool.max-idle=8 
spring.redis.pool.min-idle=0 
spring.redis.pool.max-active=8 
spring.redis.pool.max-wait=-1 

3、添加redis數(shù)據(jù)模版

新增RedisConfig.Java

package com.xiaofangtech.sun.config; 
import org.springframework.context.annotation.Bean; 
import org.springframework.data.redis.connection.RedisConnectionFactory; 
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.serializer.StringRedisSerializer; 
public class RedisConfig { 
  @Bean 
  JedisConnectionFactory jedisConnectionFactory() { 
    return new JedisConnectionFactory(); 
  } 
  @Bean RedisTemplate<String, String>redisTemplate(RedisConnectionFactory factory) 
  { 
    RedisTemplate<String, String> template = new RedisTemplate<String, String>(); 
    template.setConnectionFactory(jedisConnectionFactory()); 
    template.setKeySerializer(new StringRedisSerializer()); 
    template.setValueSerializer(new StringRedisSerializer()); 
    return template; 
  } 
} 

4、redis的基本使用(緩存生成的驗證碼信息)

新建CaptchaModule.java,涉及redis插入操作關(guān)鍵代碼

@Autowired 
  private RedisTemplate<String, String> redisTemplate; 
//將驗證碼以<key,value>形式緩存到redis 
    redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS); 

完整代碼

package com.xiaofangtech.sunt.utils; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.UUID; 
import java.util.concurrent.TimeUnit; 
import javax.imageio.ImageIO; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.http.MediaType; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 
import cn.apiclub.captcha.Captcha; 
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer; 
import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer; 
@RestController 
@RequestMapping("captcha") 
public class CaptchaModule { 
  @Autowired 
  private RedisTemplate<String, String> redisTemplate; 
  private static int captchaExpires = 3*60; //超時時間3min 
  private static int captchaW = 200; 
  private static int captchaH = 60; 
  @RequestMapping(value = "getcaptcha", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE) 
  public @ResponseBody byte[] getCaptcha(HttpServletResponse response) 
  { 
    //生成驗證碼 
    String uuid = UUID.randomUUID().toString(); 
    Captcha captcha = new Captcha.Builder(captchaW, captchaH) 
        .addText().addBackground(new GradiatedBackgroundProducer()) 
        .gimp(new FishEyeGimpyRenderer()) 
        .build(); 
    //將驗證碼以<key,value>形式緩存到redis 
    redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS); 
    //將驗證碼key,及驗證碼的圖片返回 
    Cookie cookie = new Cookie("CaptchaCode",uuid); 
    response.addCookie(cookie); 
    ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
    try { 
      ImageIO.write(captcha.getImage(), "png", bao); 
      return bao.toByteArray(); 
    } catch (IOException e) { 
      return null; 
    } 
  } 
} 

5、redis內(nèi)容的獲取(根據(jù)key獲取驗證碼)

完善前面獲取token的流程,在獲取token的接口中添加校驗驗證碼的流程(根據(jù)登錄參數(shù)中的驗證碼id獲取驗證碼內(nèi)容,并與登錄參數(shù)中的驗證碼內(nèi)容進行比對)

修改JsonWebToken.java

@Autowired 
  private RedisTemplate<String, String> redisTemplate; 
//驗證碼校驗在后面章節(jié)添加 
String captchaCode = loginPara.getCaptchaCode(); 
try { 
  if (captchaCode == null) 
  { 
    throw new Exception(); 
  } 
  String captchaValue = redisTemplate.opsForValue().get(captchaCode); 
  if (captchaValue == null) 
  { 
    throw new Exception(); 
  } 
  redisTemplate.delete(captchaCode); 
  if (captchaValue.compareTo(loginPara.getCaptchaValue()) != 0) 
  { 
    throw new Exception(); 
  } 
} catch (Exception e) { 
  resultMsg = new ResultMsg(ResultStatusCode.INVALID_CAPTCHA.getErrcode(),  
      ResultStatusCode.INVALID_CAPTCHA.getErrmsg(), null); 
  return resultMsg; 
} 

6、測試

1)請求獲取驗證碼,可以獲取到驗證碼圖片,以及在cookie中返回緩存入redis的key值

實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼

2)查看redis,可以查看到之前緩存的key value

實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼

3)登錄獲取token時,添加驗證碼參數(shù)

如果驗證碼錯誤,返回驗證碼錯誤

實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼

驗證碼正確,且用戶名密碼正確,返回token

實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼

實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼

總結(jié)

以上所述是小編給大家介紹的實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

本文題目:實例詳解SpringBoot實戰(zhàn)之Redis緩存登錄驗證碼
URL地址:http://bm7419.com/article10/pcdsgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、面包屑導(dǎo)航做網(wǎng)站、云服務(wù)器、關(guān)鍵詞優(yōu)化服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)