老师,我这个登录页面一直显示验证码错误
package com.imooc.reader.controller; import com.imooc.reader.entity.Member; import com.imooc.reader.service.MemberService; import com.imooc.reader.service.exception.BussinessException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; @Controller public class MemberController { @Resource private MemberService memberService; @GetMapping("/register.html") public ModelAndView showRegister(){ return new ModelAndView("/register"); } @GetMapping("/login.html") public ModelAndView showLogin(){ return new ModelAndView("/login"); } @PostMapping("/registe") @ResponseBody public Map registe(String vc, String username, String password, String nickname, HttpServletRequest request){ //正确验证码 String verifyCode = (String)request.getSession().getAttribute("KaptchaVerifyCode"); //验证码比对 Map result = new HashMap(); if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){ result.put("code", "VC01"); result.put("msg", "验证码错误"); }else{ result.put("code", "0"); result.put("msg", "success"); } try { memberService.createMember(username, password, nickname); result.put("code", "0"); result.put("msg", "success"); }catch(BussinessException ex){ ex.printStackTrace(); result.put("code", ex.getCode()); result.put("msg", ex.getMsg()); } return result; } @PostMapping("/check_login") @ResponseBody public Map checkLogin(String username, String password, String vc , HttpSession session){ //正确验证码 String verifyCode = (String)session.getAttribute("kaptchaVerifyCode"); //验证码对比 Map result = new HashMap(); if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){ result.put("code", "VC01"); result.put("msg", "验证码错误"); }else{ try { Member member = memberService.checkLogin(username, password); session.setAttribute("loginMember" , member); result.put("code", "0"); result.put("msg", "success"); }catch (BussinessException ex){ ex.printStackTrace(); result.put("code", ex.getCode()); result.put("msg", ex.getMsg()); } } return result; } }
package com.imooc.reader.service; import com.imooc.reader.entity.Member; public interface MemberService { /** * 会员注册 * @param username 用户名 * @param password 密码 * @param nickname 昵称 * @return 新会员对象 */ public Member createMember(String username, String password, String nickname); /** * 登录检查 * @param username 用户名 * @param password 密码 * @return 登录対象 */ public Member checkLogin(String username, String password); }
package com.imooc.reader.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.imooc.reader.entity.Member; import com.imooc.reader.mapper.MemberMapper; import com.imooc.reader.service.MemberService; import com.imooc.reader.service.exception.BussinessException; import com.imooc.reader.utils.MD5Utils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Random; @Service("memberService") @Transactional public class MemberServiceImpl implements MemberService { @Resource private MemberMapper memberMapper; /** * 会员注册,创建新会员 * * @param username 用户名 * @param password 密码 * @param nickname 昵称 * @return 新会员对象 */ public Member createMember(String username, String password, String nickname) { QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>(); queryWrapper.eq("username", username); List<Member> memberList = memberMapper.selectList(queryWrapper); //判断用户名是否已存在 if(memberList.size() > 0){ throw new BussinessException("M01","用户名已存在"); } Member member = new Member(); member.setUsername(username); member.setNickname(nickname); int salt = new Random().nextInt(1000) + 1000; //盐值 String md5 = MD5Utils.md5Digest(password, salt); member.setPassword(md5); member.setSalt(salt); member.setCreateTime(new Date()); memberMapper.insert(member); return member; } /** * 登录检查 * * @param username 用户名 * @param password 密码 * @return 登录对象 */ public Member checkLogin(String username, String password) { QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>(); queryWrapper.eq("username", username); Member member = memberMapper.selectOne(queryWrapper); if(member == null){ throw new BussinessException("M02", "用户不存在"); } String md5 = MD5Utils.md5Digest(password, member.getSalt()); if(!md5.equals(member.getPassword())){ throw new BussinessException("M03", "输入密码有误"); } return member; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>会员登录-慕课书评网</title> <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0,user-scalable=no"> <link rel="stylesheet" href="http://cdn.itlaoqi.com./resources/bootstrap4/css/bootstrap.css"> <link rel="stylesheet" href="./resources/raty/lib/jquery.raty.css"> <script src="http://cdn.itlaoqi.com./resources/jquery.3.3.1.min.js"></script> <script src="http://cdn.itlaoqi.com./resources/bootstrap4/js/bootstrap.min.js"></script> <style> .container { padding: 0px; margin: 0px; } .row { padding: 0px; margin: 0px; } .col- * { padding: 0px; } .description p { text-indent: 2em; } .description img { width: 100%; } </style> </head> <body> <!--<div style="width: 375px;margin-left: auto;margin-right: auto;">--> <div class="container "> <nav class="navbar navbar-light bg-white shadow"> <ul class="nav"> <li class="nav-item"> <a href="/"> <img src="https://m.imooc.com/static/wap/static/common/img/logo2.png" class="mt-1" style="width: 100px"> </a> </li> </ul> </nav> <div class="container mt-2 p-2 m-0"> <form id="frmLogin"> <div class="passport bg-white"> <h4 class="float-left">会员登录</h4> <h6 class="float-right pt-2"><a href="/register.html">会员注册</a></h6> <div class="clearfix"></div> <div class="alert d-none mt-2" id="tips" role="alert"> </div> <div class="input-group mt-2 "> <input type="text" id="username" name="username" class="form-control p-4" placeholder="请输入用户名" aria-label="Username" aria-describedby="basic-addon1"> </div> <div class="input-group mt-4 "> <input id="password" name="password" class="form-control p-4" placeholder="请输入密码" type="password" aria-describedby="basic-addon1"> </div> <div class="input-group mt-4 "> <div class="col-5 p-0"> <input type="text" id="verifyCode" name="vc" class="form-control p-4" placeholder="验证码"> </div> <div class="col-4 p-0 pl-2 pt-0"> <img id="imgVerifyCode" src="/verify_code" style="width: 120px;height:50px;cursor: pointer"> </div> </div> <a id="btnSubmit" class="btn btn-success btn-block mt-4 text-white pt-3 pb-3">登 录</a> </div> </form> </div> </div> <script> function showTips(isShow, css, text) { if (isShow) { $("#tips").removeClass("d-none") $("#tips").hide(); $("#tips").addClass(css); $("#tips").text(text); $("#tips").fadeIn(200); } else { $("#tips").text(""); $("#tips").fadeOut(200); $("#tips").removeClass(); $("#tips").addClass("alert") } } function reloadVerifyCode(){ $("#imgVerifyCode").attr("src", "/verify_code?ts=" + new Date().getTime()); } $("#imgVerifyCode").click(function () { reloadVerifyCode(); }); $("#btnSubmit").click(function () { var username = $.trim($("#username").val()); var regex = /^.{1,10}$/; if (!regex.test(username)) { showTips(true, "alert-danger", "用户名请输入正确格式(1-10位)"); return; } else { showTips(false); } var password = $.trim($("#password").val()); if (!regex.test(password)) { showTips(true, "alert-danger", "密码请输入正确格式(1-10位)"); return; } else { showTips(false); } $btnReg = $(this); $btnReg.text("正在处理..."); $btnReg.attr("disabled", "disabled"); $.ajax({ url: "/check_login", type: "post", dataType: "json", data: $("#frmLogin").serialize(), success: function (data) { console.info(data); if (data.code == "0") { window.location = "/?ts=" + new Date().getTime(); } else { showTips(true, "alert-danger", data.msg); reloadVerifyCode(); $btnReg.text("登录"); $btnReg.removeAttr("disabled"); } } }); return false; }); </script> </body> </html>
我无论怎么输验证码都不行
38
收起
正在回答
3回答
同学你好,1、根据同学的描述,只有在checkLogin方法中获取不到验证码,同学可以尝试重新写一个方法再来尝试获取一下session中的verifyCode,查看能否正确获取。
2、同学可以尝试重新新建一个项目再来试试或者尝试运行一下源码再来对比一下,老师检查贴出的checkLogin与源码是一致的,并没有问题。
继续加油 祝:学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星