1)创建KaptcheConfig.java类
package com.zyb.hbase.config; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.Properties; @Component public class KaptcheConfig { @Bean public DefaultKaptcha getDefaultKaptcha(){ // 创建DefaultKaptcha对象 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.put("kaptcha.border", "no"); properties.put("kaptcha.textproducer.font.color", "72,118,255"); properties.put("kaptcha.image.width", "160"); properties.put("kaptcha.image.height", "40"); properties.put("kaptcha.textproducer.font.size", "30"); properties.put("kaptcha.session.key", "verifyCode"); properties.put("kaptcha.textproducer.char.space", "5"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }2)创建验证码的配置文件:kaptcha.properties(名字可以随便起)
具体可参考https://blog.csdn.net/weixin_42759709/article/details/84988440
kaptcha属性的配置可以根据自己的需要进行修改,有关具体的配置可以参考 https://blog.csdn.net/du_zilin/article/details/77367978)
A,创建验证码的配置文件:kaptcha.properties(名字可以随便起) ##### Kaptcha Information kaptcha.width=150 kaptcha.height=42 kaptcha.border=no kaptcha.textproducer.font.size=40 kaptcha.textproducer.char.space=10 kaptcha.textproducer.font.names=\u4EFF\u5B8B,\u5FAE\u8F6F\u96C5\u9ED1 kaptcha.textproducer.char.string=1234567890 kaptcha.textproducer.char.length=4 kaptcha.background.clear.from=92,189,170 kaptcha.background.clear.to=255,255,255 B,创建验证码生成工具类NumCodeUtil ```java package com.mmall.common.util; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; @Component("numCodeUtil") public class NumCodeUtil { private static Properties props = new Properties(); @Bean public DefaultKaptcha defaultKaptcha() throws Exception{ // 创建DefaultKaptcha对象 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); // 读取配置文件 try { props.load(NumCodeUtil.class.getClassLoader() .getResourceAsStream("kaptcha.properties")); }catch (Exception e) { e.printStackTrace(); } // 将Properties文件设到DefaultKaptcha对象中 defaultKaptcha.setConfig(new Config(props)); return defaultKaptcha } }在使用@Autowired时,首先在容器中查询对应类型的bean
如果查询结果刚好为一个,就将该bean装配给@Autowired指定的数据
如果查询的结果不止一个,那么@Autowired会根据名称来查找
如果查询的结果为空,那么会抛出异常。解决方法时,使用required=false @GetMapping和@PostMapping
@GetMapping("/kaptcha") //===@RequestMapping(method = RequestMethod.GET name=“kaptcha”)
@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
其实一般项目中是要区分get和post请求的,未避免忘记写method,应该要写getmapping或者postmapping
package com.zyb.hbase.controller; import com.google.code.kaptcha.impl.DefaultKaptcha; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @Controller public class KaptcheController { @Autowired private DefaultKaptcha captchaProducer;//定义DefaultKaptcha对象 @GetMapping("/kaptcha") //@RequestMapping(method = RequestMethod.GET name="kaptcha") //@PostMapping是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。 public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { // 创建字节数组用于存放图片信息 byte[] captchaOutputStream = null; // 创建字节数组用于存放图片信息 ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream(); try { //生产验证码字符串并保存到session中 // 通过DefaultKaptcha获得随机验证码 String verifyCode = captchaProducer.createText(); // 将生成的验证码存放在session中 httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); // 使用生成的验证码字符串,完成图片的生成 BufferedImage challenge = captchaProducer.createImage(verifyCode); // 将图片写入到流中 ImageIO.write(challenge, "jpg", imgOutputStream); } catch (IllegalArgumentException e) { //将图片写入到输入流出现错误 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // 使用HttpServletResponse将图片写入到浏览器中 captchaOutputStream = imgOutputStream.toByteArray(); /// 通过response设定响应请求类型 // // no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。 httpServletResponse.setHeader("Cache-Control", "no-store"); // no-cache指示请求或响应消息不能缓存 httpServletResponse.setHeader("Pragma", "no-cache"); /* expires是response的一个属性,它可以设置页面在浏览器的缓存里保存的时间 ,超过设定的时间后就过期 。 过期后再次浏览该页面就需要重新请求服务器发送页面数据, 如果在规定的时间内再次访问次页面 就不需从服务器传送 直接从缓存中读取。 * */ httpServletResponse.setDateHeader("Expires", 0); // servlet接受request请求后接受图片形式的响应 httpServletResponse.setContentType("image/jpeg"); //通过response获得输出流 ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaOutputStream); responseOutputStream.flush(); responseOutputStream.close(); } }