一 驗證碼
登錄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; }