您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        使用Kaptcha 生成验证码十分简单并且参数可以进行自定义,以下简单记录下使用步骤。
1.在pom.xml中添加maven依赖:
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency>
2.web.xml中配置kaptcha属性:
<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>
其中bean节点的id值 verifyCodeProducer 为在类中引用@Resource生成实例时的名称;属性配置中 kaptcha.session.key 的值为在session中存取名称。
在servlet节点中配置
3.controller类中的相关方法:
@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为属性配置中 kaptcha.session.key 的值。
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 ></div>
  <img src="${pageContext.request.contextPath}/getVerifyCodeImage" id="verifyCodeImage"  /> 
  <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属性说明:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。