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

SpringBoot實(shí)現(xiàn)驗(yàn)證碼的案例分享

 更新時(shí)間:2024年11月04日 08:41:26   作者:vampire-wpre  
驗(yàn)證碼可以有效防止其他人對(duì)某一個(gè)特定的注冊(cè)用戶用特定的程序,破解方式進(jìn)行不斷的登錄嘗試,我們其實(shí)很經(jīng)??吹?登錄一些網(wǎng)站其實(shí)是需要驗(yàn)證碼的,所以本文給大家分享了SpringBoot實(shí)現(xiàn)驗(yàn)證碼的案例,需要的朋友可以參考下

實(shí)現(xiàn)邏輯

1、后端功能:隨機(jī)生成驗(yàn)證碼圖片,并把交給前端、接收用戶輸入,驗(yàn)證用戶輸入的驗(yàn)證碼是否正確、

2、前端功能:顯示驗(yàn)證碼,提供輸入框供用戶輸入他們看到的驗(yàn)證碼,把用戶輸入的數(shù)據(jù)交給后端,接收后端返回的驗(yàn)證結(jié)果

前后端交互接口

后端需要完成的兩個(gè)工作:1、生成驗(yàn)證碼,2、校驗(yàn)驗(yàn)證碼的正確性

接口定義:

1、生成驗(yàn)證碼

請(qǐng)求url:/captcha/getCaptcha

響應(yīng):返回驗(yàn)證碼的圖片

2、校驗(yàn)驗(yàn)證碼的正確性

請(qǐng)求url:/captcha/check

請(qǐng)求參數(shù):用戶輸入的驗(yàn)證碼captcha

響應(yīng):驗(yàn)證碼正確返回true,錯(cuò)誤返回false

前端代碼

index.html

展示效果:

用戶點(diǎn)擊圖片之后,可以更新驗(yàn)證碼圖片

在這里插入圖片描述

代碼如下:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <title>驗(yàn)證碼</title>
        <style>
            body {
                font-family: 'Arial', sans-serif;
                background-color: #f7f7f7;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
            #container {
                text-align: center;
                background: white;
                padding: 50px;
                border-radius: 8px;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            }
            h1 {
                color: #333;
                font-size: 2em;
                margin-bottom: 20px;
            }
            #inputCaptcha {
                height: 40px;
                margin-right: 10px;
                vertical-align: middle;
                border: 1px solid #ddd;
                border-radius: 4px;
                padding: 0 10px;
            }
            #verificationCodeImg {
                vertical-align: middle;
                border: 1px solid #ddd;
                cursor: pointer;
                transition: transform 0.2s;
            }
            #verificationCodeImg:hover {
                transform: scale(1.05);
            }
            #checkCaptcha {
                height: 40px;
                width: 120px;
                background-color: #5cb85c;
                color: white;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                transition: background-color 0.2s;
            }
            #checkCaptcha:hover {
                background-color: #4cae4c;
            }
        </style>
    </head>

    <body>
        <div id="container">
            <h1>輸入驗(yàn)證碼</h1>
            <div id="confirm">
                <input type="text" name="inputCaptcha" id="inputCaptcha">
                <img id="verificationCodeImg" src="http://127.0.0.1:8080/captcha/getCaptcha" style="cursor: pointer;"
                     title="看不清?換一張"/>
                <input type="button" value="提交" id="checkCaptcha">
            </div>
        </div>
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
        <script>
            $("#verificationCodeImg").click(function () {
                $(this).hide().attr('src', 'http://127.0.0.1:8080/captcha/getCaptcha?dt=' + new Date().getTime()).fadeIn();
            });

            $("#checkCaptcha").click(function () {
                $.ajax({
                    type: "post",
                    url: "captcha/check",
                    data: {
                        captcha: $("#inputCaptcha").val()
                    },
                    success: function (result) {
                        if (result) {
                            location.href = "success.html"
                        } else {
                            alert("驗(yàn)證碼錯(cuò)誤")
                        }
                    }
                });
            });
        </script>
    </body>

</html>

success.html,當(dāng)用戶驗(yàn)證碼輸入正確時(shí)展示的內(nèi)容

展示效果:

在這里插入圖片描述

代碼如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>驗(yàn)證成功頁(yè)</title>
        <style>
            body {
                font-family: 'Arial', sans-serif;
                background-color: #f7f7f7;
                margin: 0;
                padding: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
            }
            .container {
                text-align: center;
                background: white;
                padding: 50px;
                border-radius: 8px;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            }
            h1 {
                color: green;
                font-size: 2.5em;
                margin: 0;
            }
            p {
                color: blue;
                font-size: 1.2em;
                margin-top: 20px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>驗(yàn)證成功</h1>
        </div>
    </body>
</html>

后端代碼

結(jié)構(gòu)如下:

在這里插入圖片描述

在pom.xml中添加依賴:

在這里插入圖片描述

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-captcha</artifactId>
    <version>5.8.26</version>
</dependency>

CaptchaController類:

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    //注入
    @Autowired
    private CaptchaProperties captchaProperties;

    @Value("${captcha.session.key}")
    private String key;
    @Value("${captcha.session.datetime}")
    private String datetime;


    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        //定義圖形驗(yàn)證碼的長(zhǎng)和寬
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight());

        String code = lineCaptcha.getCode();

        //設(shè)置session,把驗(yàn)證碼信息存儲(chǔ)到session中
        session.setAttribute(key, code);
        session.setAttribute(datetime, new Date());


        //驗(yàn)證碼寫入瀏覽器
        lineCaptcha.write(response.getOutputStream());

        //設(shè)置ContentType
        response.setContentType("image/jpeg");
        //設(shè)置編碼
        response.setCharacterEncoding("utf8");
        //防止瀏覽器緩存驗(yàn)證碼圖片
        response.setHeader("Pragma", "No-cache");

    }

    @RequestMapping("/check")
    public boolean check(String captcha, HttpSession session) {
        String code = (String) session.getAttribute(key);


        if (!StringUtils.hasLength(captcha)) {
            return false;
        }

        //從session中獲取時(shí)間
        Date date = (Date) session.getAttribute(datetime);

        if (date == null || System.currentTimeMillis() - date.getTime() > 60 * 1000) {
            //session為null,或者session過(guò)期(超過(guò)一分鐘就算過(guò)期)
            //System.currentTimeMillis() - date.getTime(): 當(dāng)前時(shí)間-請(qǐng)求時(shí)間
            return false;
        }

        //不區(qū)分大小寫
        return captcha.equalsIgnoreCase(code);
    }
}

