1. 程式人生 > 程式設計 >springboot整合kaptcha生成驗證碼功能

springboot整合kaptcha生成驗證碼功能

介紹:kaptcha 是谷歌開源的非常實用的驗證碼生成工具

一、匯入jar包

<!-- kaptcha驗證碼 -->
<dependency>
	<groupId>com.github.penggle</groupId>
	<artifactId>kaptcha</artifactId>
	<version>2.3.2</version>
</dependency>

二、編寫kaptcha配置類

package com.zym.config;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KaptchaConfig {

  @Bean
  public DefaultKaptcha defaultKaptcha(){
    DefaultKaptcha kaptcha = new DefaultKaptcha();

    Properties properties = new Properties();
    //邊框
    properties.setProperty("kaptcha.border","no");
    //字型顏色
    properties.setProperty("kaptcha.textproducer.font.color","green");
    //圖片寬度
    properties.setProperty("kaptcha.image.width","120");
    //圖片高度
    properties.setProperty("kaptcha.image.height","30");
    //字型大小
    properties.setProperty("kaptcha.textproducer.font.size","20");
    //session key
    properties.setProperty("kaptcha.session.key","kaptcha");
    //驗證碼長度
    properties.setProperty("kaptcha.textproducer.char.length","4");
    //字型
    properties.setProperty("kaptcha.textproducer.font.names","宋體");
    //文字間隔
    properties.setProperty("kaptcha.textproducer.char.space","10");
    //噪點實現類
    properties.setProperty("kaptcha.noise.impl","com.google.code.kaptcha.impl.NoNoise");
    //圖片樣式-陰影
    properties.setProperty("kaptcha.obscurificator.impl","com.google.code.kaptcha.impl.ShadowGimpy");

    Config config = new Config(properties);
    kaptcha.setConfig(config);
    return kaptcha;
  }
}

三、編寫介面

package com.zym.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class KaptchaController {

  @Autowired
  private DefaultKaptcha defaultKaptcha;

  @GetMapping("/kaptcha")
  public void getKaptcha(HttpServletRequest request,HttpServletResponse response) throws IOException {
    //設定響應頭
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");
    response.setContentType("image/jpeg");

    String text = defaultKaptcha.createText();
    HttpSession session = request.getSession();
    //將驗證碼存入session
    session.setAttribute("code",text);
    //建立驗證碼圖片
    BufferedImage image = defaultKaptcha.createImage(text);
    ServletOutputStream os = response.getOutputStream();
    ImageIO.write(image,"jpg",os);
    IOUtils.closeQuietly(os);
  }
}

四、測試介面

使用PostMan:
輸入url:localhost:8080/kaptcha
成功得到驗證碼圖片,大功告成!

在這裡插入圖片描述

到此這篇關於springboot整合kaptcha生成驗證碼功能的文章就介紹到這了,更多相關springboot整合kaptcha驗證碼內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!