add<验证码校验>
parent
66449ed821
commit
d546287891
|
@ -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);
|
||||||
|
|
|
@ -1,23 +1,33 @@
|
||||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
|
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
|
||||||
</head>
|
<script>
|
||||||
|
// 定义一个函数,用于刷新验证码
|
||||||
|
function refreshCaptcha() {
|
||||||
|
const captchaImage = document.getElementById("captchaImage");
|
||||||
|
captchaImage.src = "/captcha?timestamp=" + new Date().getTime(); // 添加时间戳避免缓存
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h2>-电脑商城-用户登录</h2>
|
<h2>-电脑商城-用户登录</h2>
|
||||||
<form action="login" method="post">
|
<form action="login" method="post">
|
||||||
<label for="username">用户名:</label>
|
<label for="username">用户名:</label>
|
||||||
<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>
|
||||||
<button type="submit">登录</button>
|
<label for="captcha">验证码</label>
|
||||||
<a href="/register">没有账号?去注册</a>
|
<input type="text" id="captcha" name="captcha" required>
|
||||||
</form>
|
<img id="captchaImage" src="/captcha" alt="验证码" onclick="refreshCaptcha()" style="cursor: pointer;" title="点击刷新验证码"><br><br>
|
||||||
<br>
|
<button type="submit">登录</button>
|
||||||
${requestScope.msg}
|
<a href="/register">没有账号?去注册</a>
|
||||||
</body>
|
</form>
|
||||||
|
<br>
|
||||||
|
${requestScope.msg}
|
||||||
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
Reference in New Issue