spring mvc USES the kaptcha configuration to generate captcha instances

  • 2020-06-23 00:31:42
  • OfStack

Using Kaptcha to generate captcha 10 minutes is simple and the parameters can be customized. The following steps are simply recorded.

1. Add maven dependencies to ES4en.xml:


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

2. Configure kaptcha attribute in ES11en. xml:


<bean id="verifyCodeProducer" 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.border.thickness">1</prop>

            <prop key="kaptcha.noise.color">blue</prop>

            <prop key="kaptcha.image.width">150</prop>
            <prop key="kaptcha.image.height">50</prop>

            <prop key="kaptcha.session.key">verifyCode</prop>

            <!-- <prop key="kaptcha.textproducer.char.string">0123456789abcdefghijklmnopqrst!@#$%^*</prop> -->
            <prop key="kaptcha.textproducer.char.length">4</prop>
            <prop key="kaptcha.textproducer.char.space">4</prop>


            <prop key="kaptcha.textproducer.font.size">30</prop>
            <prop key="kaptcha.textproducer.font.color">blue</prop>

          </props>
        </constructor-arg>
      </bean>
    </property>
  </bean>

The id value of bean node verifyCodeProducer is the name of the generated instance when @Resource is referenced in the class; The value of kaptcha. session. key in the property configuration is the access name in session.

Configure in the servlet node

3. Related methods in controller class:


@Controller
public class CommonController {

  @Autowired
  private Producer verifyCodeProducer;

  @RequestMapping(path = "/getVerifyCodeImage", method = RequestMethod.GET)
  public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();

    ResponseUtils.noCache(response);
    response.setContentType("image/jpeg");

    String capText = verifyCodeProducer.createText();
    session.setAttribute(Constants.SESSION_KEY_VERIFY_CODE, capText);

    BufferedImage bi = verifyCodeProducer.createImage(capText);
    ServletOutputStream out = null;
    try {
      out = response.getOutputStream();
      ImageIO.write(bi, "jpg", out);
      out.flush();
    } catch (Exception ex) {
      LOGGER.error("Failed to produce the verify code image: ", ex);
      throw new ServerInternalException("Cannot produce the verify code image.");
    } finally {
      IOUtils.closeQuietly(out);
    }
  }
}

Constants.SESSION_KEY_VERIFY_CODE is the value of kaptcha.session.key in the property configuration.

4. jsp:


<div class="form-group has-feedback">
  <span class="glyphicon glyphicon-barcode form-control-feedback"></span> 
  <input id="verifyCode" name="verifyCode" type="text" maxlength="4" class="form-control" placeholder="<spring:message code='login.label.code' />" />
  <div style="height: 1px"></div>
  <img src="${pageContext.request.contextPath}/getVerifyCodeImage" id="verifyCodeImage" style="margin-bottom: -3px" /> 
  <a href="#" rel="external nofollow" onclick="changeVerifyCode()"><spring:message code='login.code.tip' /></a>
</div>

function changeVerifyCode() {
  $('#verifyCodeImage').hide().attr('src', '${pageContext.request.contextPath}/getVerifyCodeImage?' + Math.floor(Math.random()*100) ).fadeIn(); 
  event.cancelBubble=true; 
}

5.kaptcha attribute description:

The border color defaults to ES60en.BLACK kaptcha. border. thickness border thickness defaults to 1 kaptcha. producer. impl captcha generator is DefaultKaptcha by default kaptcha. textproducer. impl captcha text generator is DefaultTextCreator by default kaptcha. textproducer. char. string captcha text character content range is abcde2345678gfynmnpwx by default kaptcha. textproducer. char. length captcha text defaults to 5 characters in length kaptcha. textproducer. font. names captcha text font style by default new Font (" Arial ", 1, fontSize), new Font (" Courier ", 1, fontSize) kaptcha. textproducer. font. size captcha text size defaults to 40 characters kaptcha. textproducer. font. color captcha text character color by default Color. BLACK kaptcha. textproducer. char. space captcha text character spacing of 2 by default kaptcha.noise.impl CaptCHA noise generation object is DefaultNoise by default kaptcha.noise.color Captcha noise color default to ES116en. BLACK kaptcha. obscurificator. impl Captcha style engine defaults to WaterRipple kaptcha. word. impl captcha text character rendering is DefaultWordRenderer by default kaptcha. background. impl Captcha background generator is DefaultBackground by default kaptcha. background. clear. from captcha background color gradual defaults to Color. LIGHT_GRAY kaptcha. background. clear. to captcha background color gradual defaults to Color. WHITE kaptcha. image. width Captcha image width defaults to 200 kaptcha. image. height captcha image has a height of 50 by default

Related articles: