Idea中maven项目实现登录验证码功能

作者:旧茶忆故人 时间:2023-11-29 16:50:51 

1、配置maven环境变量,将maven安装的bin⽬录添加到path路径中(此电脑->属性->高级系统设置->环境变量->)

Idea中maven项目实现登录验证码功能

路径为maven安装目录

Idea中maven项目实现登录验证码功能

2、找到ValidateCode.jar包的本地路径

Idea中maven项目实现登录验证码功能

3、制作Jar包

原jar包地址:链接: https://pan.baidu.com/s/1QpqiZaF_ZYhW1Qn3ifn2eg 提取码: uc47

无法直接使用,需要命令行制作,命令如下:


mvn install:install-file -DgroupId=it.source
-DartifactId=ValidateCode -Dversion=1.0
-Dpackaging=jar -Dfile=C:\Users\xiyang\Desktop\ValidateCode-1.0.jar

‘C:\Users\xiyang\Desktop\ValidateCode-1.0.jar'为jar包路径

4、成功效果

Idea中maven项目实现登录验证码功能

5、在maven项目的pom.xml文件中添加依赖


<dependency>
<groupId>cn.dsna.util.images</groupId>
<artifactId>ValidateCode</artifactId>
<version>1.0</version>
</dependency>

注意:‘cn.dsna.util.images'为依赖的路径,笔者是将jar包放在本地maven仓库的cn/dsna/util/images路径下

6、前端html实现


<!DOCTYPE html>
<html class="loginHtml" lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>后台登录</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">

<link rel="icon" href="img/ico.ico" rel="external nofollow" >
<link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" media="all" />
<link rel="stylesheet" href="css/public.css" rel="external nofollow" media="all" />
</head>
<body class="loginBody">
<form class="layui-form" >
<div class="login_face"><img src="" class="userAvatar" style="width: 100%;height: 100%"></div>
<div class="layui-form-item input-item">
 <label for="userName">用户名</label>
 <input type="text" placeholder="请输入用户名" autocomplete="off" id="username" name="username" class="layui-input" lay-verify="required">
</div>
<div class="layui-form-item input-item">
 <label for="password">密码</label>
 <input type="password" placeholder="请输入密码" autocomplete="off" id="password" name="password" class="layui-input" lay-verify="required">
</div>
<div class="layui-form-item input-item" id="imgCode">
 <label for="code">验证码</label>
 <input type="text" placeholder="请输⼊验证码" autocomplete="off" id="code" name="code" class="layui-input">
 <img src="http://localhost:8080/home/code" onclick="changeCode()" id="codeImg">
</div>
<div class="layui-form-item">
 <button class="layui-btn layui-block" lay-filter="login" lay-submit>登录</button>
</div>
</form>

</body>

<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="js/login.js"></script>
</html>

注意:页面是layui框架渲染的,layui官网: https://www.layui.com/

也可以在网盘中下载:链接: https://pan.baidu.com/s/1QpqiZaF_ZYhW1Qn3ifn2eg 提取码: uc47

7、前端js代码(login.js)


//点击验证码进⾏刷新验证码,(登陆失败以后,重新调⽤该⽅法去刷新验证码)
function changeCode(){
var img = document.getElementById("codeImg");
//注意:如果请求⽹址完全相同 则浏览器不会帮你刷新
//可以拼接当前时间 让每次请求的⽹址都不⼀样
img.src ="http://localhost:8080/home/code?time="+new Date().getTime();
}

8、后端java代码(控制层)


//获取验证码
@GetMapping("code")
public void getCode(HttpServletRequest request, HttpServletResponse response){
//参数列表:宽度,⾼度,字符数,⼲扰线数量
ValidateCode vs = new ValidateCode(120,40,5,100);
//获取文本
//String code = vs.getCode();
//将文本放入session中,一个公共的存储空间,存值的方式是key-value
try {
request.getSession().setAttribute("code",vs.getCode());
request.getSession().setMaxInactiveInterval(300);//永不过期为-1
vs.write(response.getOutputStream());
} catch (IOException e) {//有io流就可能有io流异常,就要加try catch语句
e.printStackTrace();
}
}

9、最终效果

Idea中maven项目实现登录验证码功能

点击验证码自动刷新

来源:https://segmentfault.com/a/1190000038695388

标签:Idea,maven,登录,验证码
0
投稿

猜你喜欢

  • Android开发之使用ViewPager实现图片左右滑动切换效果

    2022-11-12 11:44:59
  • Android实现跳动的小球加载动画效果

    2022-01-24 19:43:28
  • C#利用ScriptControl动态执行JS和VBS脚本

    2023-04-29 21:26:13
  • MyBatis批量查询、插入、更新、删除的实现示例

    2021-07-21 22:33:16
  • SpringBoot集成gRPC微服务工程搭建实践的方法

    2022-03-11 22:10:39
  • 新手入门Jvm--jvm概览

    2023-11-09 15:42:31
  • MyBatis查询数据返回null的解决

    2021-11-17 20:46:48
  • Unity游戏之存储数据

    2022-06-06 20:58:28
  • Linux下执行java程序的方法

    2023-01-25 07:22:23
  • Java中构造函数,set/get方法和toString方法使用及注意说明

    2021-07-15 13:01:39
  • 详解Android StrictMode严格模式的使用方法

    2023-09-14 17:03:39
  • C#中抽象类与接口的区别详解

    2023-08-12 22:33:01
  • Android开发使用Databinding实现关注功能mvvp

    2023-07-23 19:27:32
  • 10种简单的Java性能优化

    2023-06-20 20:43:41
  • C#函数式编程中的惰性求值详解

    2022-01-27 03:07:29
  • SpringBoot整合TKMyBatis实现单表增删改查操作

    2022-01-30 19:52:28
  • 浅谈C#指针问题

    2022-07-19 17:06:00
  • 使用SpringMVC在redirect重定向的时候携带参数的问题

    2021-06-17 05:18:43
  • C#中Array与ArrayList用法及转换的方法

    2021-07-18 13:13:23
  • C# WPF Image控件的绑定方法

    2023-09-03 06:41:42
  • asp之家 软件编程 m.aspxhome.com