登录失败后,我想将用户重定向到错误页面并显示有意义的错误消息.是否可以添加将传递给后续请求的Flash属性?
下面显示的代码不起作用. RequestContextUtils.getOutputFlashMap()返回null.
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler
{
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException
{
FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
if (outputFlashMap != null)
{
outputFlashMap.put("error", "Error message");
}
response.sendRedirect(request.getContextPath() + "/error");
}
}
解决方法:
我猜这是空的,因为您是从过滤器链中调用该函数的,而flash映射是由Spring的DispatcherServlet维护的,该请求此时尚未通过.
为什么不只使用参数?即
response.sendRedirect(request.getContextPath()+"/error?error=" + "Error Message");