springboot中使⽤验证码kaptcha l引⼊kaptcha所需要的jar包
<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.添加KaptchaConfig类
package com.demomon;
import java.util.Properties;
import t.annotation.Bean;
import org.springframework.stereotype.Component;
le.code.kaptcha.impl.DefaultKaptcha;
le.code.kaptcha.util.Config;
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
Properties properties = new Properties();
// 图⽚边框
properties.setProperty("kaptcha.border", "yes");
// 边框颜⾊
properties.setProperty("lor", "105,179,90");
// 字体颜⾊
properties.setProperty("lor", "red");
// 图⽚宽
properties.setProperty("kaptcha.image.width", "110");
// 图⽚⾼
properties.setProperty("kaptcha.image.height", "40");
// 字体⼤⼩
properties.setProperty("producer.font.size", "30");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("producer.char.length", "4");
// 字体
properties.setProperty("producer.font.names", "宋体,楷体,微软雅⿊");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3.添加获取验证码的COntroller
package ller;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
le.code.kaptcha.Constants;
le.code.kaptcha.Producer;
@Controller
@RequestMapping(value = "/captcha")
public class CaptchaController {
private Producer captchaProducer = null;
@Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
}
/
**
*
* 获取验证码图⽚
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("getCaptchaCode")
public ModelAndView getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = Session();
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 = ateText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// 利⽤⽣成的字符串构建图⽚
BufferedImage bi = ateImage(capText);
ServletOutputStream out = OutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
<form class="registerform" id="form1">
<div class="fm-item">
<label for="logonId" class="form-label">登陆账号:</label>
<input type="text" maxlength="100" id="username" name="username" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label">登陆密码:</label>
<input type="password" maxlength="100" id="password" name="password" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item pos-r">
<label for="logonId" class="form-label">验证码</label>
<input type="text" maxlength="100" id="yzm" name="yzm" class="i-text yzm"/>
<div class="ui-form-explain">
<img id="captchaImg" alt="" th:src="@{/captcha/getCaptchaCode}" class="yzm-img" />
</div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label"></label>
<input type="button" value="" tabindex="4" id="submit" class="btn-login"/>
<div class="ui-form-explain"></div>
</div>
</form>
5.js部分点击验证码获取新的验证码
//获取新验证码
$('#captchaImg').click(function() {spring framework
$(this).attr('src', '/captcha/getCaptchaCode.jpg');
});
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论