文章目录
- 效果展示
- 1.导入maven坐标
- 2.编写代码生成一个验证码图片
- 3.前端如何拿到验证码
- 4. 后端生成验证码
- 5前端代码
效果展示
效果,每次进入页面展现出来不同的验证码。
技术 使用别人已经写好的验证码生成器,生成图片,转为Base64编码,将验证码存在Redis里面。前端根据传过来的验证码进行登录。登录完成之后将redis里面的验证码删除。
1.导入maven坐标
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
easy-captcha 生成验证码的工具。
2.编写代码生成一个验证码图片
import com.wf.captcha.SpecCaptcha;
import java.io.FileOutputStream;
import java.io.IOException;
public class CaptchaExample {
//将验证码传给前端 存储验证码 用户登录的时候 输入验证码进行比较
public static void main(String[] args) throws IOException {
// 创建一个SpecCaptcha对象,参数为宽度、高度、验证码字符数
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 设置验证码类型为数字+字母组合
captcha.setCharType(SpecCaptcha.TYPE_DEFAULT);
// 获取生成的验证码字符
String text = captcha.text();
System.out.println("验证码: " + text);
// 输出验证码图片到文件
FileOutputStream fos = new FileOutputStream("captcha.jpg");
captcha.out(fos);
fos.close();
}
}
生成的验证码图片
3.前端如何拿到验证码
我们后端生成的是一个图片,可以将图片转为字符串即Base64编码的格式,前端通过image标签就可以展示图片。
4. 后端生成验证码
编写两个API, 在页面夹在的时候请求验证码。前端使用钩子函数。后端将验证码存在Redis里面。
那么如何保证每个用户都有不同的验证码,可以生成一个唯一的ID,这里用的是UUID,保证了用户的唯一性。
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
public class LoginController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public static final String prefixCode = "njitzx:";
//首先先请求一个code
@GetMapping("/code")
public Result getCode() throws IOException {
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 设置验证码类型为数字+字母组合
captcha.setCharType(SpecCaptcha.TYPE_DEFAULT);
String base64 = captcha.toBase64();
// 获取生成的验证码字符
String text = captcha.text();
System.out.println("验证码: " + text);
// 输出验证码图片到文件
FileOutputStream fos = new FileOutputStream("captcha.jpg");
captcha.out(fos);
fos.close();
//生成一个临时用户的id 对应每一个用户的验证码
String uuid = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(prefixCode + uuid, text, TimeConstant.CODE_TIME, TimeUnit.DAYS);
return Result.success(new CodeVo(uuid, base64));
}
@GetMapping("/login")
public Result login(LoginDTO loginDTO) {
String code = loginDTO.getCode();
String key = prefixCode + loginDTO.getUuid();
String getCode = (String) redisTemplate.opsForValue().get(key);
if (getCode == null) {
throw new RuntimeException("验证码已过期");
}
if (!getCode.equalsIgnoreCase(code)) {
throw new RuntimeException("验证码错误");
}
redisTemplate.delete(key);
return Result.success("登录成功");
}
}
5前端代码
前端 用了vue3+element组件开发
<script setup>
import {ref} from "vue";
import {Search} from '@element-plus/icons-vue'
import {onMounted} from 'vue'
import {ElMessage} from 'element-plus'
onMounted(() => {
reloadCode();
})
import {getCode,UserLogin} from '@/util/user.js'
const reloadCode = async () => {
const res = await getCode();
url.value = res.data.data.codeURL;
loginData.value.uuid = res.data.data.uuid;
}
const loginData = ref({
username: '',
password: '',
code: '',
uuid: ''
})
// const url =data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIIAAAAwCAIAAABSYzXUAAAP+ElEQVR42u1bC1BTVxp2pu12ut1O12m30+l0Ot1OO91Ot7O7Y6mtYx1rW12mOq116o6ta60PBkQE8cFjwRfVVaCiopUQiLyf0qAREHkJ8hAJhFd4Y0KA8H4EwRAgnP2OJ9yEEJK4be0rZ85kzjn3cE3+7/z/93//vS4gtvYzaAtsJrDBYGs2GGww2JoNhl8CDC0DA8L6+tKODoVKNaXV2gw0b9NOEpmQZH5CBH8gvAX0M/2fpCWeTKl/ABguSCSLeDzWl1+4sOHiRd/c3LDy8jyZrHNkxGZ8XRttJ8K3yZUPSJuIqPvoimaIKDIoEsl/pVe/JwzZra1e2dmbhcKV0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Kq+Um37MrjBmYw8Pmik1NemPv6srpW6JhCQl0Q2ZmbNqG2a8odHBoe7zzxWBgdT0BykMI+Xl9wFDwiv04JuvWzBxB5mtUVlQ0bAyjJtRq8KR/9sRKVPLQdk9CE3IX0EMUqUaFI0BgwGuY/llhuAMzsQIQXAFkER+VD7i0tyiN/pw9/C8gXiEWhkewOHh6am/yucbuwuris8qcZvkBq0W+kB26BDAKLd7c1pEY8tQXt59wCA9p8tNEZeaY/Qp6bSWlvZup+j8AL0+zHIxo7FbvdBNguwIA/ZM9Fh617YoOYQ0otOYhp5lrty04mSjVWEzrYIzMUxvpN2MOjzDmnvCqaqqyJUrxNBKrLxq2OErSFWnDZ9NITrHPG+UKbEyqpLPBxj1a/7OYstAejotvpaWWgUDpBnEQfjjVCfPV9+Ofo60JFj7SgCkMhIkIMHVVvWPDLvU9meaGQZvHJaafF5tQsrI+8zYndVcQdfSfKkZP7ASG/hKeDgJCqKsAI/x8dE/INK3wp30UYEBEszihiWNhq1b6zdtwtjCixqzVP51kreJXF1DYl/QPXeDUgM/g6vzt9Gnp/O/ZGaar5CbHhIpn91b9daxeodoOagbHsA9EEVe65HSYfFNGcNmFP3R+U580UmRWCRmL2c80NcxYA4gAfXQeIHxRHf4OZMlDQV75P3jNwuvi0HHgRig3dARl6JK+i2+OmayQZTB7hFuEdBo5VfK26Xt2p/8HTJEJ/DEhT/i2Gr5C/uPLpPt2dro6AhXYI+AWj08Hth3sf03k59Fs8Fgg8HWbDDYYLA1Gww2GGzNBoMNBluzwfCLaP8DTqVG9p9iyJQAAAAASUVORK5CYII=')
const url = ref('')
import {useRouter} from 'vue-router'
const router = useRouter()
const login = async () => {
const res = await UserLogin(loginData.value)
if (res.data.code == 200) {
ElMessage.success('登录成功')
router.push('/container')
} else {
ElMessage.error(res.data.msg)
}
}
</script>
<template>
<el-form :inline="true">
<el-form-item>
<el-input
v-model="loginData.username"
style="width: 240px"
placeholder="Please input"
clearable
/>
</el-form-item>
<el-form-item>
<el-input
v-model="loginData.password"
style="width: 240px"
type="password"
placeholder="Please input password"
show-password
/>
</el-form-item>
<el-form-item>
<el-input
v-model="loginData.code"
style="width: 120px"
placeholder="输入验证码"
:suffix-icon="Search"
/>
<el-image :src="url" style="width: 120px;height: 40px"/>
</el-form-item>
</el-form>
<!-- <el-button type="success" @click="()=>{router.push('/container')}">Login</el-button>-->
<el-button type="success" @click="login()">Login</el-button>
</template>
<style scoped>
</style>
请求的js代码
import request from '@/util/request.js'
export const getCode = () => {
return request.get('/code')
}
export const UserLogin = (data) => {
var params = new URLSearchParams();
for (let key in data) {
params.append(key, data[key])
}
return request.get('/login', {params})
}
不传验证码会报错。验证码过期会显示验证码已经过期
Redis图形化界面: