CheckUserInterceptor.java
1.28 KB
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
35
36
37
38
39
40
41
42
43
44
45
package fr.plil.sio.web.mvc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CheckUserInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory.getLogger(CheckUserInterceptor.class);
@Resource
private UserSession userSession;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws IOException {
logger.debug("servlet path: " + request.getServletPath());
if (request.getServletPath().equals("/login")) {
logger.debug("access granted as path is /login");
return true;
}
String username = userSession.getUsername();
if (username != null) {
logger.debug("authenticated");
return true;
} else {
logger.debug("not authenticated");
response.sendRedirect("login");
return false;
}
}
void setUserSession(UserSession userSession) {
this.userSession = userSession;
}
}