验证码
1.作用
- 防止暴力攻击,前端安全保障
2.第一步
导入jar包
<!-- 验证码认证的依赖-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
3.第二步
声明验证码组件
拿来吧你(在web.xml中配置)
<!--验证码 -->
<servlet>
<servlet-name>cap</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</param-value>
</init-param>
<init-param>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>211,229,237</param-value>
</init-param>
<init-param>
<!-- session.setAttribute("captcha","验证码") -->
<param-name>kaptcha.session.key</param-name>
<param-value>captcha</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cap</servlet-name>
<url-pattern>/captcha</url-pattern>
</servlet-mapping>
4.第三步
Page页面
图片
<img src="${pageContext.request.contextPath}/captcha" style="width:85px" id="cap"/>
点击刷新
<script>
$("#cap").click(function(){
//刷新验证码
path = $(this).attr("src")+"?"+new Date().getTime();
$(this).attr("src",path);
});
</script>
5.不用xml配置文件,配置类写法
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha defaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//1.边框
properties.setProperty("kaptcha.border","no");
//2.生成验证码的位数
properties.setProperty("kaptcha.textproducer.char.length","4");
//3.验证码的范围
properties.setProperty("kaptcha.textproducer.char.string","abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
//4.验证码的颜色
properties.setProperty("kaptcha.background.clear.to","211,229,237");
Config config=new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
controller层
@Controller
public class KaptchaController {
@Autowired
DefaultKaptcha defaultKaptcha;
@RequestMapping("/getCode")
public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
String text = defaultKaptcha.createText();
//将生成的验证码放置到session中
request.getSession().setAttribute("kaptcha",text);
BufferedImage image = defaultKaptcha.createImage(text);
try {
ImageIO.write(image, "jpg", jpegOutputStream);
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}