CaptchaProperties類:

@ConfigurationProperties(prefix = "captcha")
@Configuration
@Data
public class CaptchaProperties {
    private Integer height;
    private Integer width;

}

MySession類:

@Data
public class MySession {
    private String key;
    private String datetime;
}

配置文件 application.yml:

captcha:
  height: 50
  width: 150
  session:
    key: CaptchaCode
    datetime: CaptchaDate

以上就是SpringBoot實(shí)現(xiàn)驗(yàn)證碼的案例分享的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot驗(yàn)證碼案例的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 初次體驗(yàn)MyBatis的注意事項(xiàng)

    初次體驗(yàn)MyBatis的注意事項(xiàng)

    今天給大家?guī)?lái)的是關(guān)于MyBatis的相關(guān)知識(shí),文章圍繞著MyBatis的用法展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Myeclipse 2016下Aptana安裝教程

    Myeclipse 2016下Aptana安裝教程

    這篇文章主要為大家詳細(xì)介紹了Myeclipse 2016下Aptana安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Quarkus中實(shí)現(xiàn)Resteasy的文件上傳下載操作

    Quarkus中實(shí)現(xiàn)Resteasy的文件上傳下載操作

    這篇文章主要為大家介紹了Quarkus中實(shí)現(xiàn)Resteasy的文件上傳下載的操作過(guò)程步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程

    Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程

    這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • SpringBoot攔截器讀取流后不能再讀取的問(wèn)題

    SpringBoot攔截器讀取流后不能再讀取的問(wèn)題

    這篇文章主要介紹了SpringBoot攔截器讀取流后不能再讀取的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java數(shù)組和可變參數(shù)使用舉例詳解

    Java數(shù)組和可變參數(shù)使用舉例詳解

    Java中的可變參數(shù)和數(shù)組參數(shù)在使用上很相似,但它們?cè)诜椒ǘx、調(diào)用方式以及編譯處理上有明顯區(qū)別,理解這些差異有助于寫出更清晰、安全的代碼,這篇文章主要介紹了Java數(shù)組和可變參數(shù)的相關(guān)資料,需要的朋友可以參考下
    2026-05-05
  • 詳解java動(dòng)態(tài)代理的2種實(shí)現(xiàn)方式

    詳解java動(dòng)態(tài)代理的2種實(shí)現(xiàn)方式

    目前Java開(kāi)發(fā)包中包含了對(duì)動(dòng)態(tài)代理的支持,但是其實(shí)現(xiàn)只支持對(duì)接口的的實(shí)現(xiàn)。這篇文章主要介紹了詳解java動(dòng)態(tài)代理的2種實(shí)現(xiàn)方式 ,有興趣的可以了解一下。
    2016-11-11
  • 關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)

    關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本)

    這篇文章主要介紹了關(guān)于服務(wù)網(wǎng)關(guān)Spring Cloud Zuul(Finchley版本),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 解決	Spring RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)問(wèn)題

    解決 Spring RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)問(wèn)題

    本文詳解說(shuō)明了RestTemplate post傳遞參數(shù)時(shí)報(bào)錯(cuò)的問(wèn)題及其原由,需要的朋友可以參考下
    2020-02-02
  • Java中StopWatch工具類的用法詳解

    Java中StopWatch工具類的用法詳解

    stopWatch 是org.springframework.util 包下的一個(gè)工具類,使用它可直觀的輸出代碼執(zhí)行耗時(shí),以及執(zhí)行時(shí)間百分比,下面就跟隨小編一起來(lái)看看它的具體用法吧
    2024-12-12

最新評(píng)論

石家庄市| 社旗县| 怀仁县| 株洲县| 得荣县| 徐水县| 安顺市| 金湖县| 仁化县| 呼图壁县| 洪江市| 沛县| 巫溪县| 时尚| 保德县| 大丰市| 望城县| 沐川县| 荃湾区| 福建省| 浠水县| 阜康市| 宜君县| 库伦旗| 精河县| 金湖县| 故城县| 鲁山县| 芒康县| 石林| 鲁甸县| 新郑市| 平邑县| 寿阳县| 孟州市| 福安市| 金川县| 武汉市| 寿阳县| 盐池县| 乐都县|