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;
  
5e965ce4   Julien Iguchi-Cartigny   working version
3
4
  import java.io.IOException;
  import javax.annotation.Resource;
aa193b2b   Julien Iguchi-Cartigny   First version
5
6
7
8
9
10
11
12
13
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  
  public class CheckUserInterceptor extends HandlerInterceptorAdapter {
  
      private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class);
5e965ce4   Julien Iguchi-Cartigny   working version
14
  
aa193b2b   Julien Iguchi-Cartigny   First version
15
16
17
18
19
20
21
      @Resource
      private UserSession userSession;
  
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
              Object handler) throws IOException {
  
5e965ce4   Julien Iguchi-Cartigny   working version
22
23
24
25
          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
26
27
28
29
30
31
              return true;
          }
  
          String username = userSession.getUsername();
  
          if (username != null) {
5e965ce4   Julien Iguchi-Cartigny   working version
32
              logger.debug("authenticated");
aa193b2b   Julien Iguchi-Cartigny   First version
33
34
              return true;
          } else {
5e965ce4   Julien Iguchi-Cartigny   working version
35
              logger.debug("not authenticated");
aa193b2b   Julien Iguchi-Cartigny   First version
36
37
38
39
40
41
42
43
44
              response.sendRedirect("login");
              return false;
          }
      }
  
      void setUserSession(UserSession userSession) {
          this.userSession = userSession;
      }
  }