title: Can I use RedirectAttributes or FlashMap in Interceptor of Spring MVC
body: <p>I have an interceptor to handle the user session. If the <code>user</code> attribute does not exist, then the interceptor will redirect to the signin page. I want to send a <code>session timeout</code> message with the redirect url, but I don't want the message in the url. I google a lot for the <code>RedirectAttributes</code> or <code>FlashMap</code>, but I can't find any good solution.</p>
  
  <pre><code>public class UserSessionInterceptor extends HandlerInterceptorAdapter {
          protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          User user = (User)request.getSession().getAttribute(CommonConstants.USER_SESSION_KEY);
          if (user == null) {
              String msg = String.format("session timeout");
              logger.error(msg);
  
      //      response.sendRedirect("/signin?errorMessage=" + msg); //I don't want to do this..
  
              return false;
          }
  
          return true;
      }    
  }
  </code></pre>
  
  <p>signinController snippet:</p>
  
  <pre><code>@Controller
  @SessionAttributes(CommonConstants.KAPTCHA_SESSION_KEY)
  public class SigninController extends BaseController {
  
      @RequestMapping(value = "/signin", method = RequestMethod.GET)
      public String signinPage() {
          return "forward:/index.jsp";
      }
  
      @RequestMapping(value = "/signin", method = RequestMethod.POST)
      public String signin(UserForm userForm, @ModelAttribute(CommonConstants.KAPTCHA_SESSION_KEY) String captchaExpected, RedirectAttributes redirectAttributes, HttpServletRequest request) {
      userForm.setCaptchaExpected(captchaExpected);
      try {
          loginValidator.validate(userForm);
      } catch (ValidateFailedException e) {
          logger.error(e.getMessage(), e);
          redirectAttributes.addFlashAttribute(ERROR_MESSAGE_KEY, e.getMessage());
          return "redirect:/signin";
      }
  
      User user = userService.getByUsername(userForm.getUsername());
      if (user == null || !user.getPassword().equals(DigestUtils.md5Hex(userForm.getPassword()))) {
          redirectAttributes.addFlashAttribute(ERROR_MESSAGE_KEY, "username or password is invalid");
          return "redirect:/signin";
      }
      request.getSession().setAttribute(CommonConstants.USER_SESSION_KEY, user);
      return "redirect:/dashboard";
  }
  }
  </code></pre>
  
  <p>index.jsp snippet:</p>
  
  <pre><code>&lt;%@page contentType="text/html; charset=utf-8"%&gt;
  &lt;%@ include file="/WEB-INF/jsp/include.jsp" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;welcome&lt;/title&gt;
  &lt;meta http-equiv="pragma" content="no-cache" /&gt;
  &lt;meta http-equiv="keywords" content="" /&gt;
  &lt;meta http-equiv="description" content="" /&gt;
  &lt;link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /&gt;
  &lt;link rel="stylesheet" href="${ctx}/css/bootstrap.min.css"&gt;
  &lt;link rel="stylesheet" href="${ctx}/css/main.css"&gt;
  &lt;script src="${ctx}/js/jquery-1.11.1.min.js"&gt;&lt;/script&gt;
  &lt;script src="${ctx}/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
  &lt;div class="login-box text-center" &gt;
      &lt;div class="login-single-panel-header"&gt;
          &lt;h5 style="color:red"&gt;${errorMessage}&lt;/h5&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thank you very much!</p>