防惡意攻擊之驗證碼生成Kaptcha

Kaptcha 簡介

Kaptcha 是一個可高度配置的實用驗證碼生成工具,可自由配置的選項如:

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

主要代碼

KaptchaConfig.java

<code> 

public

class

KaptchaConfig

{

public

DefaultKaptcha

getDDefaultKaptcha

()

{ DefaultKaptcha dk =

new

DefaultKaptcha(); Properties properties =

new

Properties(); properties.setProperty(

"kaptcha.border"

,

"yes"

); properties.setProperty(

"kaptcha.border.color"

,

"105,179,90"

); properties.setProperty(

"kaptcha.textproducer.font.color"

,

"red"

); properties.setProperty(

"kaptcha.image.width"

,

"110"

); properties.setProperty(

"kaptcha.image.height"

,

"40"

); properties.setProperty(

"kaptcha.textproducer.font.size"

,

"30"

); properties.setProperty(

"kaptcha.session.key"

,

"code"

); properties.setProperty(

"kaptcha.textproducer.char.length"

,

"4"

); properties.setProperty(

"kaptcha.textproducer.font.names"

,

"宋體,楷體,微軟雅黑"

); Config config =

new

Config(properties); dk.setConfig(config);

return

dk; } }/<code>

控制器關鍵代碼

<code> 

public

class

KaptchaController

{ DefaultKaptcha defaultKaptcha;

public

void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception { byte[] captcha =

null

; ByteArrayOutputStream

out

= new ByteArrayOutputStream();

try

{ String createText = defaultKaptcha.createText(); request.getSession().setAttribute(

"rightCode"

, createText); BufferedImage bi = defaultKaptcha.createImage(createText); ImageIO.write(bi,

"jpg"

,

out

); }

catch

(Exception e) { response.sendError(HttpServletResponse.SC_NOT_FOUND);

return

; } captcha =

out

.toByteArray(); response.setHeader(

"Cache-Control"

,

"no-store"

); response.setHeader(

"Pragma"

,

"no-cache"

); response.setDateHeader(

"Expires"

,

0

); response.setContentType(

"image/jpeg"

); ServletOutputStream sout = response.getOutputStream(); sout.write(captcha); sout.flush(); sout.close(); }

public

ModelAndView imgvrifyControllerDefaultKaptcha(HttpServletRequest request, HttpServletResponse response) { ModelAndView model = new ModelAndView(); String rightCode = (String) request.getSession().getAttribute(

"rightCode"

); String tryCode = request.getParameter(

"tryCode"

); System.

out

.println(

"rightCode:"

+ rightCode +

" ———— tryCode:"

+ tryCode);

if

(!rightCode.equals(tryCode)) { model.addObject(

"info"

,

"驗證碼錯誤,請再輸一次!"

); model.setViewName(

"login"

); }

else

{ model.addObject(

"info"

,

"登陸成功"

); model.setViewName(

"index"

); }

return

model; }

public

ModelAndView index() {

return

new ModelAndView(

"login"

); } }/<code>

前端頁面

login.html

<code> >

<

html

xmlns:th

=

"http://www.thymeleaf.org"

>

<

head

lang

=

"en"

>

<

meta

charset

=

"UTF-8"

>

<

title

>Insert title here

title

>

<

link

rel

=

"stylesheet"

href

=

"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"

>

<

script

src

=

"https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"

>

script

>

<

script

src

=

"https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"

>

script

>

<

style

type

=

"text/css"

>

body

{

padding

:

10px

; }

#inputtext

{

width

:

100%

; }

#login

{

width

:

300px

;

margin

:

0px

auto;

padding-top

:

60px

; }

#flushimg

{

text-decoration

: underline; }

#butt

{

width

:

60%

; }

style

>

head

>

<

body

>

<

div

id

=

"login"

>

<

form

action

=

"/login"

method

=

"post"

>

<

h2

align

=

"center"

>L O G I N

h2

>

<

br

/>

<

br

/>

<

input

type

=

"text"

name

=

"userName"

class

=

"form-control"

id

=

"inputtext"

required

autofocus

placeholder

=

"-----請輸入用戶名-----"

/>

<

br

/>

<

input

type

=

"password"

name

=

"userName"

class

=

"form-control"

id

=

"inputtext"

required

placeholder

=

"----請輸入用戶密碼----"

/>

<

br

/>

<

div

id

=

"flushimg"

>

<

img

alt

=

"驗證碼"

onclick

=

"this.src='/defaultKaptcha?d=' + new Date()*1"

src

=

"/defaultKaptcha"

/>

<

a

>看不清?點擊圖片刷新一下

a

>

div

>

<

input

type

=

"text"

name

=

"tryCode"

class

=

"form-control"

required

placeholder

=

"-----請輸入驗證碼-----"

/>

<

h4

th:text

=

"${info}"

style

=

"color: red"

>

h4

>

<

input

type

=

"checkbox"

name

=

"rememberMe"

/>記住我

<

br

/>

<

div

style

=

"width: 100%;text-align: center;"

>

<

input

type

=

"submit"

value

=

"登 錄"

id

=

"butt"

class

=

"btn btn-success"

/>

div

>

form

>

div

>

body

>

html

>/<code>


分享到:


相關文章: