SVG验证码
主要通过后端实现,每次请求返回不一样的svg,前端渲染即可
例如node.js中使用svg-capture
// 核心代码演示
// 后端
const svgCaptcha = require('svg-captcha');
app.get('/captcha', function (req, res) {
var captcha = svgCaptcha.create();
req.session.captcha = captcha.text;
res.type('svg');
res.status(200).send(captcha.data);
});
// 前端
setup() {
// 每次点击重新获取验证码
const onClick = event => {
event.target.src = `http://localhost:8888/captcha?t=${Math.random()}`;
}
return () => <img src="http://localhost:8888/captcha" alt onClick={ onClick } />
}