add<验证码校验>

v3
liyansheng 2025-01-13 23:54:21 +08:00
parent 66449ed821
commit d546287891
2 changed files with 35 additions and 19 deletions

View File

@ -22,7 +22,13 @@ public class LoginServlet extends HttpServlet {
req.setCharacterEncoding("utf-8"); req.setCharacterEncoding("utf-8");
String username = req.getParameter("username"); String username = req.getParameter("username");
String password = req.getParameter("password"); String password = req.getParameter("password");
String captcha = req.getParameter("captcha");
try { try {
if(!captcha.equalsIgnoreCase((String)req.getSession().getAttribute("captcha"))){
req.getSession().setAttribute("msg","验证码错误");
resp.sendRedirect("/msg.jsp");
return;
}
User user = userService.login(username, password); User user = userService.login(username, password);
if(user!=null){ if(user!=null){
req.getSession().setAttribute("user",user); req.getSession().setAttribute("user",user);

View File

@ -4,6 +4,13 @@
<head> <head>
<script src="https://www.liyansheng.top/cdn/watermark.js"></script> <script src="https://www.liyansheng.top/cdn/watermark.js"></script>
<script>
// 定义一个函数,用于刷新验证码
function refreshCaptcha() {
const captchaImage = document.getElementById("captchaImage");
captchaImage.src = "/captcha?timestamp=" + new Date().getTime(); // 添加时间戳避免缓存
}
</script>
</head> </head>
<body> <body>
@ -13,6 +20,9 @@
<input type="text" id="username" name="username" required><br><br> <input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label> <label for="password">密码:</label>
<input type="password" id="password" name="password" required><br><br> <input type="password" id="password" name="password" required><br><br>
<label for="captcha">验证码</label>
<input type="text" id="captcha" name="captcha" required>
<img id="captchaImage" src="/captcha" alt="验证码" onclick="refreshCaptcha()" style="cursor: pointer;" title="点击刷新验证码"><br><br>
<button type="submit">登录</button> <button type="submit">登录</button>
<a href="/register">没有账号?去注册</a> <a href="/register">没有账号?去注册</a>
</form> </form>