When our users log in, for security reasons, it will increase the function of the verification code, the use of Google's kaptcha;spirngboot is light, independent, making spring-based application development is particularly simple. There are a lot of introduction springboot on the Internet, there is not much to say. The word to be caught, the use of the verification code combined with Springboot when landing
To introduce the jar packages needed for Kaptcha, I'm using maven here.
<dependency><groupid>com.github.penggle</groupid><artifactid>kaptcha</artifactid ><version>2.3.2</version><exclusions><exclusion><artifactid>javax.servlet-api </artifactid><groupid>javax.servlet</groupid></exclusion></exclusions></ Dependency>
Remove the servlet package that comes with the package. In my personal understanding, Springboot is the lightweight micro-architecture that Javaconfig and annotations build up.
Here's Kapcha's javaconfig.
@Configurationpublic class Captchaconfig {@Bean (name= "Captchaproducer") public Defaultkaptcha Getkaptchabean () { Defaultkaptcha defaultkaptcha=new Defaultkaptcha (); Properties Properties=new Properties ();p roperties.setproperty ("Kaptcha.border", "yes");p Roperties.setproperty (" Kaptcha.border.color "," 105,179,90 ");p roperties.setproperty (" Kaptcha.textproducer.font.color "," Blue "); Properties.setproperty ("Kaptcha.image.width", "a");p Roperties.setproperty ("Kaptcha.image.height", "45"); Properties.setproperty ("Kaptcha.session.key", "code");p roperties.setproperty ("Kaptcha.textproducer.char.length") , "4");p Roperties.setproperty ("Kaptcha.textproducer.font.names", "song body, italics, Microsoft ya Black"); Config config=new config (properties);d efaultkaptcha.setconfig (config); return defaultkaptcha;}}
The javaconfig of Katcha here is equivalent to the bean configuration in Springmvc, and below is an example of a bean for SPRINGMVC above javaconfig for reference
<bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" > <property name= "config" & Gt <bean class= "Com.google.code.kaptcha.util.Config" > <constructor-arg> < ;p rops> <prop key= "kaptcha.border" >yes</prop> <prop ke Y= "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.f Ont.size ">45</prop> <prop key=" Kaptcha.session.key ">code</prop> <prop key= "Kaptcha.textproducer.char.length" >4</prop> <prop key= "kapt Cha.texTproducer.font.names "> Arial, Italic, Microsoft Black </prop> </props> </constructor-arg > </bean> </property> </bean>
The attribute parameters in the constructed method can be set according to their own requirements.
The configuration file is already well-matched, so how to get your own QR code, my understanding is the concept of the canvas, and then the generated four-bit verification code to generate the corresponding canvas, and then let the results write out. The code is as follows:
@RequestMapping (value = "/captcha-image") public Modelandview getkaptchaimage (HttpServletRequest request, HttpServletResponse response) throws Exception {Response.setdateheader ("Expires", 0); Response.setheader (" Cache-control "," No-store, No-cache, must-revalidate "); Response.AddHeader (" Cache-control "," Post-check=0, Pre-Check =0 "), Response.setheader (" Pragma "," No-cache "), Response.setcontenttype (" Image/jpeg "); String Captext = Captchaproducer.createtext (); System.out.println ("Captext:" + captext); try {String uuid=uuidutils.getuuid32 (). Trim (). toString (); Redistemplate.opsforvalue (). Set (UUID, captext,60*5,timeunit.seconds); Cookie cookie = new Cookie ("Captchacode", uuid); Response.addcookie (cookie);} catch (Exception e) {e.printstacktrace ();} BufferedImage bi = captchaproducer.createimage (captext); Servletoutputstream out = Response.getoutputstream (), Imageio.write (bi, "JPG", out), try {Out.flush ();} finally { Out.close ();} return null;}
As the above code, in the user login to use the verification code and cooike in the Captchacode to achieve the uniqueness of the verification, at the beginning I consider to put into the session, at that time, think of it, it is not scientific ah, such as speaking Captchacode put into the session, This time the verification code is one, then another user login, the previous user is still in the landing, this time there will be a series of problems. Cookies and Redis are used here to deal with the user's concurrent login verification.
Page use is also relatively simple as follows:
<div style= "Float:left;" ><i></i ></div>
Replace it with a click event and then empty the previously cached data in Redis, or set the lifetime when the verification code is obtained.
Approximate method, such as above, the wrong place, please correct me.
Springboot usage of verification code Kaptcha