欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring MVC整合Kaptcha的具體使用

 更新時(shí)間:2022年06月30日 09:35:44   作者:擼帝  
Kaptcha 是一個(gè)可高度配置的實(shí)用驗(yàn)證碼生成工具,本文主要介紹了Spring MVC整合Kaptcha的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

驗(yàn)證碼的作用

防止惡意破解密碼、刷票、論壇灌水、刷頁。

有效防止某個(gè)黑客對(duì)某一個(gè)特定注冊(cè)用戶用特定程序暴力破解方式進(jìn)行不斷的登錄嘗試,實(shí)際上使用驗(yàn)證碼是現(xiàn)在很多網(wǎng)站通行的方式(比如招商銀行的網(wǎng)上個(gè)人銀行,百度社區(qū)),我們利用比較簡(jiǎn)易的方式實(shí)現(xiàn)了這個(gè)功能。雖然登錄麻煩一點(diǎn),但是對(duì)網(wǎng)友的密碼安全來說這個(gè)功能還是很有必要,也很重要。但我們還是 提醒大家要保護(hù)好自己的密碼 ,盡量使用混雜了數(shù)字、字母、符號(hào)在內(nèi)的 6 位以上密碼,不要使用諸如 1234 之類的簡(jiǎn)單密碼或者與用戶名相同、類似的密碼 ,免得你的賬號(hào)被人盜用給自己帶來不必要的麻煩。

驗(yàn)證碼通常使用一些線條和一些不規(guī)則的字符組成,主要作用是為了防止一些黑客把密碼數(shù)據(jù)化盜取。

Kaptcha 簡(jiǎn)介

Kaptcha 是一個(gè)可高度配置的實(shí)用驗(yàn)證碼生成工具,可自由配置的選項(xiàng)如:

  • 驗(yàn)證碼的字體
  • 驗(yàn)證碼字體的大小
  • 驗(yàn)證碼字體的字體顏色
  • 驗(yàn)證碼內(nèi)容的范圍(數(shù)字,字母,中文漢字!)
  • 驗(yàn)證碼圖片的大小,邊框,邊框粗細(xì),邊框顏色
  • 驗(yàn)證碼的干擾線
  • 驗(yàn)證碼的樣式(魚眼樣式、3D、普通模糊、...)

Kaptcha 詳細(xì)配置表

Constant描述默認(rèn)值
kaptcha.border圖片邊框,合法值:yes , noyes
kaptcha.border.color邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.black
kaptcha.image.width圖片寬200
kaptcha.image.height圖片高50
kaptcha.producer.impl圖片實(shí)現(xiàn)類com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl文本實(shí)現(xiàn)類com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string文本集合,驗(yàn)證碼值從此集合中獲取abcde2345678gfynmnpwx
kaptcha.textproducer.char.length驗(yàn)證碼長(zhǎng)度5
kaptcha.textproducer.font.names字體Arial, Courier
kaptcha.textproducer.font.size字體大小40px.
kaptcha.textproducer.font.color字體顏色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.textproducer.char.space文字間隔2
kaptcha.noise.impl干擾實(shí)現(xiàn)類com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color干擾 顏色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.obscurificator.impl圖片樣式:<br />水紋 com.google.code.kaptcha.impl.WaterRipple <br /> 魚眼 com.google.code.kaptcha.impl.FishEyeGimpy <br /> 陰影 com.google.code.kaptcha.impl.ShadowGimpycom.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl背景實(shí)現(xiàn)類com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from背景顏色漸變,開始顏色light grey
kaptcha.background.clear.to背景顏色漸變, 結(jié)束顏色white
kaptcha.word.impl文字渲染器com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE

Spring MVC 整合 Kaptcha

POM

pom.xml 配置文件如下:

<dependency>
    <groupId>com.google.code.kaptcha</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3</version>
</dependency>

主要增加了 com.google.code.kaptcha:kaptcha 依賴

創(chuàng)建 Spring 配置

創(chuàng)建一個(gè)名為 spring-context-kaptcha.xml Spring 配置文件,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <constructor-arg>
                    <props>
                        <prop key="kaptcha.border">yes</prop>
                        <prop key="kaptcha.border.color">105,179,90</prop>
                        <prop key="kaptcha.textproducer.font.color">blue</prop>
                        <prop key="kaptcha.image.width">125</prop>
                        <prop key="kaptcha.image.height">45</prop>
                        <prop key="kaptcha.textproducer.font.size">45</prop>
                        <prop key="kaptcha.session.key">code</prop>
                        <prop key="kaptcha.textproducer.char.length">4</prop>
                        <prop key="kaptcha.textproducer.font.names">宋體,楷體,微軟雅黑</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>
</beans>

控制器關(guān)鍵代碼

Controller 層的關(guān)鍵代碼如下,主要作用為將生成的驗(yàn)證碼放入 Session 并輸出到頁面

package com.funtl.my.shop.web.ui.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
public class KaptchaController {

    @Autowired
    private Producer captchaProducer;

    @RequestMapping(value = "verification", method = RequestMethod.GET)
    public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
        // create the text for the image
        String capText = captchaProducer.createText();
        // store the text in the session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
        return null;
    }
}

JSP 關(guān)鍵代碼

JSP 使用 <img /> 標(biāo)簽去請(qǐng)求驗(yàn)證碼圖片

<img id="verification" src="/verification" style="cursor: pointer;" title="看不清?換一張" />

為圖片綁定一個(gè)點(diǎn)擊事件用于無刷新更換驗(yàn)證碼

$(function () {
    // 刷新驗(yàn)證碼
    $("#verification").bind("click", function () {
        $(this).hide().attr('src', '/verification?random=' + Math.random()).fadeIn();
    });
});

 到此這篇關(guān)于Spring MVC整合Kaptcha的具體使用的文章就介紹到這了,更多相關(guān)Spring MVC整合Kaptcha內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論