欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年08月17日 10:27:09   作者:sun_t89  
本章簡單介紹redis的配置及使用方法,本文示例代碼在前面代碼的基礎(chǔ)上進(jìn)行修改添加,實(shí)現(xiàn)了使用redis進(jìn)行緩存驗(yàn)證碼,以及校驗(yàn)驗(yàn)證碼的過程。感興趣的的朋友一起看看吧

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

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

       <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的基本使用(緩存生成的驗(yàn)證碼信息)

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

@Autowired 
  private RedisTemplate<String, String> redisTemplate; 
//將驗(yàn)證碼以<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; //超時(shí)時(shí)間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) 
  { 
    //生成驗(yàn)證碼 
    String uuid = UUID.randomUUID().toString(); 
    Captcha captcha = new Captcha.Builder(captchaW, captchaH) 
        .addText().addBackground(new GradiatedBackgroundProducer()) 
        .gimp(new FishEyeGimpyRenderer()) 
        .build(); 
    //將驗(yàn)證碼以<key,value>形式緩存到redis 
    redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS); 
    //將驗(yàn)證碼key,及驗(yàn)證碼的圖片返回 
    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獲取驗(yàn)證碼)

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

修改JsonWebToken.java

@Autowired 
  private RedisTemplate<String, String> redisTemplate; 
//驗(yàn)證碼校驗(yàn)在后面章節(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)請(qǐng)求獲取驗(yàn)證碼,可以獲取到驗(yàn)證碼圖片,以及在cookie中返回緩存入redis的key值

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

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

如果驗(yàn)證碼錯(cuò)誤,返回驗(yàn)證碼錯(cuò)誤

驗(yàn)證碼正確,且用戶名密碼正確,返回token


總結(jié)

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

相關(guān)文章

  • Spring中的Devtools源碼解析

    Spring中的Devtools源碼解析

    這篇文章主要介紹了Spring中的Devtools源碼解析,Spring中的Devtools是一個(gè)開發(fā)工具,旨在提高開發(fā)人員的生產(chǎn)力和開發(fā)體驗(yàn),它提供了一系列功能,包括自動(dòng)重啟、熱部署、遠(yuǎn)程調(diào)試等,使開發(fā)人員能夠更快速地進(jìn)行代碼修改和調(diào)試,需要的朋友可以參考下
    2023-10-10
  • java IO流讀取圖片供前臺(tái)顯示代碼分享

    java IO流讀取圖片供前臺(tái)顯示代碼分享

    這篇文章主要介紹了java IO流讀取圖片供前臺(tái)顯示代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • mybatis?plus實(shí)現(xiàn)條件查詢

    mybatis?plus實(shí)現(xiàn)條件查詢

    這篇文章主要為大家介紹了mybatis?plus實(shí)現(xiàn)條件查詢,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 一文搞懂spring boot本地事務(wù)@Transactional參數(shù)

    一文搞懂spring boot本地事務(wù)@Transactional參數(shù)

    這篇文章主要介紹了spring boot本地事務(wù)@Transactional參數(shù)詳解,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】

    Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】

    這篇文章主要介紹了Java實(shí)現(xiàn)的可選擇及拖拽圖片的面板功能,涉及java基于swing組件選擇與操作圖片元素的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-01-01
  • Java如何實(shí)現(xiàn)讀取txt文件內(nèi)容并生成Word文檔

    Java如何實(shí)現(xiàn)讀取txt文件內(nèi)容并生成Word文檔

    本文主要介紹了通過Java實(shí)現(xiàn)讀取txt文件中的內(nèi)容,并將內(nèi)容生成Word文檔。文章的代碼非常詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2021-12-12
  • Java實(shí)現(xiàn)接口的枚舉類示例

    Java實(shí)現(xiàn)接口的枚舉類示例

    這篇文章主要介紹了Java實(shí)現(xiàn)接口的枚舉類,結(jié)合實(shí)例形式分析了java接口的枚舉類相關(guān)原理與使用技巧,需要的朋友可以參考下
    2019-08-08
  • java隨機(jī)生成8位數(shù)授權(quán)碼的實(shí)例

    java隨機(jī)生成8位數(shù)授權(quán)碼的實(shí)例

    下面小編就為大家?guī)硪黄猨ava隨機(jī)生成8位數(shù)授權(quán)碼的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • Spring的請(qǐng)求映射handlerMapping以及原理詳解

    Spring的請(qǐng)求映射handlerMapping以及原理詳解

    這篇文章主要介紹了Spring的請(qǐng)求映射handlerMapping以及原理詳解,我們每次發(fā)請(qǐng)求,它到底是怎么找到我們哪個(gè)方法來去處理這個(gè)請(qǐng)求,因?yàn)槲覀冎浪械恼?qǐng)求過來都會(huì)來到DispatcherServlet,springboot底層還是使用的是springMVC,需要的朋友可以參考下
    2023-08-08
  • java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例

    java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例

    這篇文章主要介紹了 java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評(píng)論