spring Boot登录验证之验证码 邮箱
一 验证码
登录login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>xxx需求管理系统</title> </head> <script src="${pageContext.request.contextPath}/js/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function () { $("#login").click(function () { var url="${pageContext.request.contextPath}/login" var json={"username":$("#username").val(),"password":$("#password").val(),"checkcode":$("#checkcode").val()}; if($("#username").val()==""){ alert("用户名不能为空") }else if($("#password").val()==""){ alert("密码不能为空") }else { function callback(msg) { if(msg==0){ alert("用户名或密码错误") }else if(msg==1){ alert("验证码错误!") }else if(msg==2){ window.location.href="${pageContext.request.contextPath}/toSxf"; } } } $.get(url,json,callback); }) }) </script> <body> <div align="center" style="margin-top: 150px"> <h2 style="color: blue">xxx需求管理系统登录界面</h2> 用户名:<input type="text" name="username" id="username"></br></br> 密   码:<input type="password" name="password" id="password"></br></br> <table align="center"> <tr> <td>验证码:</td> <td class="width50"><input id="checkcode" name="checkcode" type="text" class="width50" /></td> <td><img src="createImage" alt="验证码" title="点击更换" onclick="this.src='createImage?'+(new Date()).getTime();"/></td> <td><span id="checkcode_msg" class="required"></span></td> </tr> </table> <input type="submit" value="登录" id="login"> <a href="${pageContext.request.contextPath}/toForgetPwd">忘记密码?</a> <a href="${pageContext.request.contextPath}/toUpdatePwd">修改密码?</a> </div> </body> </html>
后台处理 loginController
@RequestMapping("/login") public void login(String username, String password,String checkcode, HttpServletResponse response,HttpSession session) throws IOException { String code =(String) session.getAttribute("number"); if (service.login(username,password)==null) { response.getWriter().println(0); } else if(checkcode==null||checkcode.length()==0||!code.equalsIgnoreCase(checkcode)){ response.getWriter().println(1); }else{ response.getWriter().println(2); } } @GetMapping("/createImage") public void createImage(HttpServletResponse response, HttpSession session) throws IOException { BufferedImage image = new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random r = new Random(); g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.fillRect(0, 0, 80, 20); //获取生成的验证码 String code = getNumber(); //绑定验证码 session.setAttribute("number", code); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25)); g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.drawString(code, 5, 25); //设置消息头 response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); ImageIO.write(image, "jpeg", os); } public String getNumber(){ String str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String code = ""; for(int i= 0;i<4;i++){ int index = (int)(Math.random()*str.length()); code+=str.charAt(index); } return code; }
二 邮箱(主要功能:输入用户名,将重置密码发往注册时与用户名绑定的邮箱)
在你的 application.yml中加入
spring:
mail:
host: (发送者邮箱类型)
form: (发送者邮箱用户名)
port: 25
username: (发送者邮箱用户名)
password: (发送者邮箱密码)
@Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String username; @RequestMapping("/forgetPwd") public ModelAndView getPwd(HttpServletResponse response,String loginName) throws IOException { ModelAndView mv=new ModelAndView(); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(username); simpleMailMessage.setTo( service.getMail(loginName));//目的邮箱 simpleMailMessage.setSubject("重置密码"); //邮箱主题 simpleMailMessage.setText("重置的密码为123456789");//邮箱内容 自定义 javaMailSender.send(simpleMailMessage); mv.setViewName("login/success"); return mv; }