网站建设 h5,帝国cms 关闭网站,贸易网站源码,做网站花都区若依项目学习之登录生成验证码
使用DefaultKaptcha生成验证码
/*** 验证码配置* * author ruoyi*/
Configuration
public class CaptchaConfig
{/*** 生成字符类型的验证码**/Bean(name captchaProducer)public DefaultKaptcha getKaptchaBean(){DefaultKaptcha…若依项目学习之登录生成验证码
使用DefaultKaptcha生成验证码
/*** 验证码配置* * author ruoyi*/
Configuration
public class CaptchaConfig
{/*** 生成字符类型的验证码**/Bean(name captchaProducer)public DefaultKaptcha getKaptchaBean(){DefaultKaptcha defaultKaptcha new DefaultKaptcha();Properties properties new Properties();// 是否有边框 默认为true 我们可以自己设置yesnoproperties.setProperty(KAPTCHA_BORDER, yes);// 验证码文本字符颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, black);// 验证码图片宽度 默认为200properties.setProperty(KAPTCHA_IMAGE_WIDTH, 160);// 验证码图片高度 默认为50properties.setProperty(KAPTCHA_IMAGE_HEIGHT, 60);// 验证码文本字符大小 默认为40properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, 38);// KAPTCHA_SESSION_KEYproperties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, kaptchaCode);// 验证码文本字符长度 默认为5properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, 4);// 验证码文本字体样式 默认为new Font(Arial, 1, fontSize), new Font(Courier, 1, fontSize)properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, Arial,Courier);// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpyproperties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.ShadowGimpy);Config config new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}/*** 生成数字类型的验证码**/Bean(name captchaProducerMath)public DefaultKaptcha getKaptchaBeanMath(){DefaultKaptcha defaultKaptcha new DefaultKaptcha();Properties properties new Properties();// 是否有边框 默认为true 我们可以自己设置yesnoproperties.setProperty(KAPTCHA_BORDER, yes);// 边框颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_BORDER_COLOR, 105,179,90);// 验证码文本字符颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, blue);// 验证码图片宽度 默认为200properties.setProperty(KAPTCHA_IMAGE_WIDTH, 160);// 验证码图片高度 默认为50properties.setProperty(KAPTCHA_IMAGE_HEIGHT, 60);// 验证码文本字符大小 默认为40properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, 35);// KAPTCHA_SESSION_KEYproperties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, kaptchaCodeMath);// 验证码文本生成器properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, com.ruoyi.framework.config.KaptchaTextCreator);// 验证码文本字符间距 默认为2properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, 3);// 验证码文本字符长度 默认为5properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, 6);// 验证码文本字体样式 默认为new Font(Arial, 1, fontSize), new Font(Courier, 1, fontSize)properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, Arial,Courier);// 验证码噪点颜色 默认为Color.BLACKproperties.setProperty(KAPTCHA_NOISE_COLOR, white);// 干扰实现类properties.setProperty(KAPTCHA_NOISE_IMPL, com.google.code.kaptcha.impl.NoNoise);// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpyproperties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.ShadowGimpy);Config config new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha;}
}Resource(name captchaProducerMath)private Producer captchaProducerMath;RequestMapping(/captcha)ResponseBodypublic void captcha(HttpServletResponse response) {//此代码只针对数字类型的验证码有效如果使用字符类型的验证码需要进行修改ServletOutputStream outputStream null;try {outputStream response.getOutputStream();String text captchaProducerMath.createText();//生成的计算验证码String str text.substring(0, text.lastIndexOf());//生成的验证码的答案需要存储起来比如存在session中String code text.substring(text.lastIndexOf() 1);BufferedImage image captchaProducerMath.createImage(str);ImageIO.write(image, jpg, outputStream);outputStream.flush();} catch (IOException e) {e.printStackTrace();} finally {try {if (outputStream ! null) {outputStream.close();}} catch (IOException e) {e.printStackTrace();}}}上面就是生成验证码的方法。
在若依中和验证码有关的类如下 1、CaptchaConfig注入验证码生成相关的Bean 2、CaptchaValidateFilter过滤器登录后清除验证码的值 3、SysCaptchaController生成验证码的具体逻辑 4、ShiroConfig验证码类型、是否启用验证码配置
参考
《若依项目》前后端未分离版AccessControlFilter