Java如何实现滑动验证码
发表于:2025-02-12 作者:千家信息网编辑
千家信息网最后更新 2025年02月12日,小编给大家分享一下Java如何实现滑动验证码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!功能:java实现滑动验证码项目是采用springboot,maven开发工具:采用idea
千家信息网最后更新 2025年02月12日Java如何实现滑动验证码
小编给大家分享一下Java如何实现滑动验证码,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
功能:java实现滑动验证码
项目是采用springboot,maven
开发工具:采用idea
1.效果演示
2.后端代码
控制层
@Controllerpublic class SliderCodeController { @Autowired ResourceLoader resourceLoader; @Autowired private FileUtil fileUtil; // 设置横轴位置缓存 public static Cache< String, Integer > cacheg = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS) .maximumSize(666666).build(); @GetMapping @RequestMapping("index") public String test(HttpServletRequest request, Model model) throws IOException { return "index"; } @GetMapping @RequestMapping("getImg") public @ResponseBody Map< String, Object > getPic(HttpServletRequest request) throws IOException { try { File targetFile = fileUtil.getFile("target"); File tempImgFile = fileUtil.getFile("temp"); Map < String, Object > resultMap = VerifyImageUtil.pictureTemplatesCut(tempImgFile, targetFile); // 生成流水号,这里就使用时间戳代替 String lno = Calendar.getInstance().getTimeInMillis() + ""; cacheg.put(lno, Integer.valueOf(resultMap.get("xWidth") + "")); resultMap.put("capcode", lno); // 移除横坐标送前端 resultMap.remove("xWidth"); return resultMap; } catch (Exception e) { e.printStackTrace(); return null; } } @GetMapping @RequestMapping("checkImgCode") public @ResponseBody Map < String, Object > checkcapcode(@RequestParam("xpos") int xpos, @RequestParam("capcode") String capcode, HttpServletRequest request) throws IOException { Map < String, Object > result = new HashMap< String, Object >(); Integer x = cacheg.getIfPresent(capcode); if (x == null) { // 超时 result.put("code", 3); } else if (xpos - x > 5 || xpos - x < -5) { // 验证失败 result.put("code", 2); } else { // 验证成功 result.put("code", 1); } return result; }}
工具类
@Componentpublic class FileUtil { @Value("${file.path}") private String filePath; @Value("${file.target.path}") private String targetFilePath; @Value("${file.target.num}") private Integer targetfileNum; @Value("${file.temp.path}") private String tempFilePath; @Value("${file.temp.num}") private Integer tempfileNum; public File getFile(String type){ int num = 0; String imgType = ".jpg"; String oldFilePath = ""; if(type.equals("target")){ num = new Random().nextInt(targetfileNum) + 1; oldFilePath = targetFilePath; } else if(type.equals("temp")){ num = new Random().nextInt(tempfileNum) + 1; imgType = "-w.png"; oldFilePath = tempFilePath; } String path = filePath; String fileImg = num + imgType; String filePath = path + fileImg; File pathFile = new File(path); if(!pathFile.exists()){ pathFile.mkdirs(); } File file = new File(filePath); if(!file.exists()){ try { file.createNewFile(); ClassPathResource classPathResource = new ClassPathResource(oldFilePath + fileImg); InputStream inputStream = classPathResource.getInputStream(); if(inputStream.available() != 0){ FileUtils.copyInputStreamToFile(inputStream, file); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return file; } }
3.前端页面
滑动验证码
看完了这篇文章,相信你对"Java如何实现滑动验证码"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
验证
前端
工具
篇文章
成功
代码
位置
功能
完了
开发工具
效果
时间
更多
横坐标
流水
流水号
知识
缓存
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
代理服务器 安全性
国外数据库技术
内蒙古时钟监控网关服务器
数据库开发属于什么领域
网络技术sonet要大写吗
linux连接服务器加
网络技术就业能挣多少钱
超赢北京互联网科技有限公司
四个实体的数据库设计
软件开发的用户体验
多角色系统数据库设计
服务器安全狗闪退
蝙蝠app是哪里的服务器
湖南特岗服务器
国土信息空间查询数据库
广东互联网科技有限公司官网
calis第十六届数据库
怎么知道原神是哪个服务器
常州盛景网络技术中标
广东网络软件开发商家
数据库 单表查询实验总结
网络安全等级保护怎么办
国内扫爆服务器
数据库逻辑设计视图表索引
网络技术就业能挣多少钱
四个实体的数据库设计
电网视频系统网络安全
天翼1号本机网络安全吗
visio数据库分离关系
服务器可以部署几个程序