Home  >  Article  >  Java  >  Spring code example generated through kaptcha configuration verification code

Spring code example generated through kaptcha configuration verification code

Y2J
Y2JOriginal
2017-05-05 15:19:181397browse

This article mainly introduces spring mvc using kaptcha to generate verification code examples, and introduces in detail the steps of using Kaptcha to generate verification codes. Those who are interested can learn more

Using Kaptcha to generate verification codes is very simple and The parameters can be customized, and the usage steps are simply recorded below.

1. Add maven dependency in pom.xml:

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

2. Configure kaptcha attribute in web.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 the bean node verifyCodeProducer is the name when referenced in the class@Resource; kaptcha.session.key in the attribute configuration The value is the access name in the session.

Configure in the servlet node

##3. Related methods in the 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

<p 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=&#39;login.label.code&#39; />" />
  <p style="height: 1px"></p>
  <img src="${pageContext.request.contextPath}/getVerifyCodeImage" id="verifyCodeImage" style="margin-bottom: -3px" /> 
  <a href="#" rel="external nofollow" onclick="changeVerifyCode()"><spring:message code=&#39;login.code.tip&#39; /></a>
</p>
function changeVerifyCode() {
  $(&#39;#verifyCodeImage&#39;).hide().attr(&#39;src&#39;, &#39;${pageContext.request.contextPath}/getVerifyCodeImage?&#39; + Math.floor(Math.random()*100) ).fadeIn(); 
  event.cancelBubble=true; 
}

5.kaptcha attribute description:

  1. kaptcha.border.color Border color Default is Color.BLACK

  2. kaptcha.border.thickness Border thickness Default is 1

  3. kaptcha.producer.impl Verification code

    Generator The default is DefaultKaptcha

  4. ##kaptcha.textproducer.impl Verification code text generator The default is DefaultTextCreator
  5. kaptcha.textproducer.char.string Verification code text character content range Default is abcde2345678gfynmnpwx
  6. kaptcha.textproducer.char.length Verification code text character length Default is abcde2345678gfynmnpwx 5
  7. ##kaptcha.textproducer.font.names Verification code text font style Default is new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
  8. kaptcha.textproducer.font.size Verification code text character size The default is 40
  9. kaptcha.textproducer.font.color Verification code text character Color Default is Color.BLACK
  10. kaptcha.textproducer.char.space Verification code text character spacing Default is 2
  11. ##kaptcha.noise.impl Verification code noise generation

    Object
  12. Default is DefaultNoise
  13. kaptcha.noise.color Verification code noise color Default is Color.BLACK

  14. kaptcha.obscurificator.impl Verification code style engine Default is WaterRipple

  15. kaptcha.word.impl Verification code text character rendering Default is DefaultWordRenderer

  16. kaptcha.background.impl Verification code background generator Default is DefaultBackground

  17. kaptcha.background.clear.from Verification code background color gradient Default is Color.LIGHT_GRAY

  18. kaptcha.background.clear.to Verification code background color gradient Default is Color.WHITE

  19. ##kaptcha.image.width Verification code

    Image

    Width Default It is 200
  20. kaptcha.image.height Verification code image height The default is 50

  21. [Related recommendations]

    1.
  22. Java Free Video Tutorial

2.

YMP Online Manual

##3.

Java Video Tutorial on Implementing Equal-proportional Thumbnails for Images

The above is the detailed content of Spring code example generated through kaptcha configuration verification code. 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