最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實(shí)現(xiàn)Thymeleaf驗(yàn)證碼生成

 更新時(shí)間:2021年05月14日 11:05:26   作者:將就嗎  
本文使用SpringBoot實(shí)現(xiàn)Thymeleaf驗(yàn)證碼生成,使用后臺(tái)返回驗(yàn)證碼圖片,驗(yàn)證碼存到session中后端實(shí)現(xiàn)校驗(yàn),前端只展示驗(yàn)證碼圖片。感興趣的可以了解下

使用后臺(tái)返回驗(yàn)證碼圖片,驗(yàn)證碼存到session中后端實(shí)現(xiàn)校驗(yàn),前端只展示驗(yàn)證碼圖片。

本篇用SpringBoot Thymeleaf實(shí)現(xiàn)驗(yàn)證碼生成。

創(chuàng)建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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- ThymeLeaf 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

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

</project>

application.yml配置 Thymeleaf

#Thymeleaf配置
spring:
  mvc:
    static-path-pattern: /**
  thymeleaf:
    mode: HTML
    encoding: UTF-8
    #關(guān)閉緩存
    cache: false

創(chuàng)建CaptchaController.java 類

package com.example.web.controller;

import com.example.web.util.VerifyCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class CaptchaController {
    /* 獲取驗(yàn)證碼圖片*/

    @RequestMapping("/getVerifyCode")
    public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            int width = 200;
            int height = 69;

            BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//生成對(duì)應(yīng)寬高的初始圖片
            String randomText = VerifyCode.drawRandomText(width, height, verifyImg);//單獨(dú)的一個(gè)類方法,出于代碼復(fù)用考慮,進(jìn)行了封裝。功能是生成驗(yàn)證碼字符并加上噪點(diǎn),干擾線,返回值為驗(yàn)證碼字符

            request.getSession().setAttribute("verifyCode", randomText);
            response.setContentType("image/png");//必須設(shè)置響應(yīng)內(nèi)容類型為圖片,否則前臺(tái)不識(shí)別

            OutputStream os = response.getOutputStream(); //獲取文件輸出流
            ImageIO.write(verifyImg, "png", os);//輸出圖片流
            os.flush();
            os.close();//關(guān)閉流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

創(chuàng)建VerifyCode.java 工具類

package com.example.web.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class VerifyCode {

    public static String drawRandomText(int width, int height, BufferedImage verifyImg) {

        Graphics2D graphics = (Graphics2D) verifyImg.getGraphics();
        graphics.setColor(Color.WHITE);//設(shè)置畫筆顏色-驗(yàn)證碼背景色
        graphics.fillRect(0, 0, width, height);//填充背景
        graphics.setFont(new Font("微軟雅黑", Font.BOLD, 40));

        //數(shù)字和字母的組合
        String baseNumLetter = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

        StringBuilder builder = new StringBuilder();
        int x = 10;  //旋轉(zhuǎn)原點(diǎn)的 x 坐標(biāo)
        String ch;
        Random random = new Random();

        for (int i = 0; i < 4; i++) {
            graphics.setColor(getRandomColor());

            //設(shè)置字體旋轉(zhuǎn)角度
            int degree = random.nextInt() % 30;  //角度小于30度
            int dot = random.nextInt(baseNumLetter.length());

            ch = baseNumLetter.charAt(dot) + "";
            builder.append(ch);

            //正向旋轉(zhuǎn)
            graphics.rotate(degree * Math.PI / 180, x, 45);
            graphics.drawString(ch, x, 45);

            //反向旋轉(zhuǎn)
            graphics.rotate(-degree * Math.PI / 180, x, 45);
            x += 48;
        }

        //畫干擾線
        for (int i = 0; i < 6; i++) {
            // 設(shè)置隨機(jī)顏色
            graphics.setColor(getRandomColor());

            // 隨機(jī)畫線
            graphics.drawLine(random.nextInt(width), random.nextInt(height),
                    random.nextInt(width), random.nextInt(height));

        }

        //添加噪點(diǎn)
        for (int i = 0; i < 30; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);

            graphics.setColor(getRandomColor());
            graphics.fillRect(x1, y1, 2, 2);
        }
        return builder.toString();
    }

    /**
     * 隨機(jī)取色
     */
    private static Color getRandomColor() {
        Random ran = new Random();
        return new Color(ran.nextInt(256),
                ran.nextInt(256), ran.nextInt(256));

    }
}

創(chuàng)建UserController.java 類

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
    @RequestMapping("/login")
    public String login() {
        return "login";
    }
}

resources/templates目錄下創(chuàng)建login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Show User</title>
</head>
<body>
<a href="javascript:void(0);" rel="external nofollow" title="點(diǎn)擊更換驗(yàn)證碼">
    <img th:src="@{getVerifyCode}" onclick="changeCode()" class="verifyCode"/>
</a>
</body>
<!-- 引入JQuery -->
<script src="../static/js/jquery.min.js" th:src="@{/js/jquery.min.js}"></script>
<script>
    function changeCode() {
        const src = "/getVerifyCode?" + new Date().getTime(); //加時(shí)間戳,防止瀏覽器利用緩存
        $('.verifyCode').attr("src", src);
    }
</script>
</html>

啟動(dòng)項(xiàng)目訪問http://localhost:8080/login


點(diǎn)擊圖片可以更換驗(yàn)證碼,至于后面的后臺(tái)驗(yàn)證就不講了。
參考文章后臺(tái)java 實(shí)現(xiàn)驗(yàn)證碼生成

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)Thymeleaf驗(yàn)證碼生成的文章就介紹到這了,更多相關(guān)SpringBoot Thymeleaf驗(yàn)證碼生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Scanner的使用和hasNextXXX()的用法說明

    Java Scanner的使用和hasNextXXX()的用法說明

    這篇文章主要介紹了Java Scanner的使用和hasNextXXX()的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 一文詳解Java如何使用commons-csv搞定CSV文件操作

    一文詳解Java如何使用commons-csv搞定CSV文件操作

    在?Java?開發(fā)中,處理?CSV(逗號(hào)分隔值)文件是一項(xiàng)常見的任務(wù),本文將利用Apache?Commons?CSV?庫(kù)實(shí)現(xiàn)讀取,寫入和操作CSV文件,希望對(duì)大家有所幫助
    2025-02-02
  • Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)

    Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)

    這篇文章主要介紹了Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 超詳細(xì)解釋Java反射

    超詳細(xì)解釋Java反射

    本文非常詳細(xì)的講解了java反射具體的內(nèi)容以及使用,java反射在現(xiàn)今的使用中很頻繁,希望此文可以幫大家解答疑惑,可以幫助大家理解
    2021-11-11
  • SpringMVC異常處理的三種方式

    SpringMVC異常處理的三種方式

    在SpringMVC中異常處理是一個(gè)重要的方面,它幫助我們有效地處理應(yīng)用程序中的異常情況,提高用戶體驗(yàn)和系統(tǒng)的穩(wěn)定性,這篇文章主要給大家介紹了關(guān)于SpringMVC異常處理的三種方式,需要的朋友可以參考下
    2024-02-02
  • spring配置文件解析失敗報(bào)”cvc-elt.1: 找不到元素 ''''beans'''' 的聲明”異常解決

    spring配置文件解析失敗報(bào)”cvc-elt.1: 找不到元素 ''''beans'''' 的聲明”異常解決

    這篇文章主要給大家介紹了關(guān)于spring配置文件解析失敗報(bào)”cvc-elt.1: 找不到元素 'beans' 的聲明”異常的解決方法,需要的朋友可以參考下
    2020-08-08
  • 詳解Spring中的AOP及AspectJ五大通知注解

    詳解Spring中的AOP及AspectJ五大通知注解

    這篇文章主要介紹了詳解Spring中的AOP及AspectJ五大通知注解,AOP面向切面編程是一種新的方法論,是對(duì)傳統(tǒng)OOP面向?qū)ο缶幊痰难a(bǔ)充,AOP?的主要編程對(duì)象是切面(aspect),切面模塊化橫切關(guān)注點(diǎn),需要的朋友可以參考下
    2023-08-08
  • springboot+mybatis配置clickhouse實(shí)現(xiàn)插入查詢功能

    springboot+mybatis配置clickhouse實(shí)現(xiàn)插入查詢功能

    這篇文章主要介紹了springboot+mybatis配置clickhouse實(shí)現(xiàn)插入查詢功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringSecurity攔截器鏈的使用詳解

    SpringSecurity攔截器鏈的使用詳解

    這篇文章主要介紹了SpringSecurity攔截器鏈的使用詳解,webSecurity的build方法最終調(diào)用的是doBuild方法,doBuild方法調(diào)用的是webSecurity的performBuild方法,webSecurity完成所有過濾器的插件,最終返回的是過濾器鏈代理類filterChainProxy,需要的朋友可以參考下
    2023-11-11
  • 聊聊Redis二進(jìn)制數(shù)組Bitmap

    聊聊Redis二進(jìn)制數(shù)組Bitmap

    這篇文章主要介紹了Redis二進(jìn)制數(shù)組Bitmap,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評(píng)論

曲沃县| 乌鲁木齐市| 平阳县| 鸡东县| 南昌县| 樟树市| 红河县| 小金县| 苏州市| 巴里| 武邑县| 如皋市| 平安县| 花莲县| 新和县| 咸丰县| 瑞金市| 土默特右旗| 屏山县| 临湘市| 扎兰屯市| 南开区| 敦煌市| 光山县| 木里| 华亭县| 登封市| 克拉玛依市| 盘山县| 咸丰县| 勐海县| 高雄县| 栖霞市| 林芝县| 清镇市| 绥中县| 郑州市| 耒阳市| 克什克腾旗| 竹溪县| 林芝县|