- 注冊思路:填寫注冊表單,向用戶發送郵箱,點擊郵箱中的激活鏈接激活賬號,提示激活成功並跳轉至首頁。
核心代碼如下,若存在不合理信息,則將錯誤提示封裝至map中。在controller中通過判斷返回的map是否為空,以及map中的值判斷數據和合理性,並返回給界面,顯示錯誤信息。map不為空時,生成激活碼,作為激活鏈接中的參數,並驗證激活碼是否正確。service核心方法如下。
1 public Map<String, Object> register(User user) { 2 Map<String, Object> map = new HashMap<>(); 3 4 // 空值處理 5 if (user == null) { 6 throw new IllegalArgumentException("參數不能為空!"); 7 } 8 if (StringUtils.isBlank(user.getUsername())) { 9 map.put("usernameMsg", "賬號不能為空!"); 10 return map; 11 } 12 if (StringUtils.isBlank(user.getPassword())) { 13 map.put("passwordMsg", "密碼不能為空!"); 14 return map; 15 } 16 if (StringUtils.isBlank(user.getEmail())) { 17 map.put("emailMsg", "郵箱不能為空!"); 18 return map; 19 } 20 21 // 驗證賬號 22 User u = userMapper.selectByName(user.getUsername()); 23 if (u != null) { 24 map.put("usernameMsg", "該賬號已存在!"); 25 return map; 26 } 27 28 // 驗證郵箱 29 u = userMapper.selectByEmail(user.getEmail()); 30 if (u != null) { 31 map.put("emailMsg", "該郵箱已被注冊!"); 32 return map; 33 } 34 35 // 注冊用戶 36 user.setSalt(CommunityUtil.generateUUID().substring(0, 5)); 37 user.setPassword(CommunityUtil.md5(user.getPassword() + user.getSalt())); 38 user.setType(0); 39 user.setStatus(0); 40 user.setActivationCode(CommunityUtil.generateUUID()); 41 user.setHeaderUrl(String.format("http://images.nowcoder.com/head/%dt.png", new Random().nextInt(1000))); 42 user.setCreateTime(new Date()); 43 userMapper.insertUser(user); 44 45 // 激活郵件 46 Context context = new Context(); 47 context.setVariable("email", user.getEmail()); 48 // http://localhost:8080/community/activation/101/code 49 String url = domain + contextPath + "/activation/" + user.getId() + "/" + user.getActivationCode(); 50 context.setVariable("url", url); 51 String content = templateEngine.process("/mail/activation", context); 52 mailClient.sendMail(user.getEmail(), "激活賬號", content); 53 54 return map; 55 }
通過JavaMailSender包實現了發送郵件功能時,有個細節要注意,配置類中的password不是郵箱密碼,而是郵箱授權碼!
- 登錄思路:通過Kaptcha實現驗證碼的生成,通過圖片流將圖片返回給瀏覽器,將驗證碼數字存入Session。登錄時進行驗證。新建LoginTicket類用來存儲用戶id,登錄憑證號(UUID),目的是將uuid存入Cookie,方便其他模塊獲取當前登錄用戶信息。
1 @RequestMapping(path = "/login", method = RequestMethod.POST) 2 public String login(String username, String password, String code, boolean rememberme, 3 Model model, HttpSession session, HttpServletResponse response) { 4 // 檢查驗證碼 5 String kaptcha = (String) session.getAttribute("kaptcha"); 6 if (StringUtils.isBlank(kaptcha) || StringUtils.isBlank(code) || !kaptcha.equalsIgnoreCase(code)) { 7 model.addAttribute("codeMsg", "驗證碼不正確!"); 8 return "/site/login"; 9 } 10 11 // 檢查賬號,密碼 12 int expiredSeconds = rememberme ? REMEMBER_EXPIRED_SECONDS : DEFAULT_EXPIRED_SECONDS; 13 Map<String, Object> map = userService.login(username, password, expiredSeconds); 14 if (map.containsKey("ticket")) { 15 Cookie cookie = new Cookie("ticket", map.get("ticket").toString()); 16 cookie.setPath(contextPath); 17 cookie.setMaxAge(expiredSeconds); 18 response.addCookie(cookie); 19 return "redirect:/index"; 20 } else { 21 model.addAttribute("usernameMsg", map.get("usernameMsg")); 22 model.addAttribute("passwordMsg", map.get("passwordMsg")); 23 return "/site/login"; 24 } 25 }
public Map<String, Object> login(String username, String password, int expiredSeconds) { Map<String, Object> map = new HashMap<>(); // 空值處理 if (StringUtils.isBlank(username)) { map.put("usernameMsg", "賬號不能為空!"); return map; } if (StringUtils.isBlank(password)) { map.put("passwordMsg", "密碼不能為空!"); return map; } // 驗證賬號 User user = userMapper.selectByName(username); if (user == null) { map.put("usernameMsg", "該賬號不存在!"); return map; } // 驗證狀態 if (user.getStatus() == 0) { map.put("usernameMsg", "該賬號未激活!"); return map; } // 驗證密碼 password = CommunityUtil.md5(password + user.getSalt()); if (!user.getPassword().equals(password)) { map.put("passwordMsg", "密碼不正確!"); return map; } // 生成登錄憑證 LoginTicket loginTicket = new LoginTicket(); loginTicket.setUserId(user.getId()); loginTicket.setTicket(CommunityUtil.generateUUID()); loginTicket.setStatus(0); loginTicket.setExpired(new Date(System.currentTimeMillis() + expiredSeconds * 1000)); loginTicketMapper.insertLoginTicket(loginTicket); map.put("ticket", loginTicket.getTicket()); return map; }