实现简单验证码

前言:

本文章为一个大一小白连写js所写,文章给出注释并准备好开始的所需的简易样式,但对于真正用于实践可能没有多大参考价值,慎入

需求

  1. 加载时能自动生成验证码
  2. 点击图片能更换验证码
  3. 能验证输入验证码是否正确

初始代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .v_code {
            width: 600px;
            margin: 0 auto;
        }
        .v_code>input {
            width: 60px;
            height: 36px;
            margin-top: 10px;
        }
        .code_show {
            overflow: hidden;
        }

        .code_show span {
            display: block;
            float: left;
            margin-bottom: 10px;
        }

        .code_show a {
            display: block;
            float: left;
            margin-top: 10px;
            margin-left: 10px;
        }

        .code {
            font-style: italic;
            background-color: #f5f5f5;
            color: blue;
            font-size: 30px;
            letter-spacing: 3px;
            font-weight: bolder;
            float: left;
            cursor: pointer;
            padding: 0 5px;
            text-align: center;
        }

        #inputCode {
            width: 100px;
            height: 30px;
        }
        
        a {
            text-decoration: none;
            font-size: 12px;
            color: #288bc4;
            cursor: pointer;
        }

        a:hover {
            text-decoration: underline;
        }
        .form{
            width: 300px;
            height: 300px;
            background-color: #eee;
        }
    </style>
</head>

<body>
    <div class="v_code">
        <div class="code_show">
            <span class="code" id="checkCode"></span>
            <a id="linkbt">看不清换一张</a>
        </div>
        <div class="input_code">
            <label for="inputCode">验证码:</label>
            <input type="text" id="inputCode" />
            <span id="text_show"></span>
        </div>
        <input id="Button1" type="button" value="确定" />
    </div>
    <script>
        
    </script>
</body>

</html>

 js部分

    <script>
        window.onload=function(){
            function getcode(){//在页面加载的时候要用,点击的时候也要用,故打包成函数
            //随机生成6位内容为0-9,a-f的验证码
            //利用数组实现随机效果,数组内容为验证码范围
            var arr=['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
            var str='';
            for (let i = 0; i < 6; i++) {
                let num=Math.round(Math.random()*(15-0)+0);//随机生成数组下标
                str +=arr[num];
            }
            return str;
            }
            //加载时更换验证码
            document.getElementById('checkCode').innerText=getcode();
            //点击更换验证码
            document.getElementById('linkbt').onclick=function(){
                document.getElementById('checkCode').innerText=getcode();
            }
            //验证验证码是否正确
            document.getElementById('Button1').onclick=function(){
                if(document.getElementById('inputCode').value!=document.getElementById('checkCode').innerText){
                    alert('输入错误,请重输');
                }
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是实现步骤: 1. 添加 Redis 依赖 在 `pom.xml` 文件中添加 Redis 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置 Redis 在 `application.properties` 文件中添加 Redis 相关配置: ```properties # Redis配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 spring.redis.timeout=60000 ``` 3. 编写验证码生成和存储逻辑 编写一个 `VerificationCodeUtil` 工具类,生成验证码并将验证码存储到 Redis 中: ```java @Component public class VerificationCodeUtil { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 生成验证码 * @param key 键 * @param expire 过期时间 * @return 验证码 */ public String generateCode(String key, long expire) { //生成四位数字的验证码 String code = String.format("%04d", new Random().nextInt(9999)); //将验证码存储到 Redis 中 redisTemplate.opsForValue().set(key, code, expire, TimeUnit.SECONDS); return code; } } ``` 4. 编写验证码登录逻辑 编写一个 `LoginController` 控制器,实现验证码登录功能: ```java @RestController public class LoginController { @Autowired private VerificationCodeUtil verificationCodeUtil; @Autowired private RedisTemplate<String, String> redisTemplate; @PostMapping("/login") public String login(String phone, String code) { //从 Redis 中获取验证码 String cacheCode = redisTemplate.opsForValue().get(phone); if (cacheCode == null) { return "验证码已过期,请重新获取"; } if (!cacheCode.equals(code)) { return "验证码错误"; } //验证验证通过,执行登录逻辑 return "登录成功"; } @GetMapping("/getCode") public String getCode(String phone) { //生成验证码并存储到 Redis 中,有效期为60秒 String code = verificationCodeUtil.generateCode(phone, 60); return code; } } ``` 至此,我们已经实现了使用 Redis 存储验证码并实现验证码登录的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值