SpringBoot整合kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能

舉個栗子??

登錄是所有系統(tǒng)都繞不開的一道坎,很多系統(tǒng)會在用戶名和密碼下發(fā)放置一個圖形驗(yàn)證碼,例如:

image.png

這些圖形驗(yàn)證碼看起來不僅很丑,而且模糊,但卻是保護(hù)系統(tǒng)的第一道屏障,它的作用是:設(shè)計(jì)的初衷其實(shí)就是為了防自動化,防止一些人利用自動工具惡意攻擊網(wǎng)站,比如批量注冊,撐爆你的數(shù)據(jù)庫。

實(shí)現(xiàn)這個功能并不復(fù)雜,但是為了不讓大家重復(fù)造輪子,這里我給大家推薦一個現(xiàn)成的輪子:kaptcha,使用起來非常簡單,廢話不多說直接上代碼。

嘗試一下??

配置文件

SpringBoot項(xiàng)目中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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBoot-VerifyCode</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBoot-VerifyCode</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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>

        <!-- 驗(yàn)證碼生成包 -->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

項(xiàng)目代碼

項(xiàng)目結(jié)構(gòu)

image.png

SpringBootVerifyCodeApplication.java

package com.example.springbootverifycode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootVerifyCodeApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootVerifyCodeApplication.class, args);
    }

}

VerifyCodeConfig.java

package com.example.springbootverifycode.config;

import com.google.code.kaptcha.Producer;
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 VerifyCodeConfig {

    @Bean
    Producer producer() {
        Properties properties = new Properties();
        //設(shè)置圖片邊框
        properties.setProperty("kaptcha.border", "yes");
        //設(shè)置圖片邊框?yàn)樗{(lán)色
        properties.setProperty("kaptcha.border.color", "blue");
        //背景顏色漸變開始
        properties.put("kaptcha.background.clear.from", "127,255,212");
        //背景顏色漸變結(jié)束
        properties.put("kaptcha.background.clear.to", "240,255,255");
        // 字體顏色
        properties.put("kaptcha.textproducer.font.color", "black");
        // 文字間隔
        properties.put("kaptcha.textproducer.char.space", "10");
        //如果需要去掉干擾線
        properties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
        // 字體
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋體,楷體,微軟雅黑");
        // 圖片寬度
        properties.setProperty("kaptcha.image.width", "200");
        // 圖片高度
        properties.setProperty("kaptcha.image.height", "50");
        // 從哪些字符中產(chǎn)生
        properties.setProperty("kaptcha.textproducer.char.string", "0123456789abcdefghijklmnopqrsduvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        // 字符個數(shù)
        properties.setProperty("kaptcha.textproducer.char.length", "6");
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(new Config(properties));
        return defaultKaptcha;
    }
}

KaptchaController.java

package com.example.springbootverifycode.controller;

import com.google.code.kaptcha.Producer;
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.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class KaptchaController {

    @Autowired
    private Producer producer;

    @GetMapping("/getVerifyCode")
    public void getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("image/jpeg");
        String text = producer.createText();
        session.setAttribute("vf", text);
        BufferedImage image = producer.createImage(text);
        try (ServletOutputStream sos = response.getOutputStream()) {
            ImageIO.write(image, "jpg", sos);
        }
    }
}

測試一下

image.png

生成運(yùn)算符驗(yàn)證碼

1、設(shè)置字符個數(shù)為7

 // 文字個數(shù)
 properties.put("kaptcha.textproducer.char.space", "7");

2、獲取圖片

 @GetMapping("/getCal")
    public void getCal(HttpServletResponse response, HttpSession session) throws IOException {
        response.setContentType("image/jpeg");
        //生成文字驗(yàn)證碼
        String text = producer.createText();
        System.out.println("當(dāng)前生成的字符串為:" + text);
        //個位數(shù)字相加
        String s1 = text.substring(0, 2);
        String s2 = text.substring(2, 4);
        int count = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue();
        System.out.println("計(jì)算結(jié)果為:" + count);
        //生成圖片驗(yàn)證碼
        BufferedImage image = producer.createImage(s1 + "+" + s2 + "=?");
        try (ServletOutputStream sos = response.getOutputStream()) {
            ImageIO.write(image, "jpg", sos);
        }
    }

3、演示一下

后臺打?。?/strong>

image.png

訪問鏈接:

image.png

文末小彩蛋,自己花一個星期做的小網(wǎng)站,放出來給大家看看,網(wǎng)址如下:http://47.120.49.119:8080

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容