Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to save the login verification code to redis by Java

Shulou Source: shulou.com Published: 2022-06-01 20:38:56 09月30日 Update

This article mainly introduces "Java how to save login CAPTCHA to redis". In daily operation, I believe that many people have doubts about how to save login CAPTCHA to redis in Java. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to save login CAPTCHA to redis". Next, please follow the editor to study!

Introduce redis dependency

Org.springframework.boot

Spring-boot-starter-data-redis

CAPTCHA generation class

Package com.yishang.yspay.util

Import org.springframework.beans.factory.annotation.Autowired

Import org.springframework.data.redis.core.RedisTemplate

Import java.awt.*

Import java.util.Random

/ * *

* CAPTCHA generation class

, /

Public class RandomValidateCode {

Public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY"; / / key in session

/ / private String randString = "0123456789"; / / randomly generate the string private String with only numbers

/ / private String randString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; / / randomly generate a string with only letters

Private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; / / randomly generates a string of numbers and letters

Private int width = 95 position / picture width

Private int height = 25umbnail / picture high

Private int lineSize = 10 / number of interference lines

Private int stringNum = 4 characters / number of characters randomly generated

/ * *

* redis tool

, /

@ Autowired

Private RedisTemplate redisTemplate

Private Random random = new Random ()

/ *

* get the font

, /

Private Font getFont () {

Return new Font ("Fixedsys", Font.CENTER_BASELINE, 24)

}

/ *

* get the color

, /

Public Color getRandColor (int fc, int bc) {

If (fc > 255)

Fc = 255,

If (bc > 255)

Bc = 255,

Int r = fc + random.nextInt (bc-fc-16)

Int g = fc + random.nextInt (bc-fc-14)

Int b = fc + random.nextInt (bc-fc-18)

Return new Color (r, g, b)

}

/ *

* draw string

, /

Public String drowString (Graphics g, String randomString, int I) {

G.setFont (getFont ())

G.setColor (new Color (random.nextInt, random.nextInt), random

.nextInt (121)

String rand = String.valueOf (getRandomString (random.nextInt (randString)

.length ()

RandomString + = rand

G.translate (random.nextInt (3), random.nextInt (3))

G.drawString (rand, 13 * I, 16)

Return randomString

}

/ *

* draw interference lines

, /

Public void drowLine (Graphics g) {

Int x = random.nextInt (width)

Int y = random.nextInt (height)

Int xl = random.nextInt (13)

Int yl = random.nextInt (15)

G.drawLine (x, y, x + xl, y + yl)

}

/ *

* get random characters

, /

Public String getRandomString (int num) {

Return String.valueOf (randString.charAt (num))

}

}

Interface

@ RequestMapping (value = "/ getVerify")

Public void getVerify (HttpServletRequest request, HttpServletResponse response) {

Response.setContentType ("image/jpeg"); / / set the corresponding type and tell the browser that the output is an image.

Response.setHeader ("Pragma", "No-cache"); / / set the response header information, telling the browser not to cache this content

Response.setHeader ("Cache-Control", "no-cache")

Response.setDateHeader ("Expire", 0)

RandomValidateCode randomValidateCode = new RandomValidateCode ()

/ / the BufferedImage class is an Image class with a buffer, and the Image class is a class used to describe image information.

BufferedImage image = new BufferedImage (95,25, BufferedImage.TYPE_INT_BGR)

Graphics g = image.getGraphics (); / / generate the Graphics object of the Image object, which can perform various drawing operations on the image

G.fillRect (0,0,95,25)

G.setFont (new Font ("Times New Roman", Font.ROMAN_BASELINE, 18))

G.setColor (randomValidateCode.getRandColor 110,133)

/ / draw interference lines

For (int I = 0; I

Tags: Characters authentication strings login pictures learning objects interference information content only images letters numbers quantity more browser help browse generate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Shulou Technology MySQL Docker