Blame view

src/main/java/fr/plil/sio/web/mvc/CheckUserInterceptor.java 1.28 KB
aa193b2b   Julien Iguchi-Cartigny   First version
1
2
  package fr.plil.sio.web.mvc;
  
aa193b2b   Julien Iguchi-Cartigny   First version
3
4
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
aa193b2b   Julien Iguchi-Cartigny   First version
5
6
  import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
7
8
9
10
11
  import javax.annotation.Resource;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
aa193b2b   Julien Iguchi-Cartigny   First version
12
13
14
  public class CheckUserInterceptor extends HandlerInterceptorAdapter {
  
      private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class);
5e965ce4   Julien Iguchi-Cartigny   working version
15
  
2070653e   Julien Iguchi-Cartigny   Switching to spri...
16
      @Resource
aa193b2b   Julien Iguchi-Cartigny   First version
17
18
19
20
21
22
      private UserSession userSession;
  
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
              Object handler) throws IOException {
  
5e965ce4   Julien Iguchi-Cartigny   working version
23
24
25
26
          logger.debug("servlet path: " + request.getServletPath());
  
          if (request.getServletPath().equals("/login")) {
              logger.debug("access granted as path is /login");
aa193b2b   Julien Iguchi-Cartigny   First version
27
28
29
30
31
32
              return true;
          }
  
          String username = userSession.getUsername();
  
          if (username != null) {
5e965ce4   Julien Iguchi-Cartigny   working version
33
              logger.debug("authenticated");
aa193b2b   Julien Iguchi-Cartigny   First version
34
35
              return true;
          } else {
5e965ce4   Julien Iguchi-Cartigny   working version
36
              logger.debug("not authenticated");
aa193b2b   Julien Iguchi-Cartigny   First version
37
38
39
40
41
42
43
44
45
              response.sendRedirect("login");
              return false;
          }
      }
  
      void setUserSession(UserSession userSession) {
          this.userSession = userSession;
      }
  }