自定义AuthenticationFailureHandler中的Flash属性 [英] Flash Attribute in custom AuthenticationFailureHandler
问题描述
在登录失败时,我想将用户重定向到错误页面并显示有意义的错误消息.是否可以添加将传递给后续请求的Flash属性?
On login failure I want to redirect the user to an error page and display a meaningful error message. Is it possible to add Flash Attributes that will be passed to the subsequent request?
下面显示的代码不起作用. RequestContextUtils.getOutputFlashMap()
返回null
.
The code presented below doesn't work. RequestContextUtils.getOutputFlashMap()
returns 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
维护的,而请求的源代码却没有. t通过了这一点.
I'd guess it's null because you are calling the function from the filter chain, whereas the flash map is maintained by Spring's DispatcherServlet
which the request hasn't passed through at this point.
为什么不只使用参数?即
Why not just use a parameter? i.e
response.sendRedirect(request.getContextPath()+"/error?error=" + "Error Message");
这篇关于自定义AuthenticationFailureHandler中的Flash属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!