Home  >  Article  >  Java  >  Introduction to usage examples of verification code combined with springboot when logging in

Introduction to usage examples of verification code combined with springboot when logging in

零下一度
零下一度Original
2017-06-17 11:48:061765browse

In a web applicationVerification code is a common element. Whether it is preventing robots or crawlers, it has a certain effect. The following article mainly introduces the relevant information about the login verification code kaptcha combined with spring boot usage. Friends in need can refer to it. Let’s take a look together.

Preface

When our user logs in, for security reasons, the verification code function will be added. Google's kaptcha is used here; spirngboot is lightweight and independent, making spring-based application development particularly simple. There are many introductions to springboot on the Internet, so I won’t go into details here.

Let’s get back to the point. Let’s talk about the usage of verification code combined with springboot when logging in.

The jar package needed to introduce kaptcha is what I use here. What is maven


  <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>

is to remove the servlet package that comes with the package. In my personal understanding, springboot is a lightweight micro-architecture built with javaconfig and annotations.

The following is the javaconfig of kapcha


@Configuration 
public class CaptchaConfig { 
  
 
 @Bean(name="captchaProducer") 
 public DefaultKaptcha getKaptchaBean(){ 
  DefaultKaptcha defaultKaptcha=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", "blue"); 
  properties.setProperty("kaptcha.image.width", "125"); 
  properties.setProperty("kaptcha.image.height", "45"); 
  properties.setProperty("kaptcha.session.key", "code"); 
  properties.setProperty("kaptcha.textproducer.char.length", "4"); 
  properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");   
  Config config=new Config(properties); 
  defaultKaptcha.setConfig(config); 
  return defaultKaptcha; 
 } 
}

The javaconfig of katcha here is equivalent to the bean configuration in springmvc. The following is a solution for the above Bean example of javaconfig's springmvc, for reference


<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>

Among them, the attribute parameters in the construction method can be set according to your own needs.

Configuration file has been configured, so how to get your own QR code? My understanding is the concept of canvas, and then generate the corresponding canvas from the generated four-digit verification code. Then let the result be written 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, use the verification code and cooike when the user logs in captchacode to achieve uniqueness verification. At first, I considered putting it in the session. After thinking about it at the time, I felt that it was unscientific. For example, if I put the captchacode in the session, there would be one verification code, and then another user would Log in, the previous user is still logging in, and a series of problems will occur at this time. Cookies and redis are used here to handle concurrent login verification of users.

The page is relatively simple to use as follows:


<p style="float: left;"> 
  <i><img style="height:22px;" id="codeImg" alt="点击更换" title="点击更换" src="code/captcha-image" /></i> 
</p>

If you want to change, add a click event, and then clear the previous redis The corresponding cached data; or when obtaining the verification code, set the life cycle.

The above is the detailed content of Introduction to usage examples of verification code combined with springboot when logging in. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn