谢谢!只是验证码如何和session绑定啊?前后台的代码如何写?前台的代码该如何写啊?要求点一下,验证码就变一个的。后台大致是这样的吧?
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//要接收 登录传来的三个参数
// 1.修正编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
//2.接收前端过来的三个参数
String userName = req.getParameter("userName");
String userPwd = req.getParameter("userPwd");
String code = req.getParameter("code");
System.out.println("code = " + code);
//3.第一次登录的时候,首先要验证 验证码是否正确
//3.1获取后台的验证码
HttpSession session = req.getSession();
String codeFormSession = (String) session.getAttribute("code");
System.out.println("codeFormSession = " + codeFormSession);
if(!codeFormSession.equals(code)){
//验证错误,注意前面有!
//想前端输入一段json,告知前端验证码错误.
PrintWriter writer = resp.getWriter();
Map map = new HashMap();
map.put("code",400);
map.put("msg","验证码不正确");
//把map变为json
String jsonString = JSONObject.toJSONString(map);
writer.print(jsonString);
writer.close();
后台 原文链接:
https://blog.csdn.net/qqyang_/article/details/119577526【 在 laoshifu 的大作中提到: 】
: 你的验证码和uuid绑定。。。 这也是够奇葩的了,有安全问题。
: 验证码得和session绑定,uuid只是为了破除浏览器的请求缓存进而触发验证码的后台和
: 前台更新。
: ...................
--
FROM 120.242.253.*