Blame view

src/main/java/fr/plil/sio/web/mvc/LoginController.java 1.51 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
5
6
7
8
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
9
10
  import javax.annotation.Resource;
  
aa193b2b   Julien Iguchi-Cartigny   First version
11
  @Controller
5e965ce4   Julien Iguchi-Cartigny   working version
12
  @RequestMapping(value = "/login")
aa193b2b   Julien Iguchi-Cartigny   First version
13
14
15
  public class LoginController {
  
      @Resource
8efec875   Julien Iguchi-Cartigny   switch to spring ...
16
      private UserRepository userRepository;
aa193b2b   Julien Iguchi-Cartigny   First version
17
18
19
20
      
      @Resource
      private UserSession userSession;
  
5e965ce4   Julien Iguchi-Cartigny   working version
21
      @RequestMapping(method = RequestMethod.GET)
aa193b2b   Julien Iguchi-Cartigny   First version
22
23
24
25
      public ModelAndView getLoginForm() {
          return new ModelAndView("login", "user", new User());
      }
  
5e965ce4   Julien Iguchi-Cartigny   working version
26
      @RequestMapping(method = RequestMethod.POST)
aa193b2b   Julien Iguchi-Cartigny   First version
27
28
      public String postLoginCheck(User user, BindingResult result) {
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
29
          User userFromDao = userRepository.findByUsername(user.getUsername());
aa193b2b   Julien Iguchi-Cartigny   First version
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
          if (userFromDao == null) {
              result.rejectValue("username","login.form.invalid");
              return "login";
          }
  
          if(!userFromDao.getPassword().equals(user.getPassword())) {
              result.rejectValue("username","login.form.invalid");
              return "login";            
          }
          
          userSession.setUsername(userFromDao.getUsername());
  
          return "redirect:/";
      }
  
8efec875   Julien Iguchi-Cartigny   switch to spring ...
46
47
      public void setUserRepository(UserRepository userRepository) {
          this.userRepository = userRepository;
aa193b2b   Julien Iguchi-Cartigny   First version
48
49
50
51
52
53
      }
  
      public void setUserSession(UserSession userSession) {
          this.userSession = userSession;
      }
  }