1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class UserController {
private Map<String, User> userDatabase = new HashMap<>() { { List<User> users = List.of( // new User("bob@example.com", "bob123", "Bob", "This is bob."), new User("tom@example.com", "tomcat", "Tom", "This is tom.")); users.forEach(user -> { put(user.email, user); }); } }; @GetMapping("/signin") public ModelAndView signin() { return new ModelAndView("/signin.html"); } @PostMapping("/signin") public ModelAndView doSignin(SignInBean bean, HttpServletResponse response, HttpSession session) throws IOException { User user = userDatabase.get(bean.email); if (user == null || !user.password.equals(bean.password)) { response.setContentType("application/json"); PrintWriter pw = response.getWriter(); pw.write("{\"error\":\"Bad email or password\"}"); pw.flush(); } else { session.setAttribute("user", user); response.setContentType("application/json"); PrintWriter pw = response.getWriter(); pw.write("{\"result\":true}"); pw.flush(); } return null; }
|