import com.wf.captcha.GifCaptcha; import com.wf.captcha.SpecCaptcha; import com.wf.captcha.base.Captcha; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.*; import java.io.IOException; /** * 验证码工具类,重写 {@link com.wf.captcha.utils.CaptchaUtil} * 因为它没有提供修改验证码类型方法 * * @author MrBird */ @Slf4j public class CaptchaUtil { private static IRedisService redisService = SpringContextUtil.getBean(IRedisService.class); // gif 类型验证码 private static final int GIF_TYPE = 1; // png 类型验证码 private static final int PNG_TYPE = 0; // 验证码图片默认高度 private static final int DEFAULT_HEIGHT = 48; // 验证码图片默认宽度 private static final int DEFAULT_WIDTH = 130; // 验证码默认位数 private static final int DEFAULT_LEN = 5; public static void out(HttpServletRequest request, HttpServletResponse response) throws IOException { out(DEFAULT_LEN, request, response); } public static void out(int len, HttpServletRequest request, HttpServletResponse response) throws IOException { out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, null, request, response); } public static void out(int len, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException { out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, null, font, request, response); } public static void out(int width, int height, int len, Integer vType, HttpServletRequest request, HttpServletResponse response) throws IOException { out(width, height, len, vType, null, request, response); } public static void out(int width, int height, int len, Integer vType, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException { outCaptcha(width, height, len, font, GIF_TYPE, vType, request, response); } public static void outPng(HttpServletRequest request, HttpServletResponse response) throws IOException { outPng(DEFAULT_LEN, request, response); } public static void outPng(int len, HttpServletRequest request, HttpServletResponse response) throws IOException { outPng(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, null, request, response); } public static void outPng(int len, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException { outPng(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, null, font, request, response); } public static void outPng(int width, int height, int len, Integer vType, HttpServletRequest request, HttpServletResponse response) throws IOException { outPng(width, height, len, vType, null, request, response); } public static void outPng(int width, int height, int len, Integer vType, Font font, HttpServletRequest request, HttpServletResponse response) throws IOException { outCaptcha(width, height, len, font, PNG_TYPE, vType, request, response); } public static boolean verify(String code, HttpServletRequest request) { HttpSession session = request.getSession(); String key = FebsConstant.CODE_PREFIX + session.getId(); String sessionCode = ""; try { sessionCode = redisService.get(key); } catch (RedisConnectException e) { log.error("获取验证码异常", e); } return StringUtils.equalsIgnoreCase(code, sessionCode); } private static void outCaptcha(int width, int height, int len, Font font, int cType, Integer vType, HttpServletRequest request, HttpServletResponse response) throws IOException { setHeader(response, cType); Captcha captcha = null; if (cType == GIF_TYPE) { captcha = new GifCaptcha(width, height, len); } else { captcha = new SpecCaptcha(width, height, len); } if (font != null) { captcha.setFont(font); } if (vType != null) { captcha.setCharType(vType); } HttpSession session = request.getSession(); String code = captcha.text().toLowerCase(); String key = FebsConstant.CODE_PREFIX + session.getId(); try { redisService.set(key, code, 120000L); } catch (RedisConnectException e) { log.error("保存验证码异常", e); } captcha.out(response.getOutputStream()); } public static void setHeader(HttpServletResponse response, int cType) { if (cType == GIF_TYPE) { response.setContentType(MediaType.IMAGE_GIF_VALUE); } else { response.setContentType(MediaType.IMAGE_PNG_VALUE); } response.setHeader(HttpHeaders.PRAGMA, "No-cache"); response.setHeader(HttpHeaders.CACHE_CONTROL, "No-cache"); response.setDateHeader(HttpHeaders.EXPIRES, 0L); } }
图片验证-工具类CaptchaUtil
原创
©著作权归作者所有:来自51CTO博客作者小飞侠格鲁帅的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:如何将网站制作成自己app
下一篇:redis本地启动
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
生成验证码-超简单
轻松应对验证挑战:简单验证码的实现方法!
验证码 Java -
java图片验证码识别 java 图片验证码java图片验证码识别
-
java 图片验证码识别 java绘制验证码
温馨提示: 代码中的注释值得参考。/** * @author ausu:java绘制验证码。 * */ package indentiy; import java.awt.F
java 图片验证码识别 java 验证码 java验证码 字符串 -
cnn 和 transformer融合
SwinT模块,让Swin-Transformer 的使用变得和CNN一样方便快捷!项目内容一、SwinT模块的使用演示,接口酷似Conv2D由于以下两点原因,我们将Swin-Transformer最核心的部分制成了一个类似于nn.Conv2D的接口并命名为SwinT。其输入、输出数据形状完全和Conv2D(CNN)一样,这极大的方便了使用Transformer来编写模型代码。1、一方面,虽然随着
cnn 和 transformer融合 transformer cnn paddlepaddle paddle