|
1 | 1 | package org.baeldung.web.interceptor; |
2 | 2 |
|
3 | | -import javax.servlet.http.HttpServletRequest; |
4 | | -import javax.servlet.http.HttpServletResponse; |
5 | | -import javax.servlet.http.HttpSession; |
6 | | - |
7 | 3 | import org.slf4j.Logger; |
8 | 4 | import org.slf4j.LoggerFactory; |
9 | 5 | import org.springframework.security.core.context.SecurityContextHolder; |
|
12 | 8 | import org.springframework.web.servlet.View; |
13 | 9 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
14 | 10 |
|
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
| 13 | +import javax.servlet.http.HttpSession; |
| 14 | + |
15 | 15 | public class UserInterceptor extends HandlerInterceptorAdapter { |
16 | 16 |
|
17 | | - private static Logger log = LoggerFactory.getLogger(UserInterceptor.class); |
| 17 | + private static Logger log = LoggerFactory.getLogger(UserInterceptor.class); |
18 | 18 |
|
19 | 19 | /** |
20 | 20 | * Executed before actual handler is executed |
21 | 21 | **/ |
22 | | - @Override |
23 | | - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { |
24 | | - if (isUserLogged()) { |
25 | | - addToModelUserDetails(request.getSession()); |
26 | | - } |
27 | | - return true; |
28 | | - } |
29 | | - |
30 | | - /** |
| 22 | + @Override |
| 23 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { |
| 24 | + if (isUserLogged()) { |
| 25 | + addToModelUserDetails(request.getSession()); |
| 26 | + } |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
31 | 31 | * Executed before after handler is executed. If view is a redirect view, we don't need to execute postHandle |
32 | 32 | **/ |
33 | | - @Override |
34 | | - public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model) |
35 | | - throws Exception { |
36 | | - if (model != null && !isRedirectView(model)) { |
37 | | - if (isUserLogged()) { |
38 | | - addToModelUserDetails(model); |
39 | | - } |
40 | | - } |
41 | | - } |
| 33 | + @Override |
| 34 | + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView model) |
| 35 | + throws Exception { |
| 36 | + if (model != null && !isRedirectView(model)) { |
| 37 | + if (isUserLogged()) { |
| 38 | + addToModelUserDetails(model); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
42 | 42 |
|
43 | | - /** |
44 | | - * Used before model is generated, based on session |
45 | | - * @param session |
46 | | - */ |
47 | | - private void addToModelUserDetails(HttpSession session) { |
48 | | - log.info("================= addToModelUserDetails ============================"); |
49 | | - String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName(); |
50 | | - session.setAttribute("username", loggedUsername); |
51 | | - log.info("user(" + loggedUsername + ") session : " + session); |
52 | | - log.info("================= addToModelUserDetails ============================"); |
| 43 | + /** |
| 44 | + * Used before model is generated, based on session |
| 45 | + */ |
| 46 | + private void addToModelUserDetails(HttpSession session) { |
| 47 | + log.info("================= addToModelUserDetails ============================"); |
| 48 | + String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName(); |
| 49 | + session.setAttribute("username", loggedUsername); |
| 50 | + log.info("user(" + loggedUsername + ") session : " + session); |
| 51 | + log.info("================= addToModelUserDetails ============================"); |
| 52 | + |
| 53 | + } |
53 | 54 |
|
54 | | - } |
| 55 | + /** |
| 56 | + * Used when model is available |
| 57 | + */ |
| 58 | + private void addToModelUserDetails(ModelAndView model) { |
| 59 | + log.info("================= addToModelUserDetails ============================"); |
| 60 | + String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName(); |
| 61 | + model.addObject("loggedUsername", loggedUsername); |
| 62 | + log.trace("session : " + model.getModel()); |
| 63 | + log.info("================= addToModelUserDetails ============================"); |
55 | 64 |
|
56 | | - /** |
57 | | - * Used when model is available |
58 | | - * @param model |
59 | | - */ |
60 | | - private void addToModelUserDetails(ModelAndView model) { |
61 | | - log.info("================= addToModelUserDetails ============================"); |
62 | | - String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName(); |
63 | | - model.addObject("loggedUsername", loggedUsername); |
64 | | - log.trace("session : " + model.getModel()); |
65 | | - log.info("================= addToModelUserDetails ============================"); |
| 65 | + } |
66 | 66 |
|
67 | | - } |
| 67 | + public static boolean isRedirectView(ModelAndView mv) { |
68 | 68 |
|
69 | | - public static boolean isRedirectView(ModelAndView mv) { |
| 69 | + String viewName = mv.getViewName(); |
| 70 | + if (viewName.startsWith("redirect:/")) { |
| 71 | + return true; |
| 72 | + } |
70 | 73 |
|
71 | | - String viewName = mv.getViewName(); |
72 | | - if (viewName.startsWith("redirect:/")) { |
73 | | - return true; |
74 | | - } |
| 74 | + View view = mv.getView(); |
| 75 | + return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView()); |
| 76 | + } |
75 | 77 |
|
76 | | - View view = mv.getView(); |
77 | | - return (view != null && view instanceof SmartView && ((SmartView) view).isRedirectView()); |
78 | | - } |
79 | | - |
80 | | - public static boolean isUserLogged() { |
81 | | - try { |
82 | | - return !SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser"); |
83 | | - } catch (Exception e) { |
84 | | - return false; |
85 | | - } |
86 | | - } |
| 78 | + public static boolean isUserLogged() { |
| 79 | + try { |
| 80 | + return !SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser"); |
| 81 | + } catch (Exception e) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
87 | 85 | } |
0 commit comments