本文大綱
文章目錄
前言
爲了防止世界被破壞,爲了守護世界的和平。。。說錯了,重來~
爲了防止驗證系統被暴力破解,很多系統都增加了驗證碼效驗,比較常見的就是圖片二維碼,業內比較安全的是短信驗證碼,當然還有一些拼圖驗證碼,加入人工智能的二維碼等等,我們今天的主題就是前後端分離的圖片二維碼登錄方案。
前後端未分離的驗證碼登錄方案
傳統的項目大都是基於session交互的,前後端都在一個項目裏面,比如傳統的SSH項目或者一些JSP系統,當前端頁面觸發到獲取驗證碼請求,可以將驗證碼裏面的信息存在上下文中,所以登錄的時候只需要 用戶名
、密碼
、驗證碼
即可。
驗證碼生成流程如下
登錄驗證流程如下
可以發現,整個登錄流程還是依賴session上下文的,並且由後端調整頁面。
前後端分離的驗證碼登錄方案
隨着系統和業務的不停升級,前後端代碼放在一起的項目越來越臃腫,已經無法快速迭代和職責區分了,於是紛紛投入了前後端分離的懷抱,發現代碼和職責分離以後,開發效率越來越高了,功能迭代還越來越快,但是以前的驗證碼登錄方案就要更改了。
驗證碼生成流程如下
對比原來的方案,增加了redis中間件,不再是存在session裏面了,但是後面怎麼區分這個驗證碼是這個請求生成的呢?所以我們加入了唯一標識符來區分
登錄驗證流程如下
可以發現,基於前後端分離的分佈式項目登錄方案對比原來,加了一個redis中間件和token返回,不再依賴上下文session,並且頁面調整也是由後端換到了前端
動手擼輪子
基於驗證碼的輪子還是挺多的,本文就以Kaptcha
這個項目爲例,通過springboot項目集成Kaptcha
來實現驗證碼生成和登錄方案。
Kaptcha介紹
Kaptcha是一個基於SimpleCaptcha的驗證碼開源項目
我找的這個輪子是基於SimpleCaptcha二次封裝的,maven依賴如下
<!--Kaptcha是一個基於SimpleCaptcha的驗證碼開源項目-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
新建項目並加入依賴
依賴主要有 SpringBoot、Kaptcha、Redis
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lzp</groupId>
<artifactId>kaptcha</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--Kaptcha是一個基於SimpleCaptcha的驗證碼開源項目-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis依賴commons-pool 這個依賴一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Redis配置類RedisConfig
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
驗證碼配置類KaptchaConfig
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha producer(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "no");
properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "black");
properties.setProperty("kaptcha.image.width", "110");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.char.string","23456789abcdefghkmnpqrstuvwxyzABCDEFGHKMNPRSTUVWXYZ");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.textproducer.char.space","3");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑");
// properties.setProperty("kaptcha.obscurificator.impl","com.xxx");可以重寫實現類
properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
驗證碼控制層CaptchaController
爲了方便代碼寫一塊了,講究看
package com.lzp.kaptcha.controller;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.lzp.kaptcha.service.CaptchaService;
import com.lzp.kaptcha.vo.CaptchaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@RestController
@RequestMapping("/captcha")
public class CaptchaController {
@Autowired
private DefaultKaptcha producer;
@Autowired
private CaptchaService captchaService;
@ResponseBody
@GetMapping("/get")
public CaptchaVO getCaptcha() throws IOException {
// 生成文字驗證碼
String content = producer.createText();
// 生成圖片驗證碼
ByteArrayOutputStream outputStream = null;
BufferedImage image = producer.createImage(content);
outputStream = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", outputStream);
// 對字節數組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
String str = "data:image/jpeg;base64,";
String base64Img = str + encoder.encode(outputStream.toByteArray()).replace("\n", "").replace("\r", "");
CaptchaVO captchaVO =captchaService.cacheCaptcha(content);
captchaVO.setBase64Img(base64Img);
return captchaVO;
}
}
驗證碼返回對象CaptchaVO
package com.lzp.kaptcha.vo;
public class CaptchaVO {
/**
* 驗證碼標識符
*/
private String captchaKey;
/**
* 驗證碼過期時間
*/
private Long expire;
/**
* base64字符串
*/
private String base64Img;
public String getCaptchaKey() {
return captchaKey;
}
public void setCaptchaKey(String captchaKey) {
this.captchaKey = captchaKey;
}
public Long getExpire() {
return expire;
}
public void setExpire(Long expire) {
this.expire = expire;
}
public String getBase64Img() {
return base64Img;
}
public void setBase64Img(String base64Img) {
this.base64Img = base64Img;
}
}
Redis封裝類 RedisUtils
網上隨意找的,類裏面註明來源,將就用,代碼較多就不貼了,文末有代碼獲取
驗證碼方法層CaptchaService
package com.lzp.kaptcha.service;
import com.lzp.kaptcha.utils.RedisUtils;
import com.lzp.kaptcha.vo.CaptchaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class CaptchaService {
@Value("${server.session.timeout:300}")
private Long timeout;
@Autowired
private RedisUtils redisUtils;
private final String CAPTCHA_KEY = "captcha:verification:";
public CaptchaVO cacheCaptcha(String captcha){
//生成一個隨機標識符
String captchaKey = UUID.randomUUID().toString();
//緩存驗證碼並設置過期時間
redisUtils.set(CAPTCHA_KEY.concat(captchaKey),captcha,timeout);
CaptchaVO captchaVO = new CaptchaVO();
captchaVO.setCaptchaKey(captchaKey);
captchaVO.setExpire(timeout);
return captchaVO;
}
}
用戶登錄對象封裝LoginDTO
package com.lzp.kaptcha.dto;
public class LoginDTO {
private String userName;
private String pwd;
private String captchaKey;
private String captcha;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getCaptchaKey() {
return captchaKey;
}
public void setCaptchaKey(String captchaKey) {
this.captchaKey = captchaKey;
}
public String getCaptcha() {
return captcha;
}
public void setCaptcha(String captcha) {
this.captcha = captcha;
}
}
登錄控制層UserController
這塊我寫邏輯代碼了,相信大家都看的懂
package com.lzp.kaptcha.controller;
import com.lzp.kaptcha.dto.LoginDTO;
import com.lzp.kaptcha.utils.RedisUtils;
import com.lzp.kaptcha.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RedisUtils redisUtils;
@PostMapping("/login")
public UserVO login(@RequestBody LoginDTO loginDTO) {
Object captch = redisUtils.get(loginDTO.getCaptchaKey());
if(captch == null){
// throw 驗證碼已過期
}
if(!loginDTO.getCaptcha().equals(captch)){
// throw 驗證碼錯誤
}
// 查詢用戶信息
//判斷用戶是否存在 不存在拋出用戶名密碼錯誤
//判斷密碼是否正確,不正確拋出用戶名密碼錯誤
//構造返回到前端的用戶對象並封裝信息和生成token
return new UserVO();
}
}
驗證碼獲取和查看
代碼獲取
代碼已上傳地址庫,記得加個星標哦!
https://github.com/pengziliu/GitHub-code-practice/
還可查看往期更多精彩~