What is the best way to handle Invalid CSRF token found in the request when session times out in Spr
Posted 沧海一滴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了What is the best way to handle Invalid CSRF token found in the request when session times out in Spr相关的知识,希望对你有一定的参考价值。
18.5.1 Timeouts
One issue is that the expected CSRF token is stored in the HttpSession, so as soon as the HttpSession expires your configured AccessDeniedHandler
will receive a InvalidCsrfTokenException. If you are using the default AccessDeniedHandler
, the browser will get an HTTP 403 and display a poor error message.
One might ask why the expected |
A simple way to mitigate an active user experiencing a timeout is to have some javascript that lets the user know their session is about to expire. The user can click a button to continue and refresh the session.
Alternatively, specifying a custom AccessDeniedHandler
allows you to process the InvalidCsrfTokenException
any way you like. For an example of how to customize the AccessDeniedHandler
refer to the provided links for both xml and Java configuration.
Finally, the application can be configured to use CookieCsrfTokenRepository which will not expire. As previously mentioned, this is not as secure as using a session, but in many cases can be good enough.
https://docs.spring.io/spring-security/site/docs/4.2.3.RELEASE/reference/htmlsingle/#csrf-timeouts
What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security
The easiest way I found to handle invalidate CSRF token when session times out at the login page is one of the followings:
-
Redirect the request again to the login page again vi CustomAccessDeniedHandler:
static class CustomAccessDeniedHandler extends AccessDeniedHandlerImpl{ @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { if (accessDeniedException instanceof MissingCsrfTokenException || accessDeniedException instanceof InvalidCsrfTokenException) { if(request.getRequestURI().contains("login")){ response.sendRedirect(request.getContextPath()+"/login"); } } super.handle(request, response, accessDeniedException); } }
-
Add refresh header as Neil McGuigan suggested:
<meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval}">
- Furthermore you must create a bean for the new CustomAccessDeniedHandler and register it. The following example shows this for Java config.
In any config class:
@Bean
public AccessDeniedHandler accessDeniedHandler() {
return new CustomAccessDeniedHandler();
}
In your security config modify the configure method as follows:
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
// ...
.and()
.exceptionHandling().accessDeniedHandler(accessDeniedHandler());
}
Also see here.
a more Optimum solution will be for Spring security to handle this situation in their framework.
https://stackoverflow.com/questions/32446903/what-is-the-best-way-to-handle-invalid-csrf-token-found-in-the-request-when-sess
未找到预期的CSRF令牌。您的会话已过期403
https://gxnotes.com/article/245164.html
Spring Security – Customize the 403 Forbidden/Access Denied Page
http://www.baeldung.com/spring-security-custom-access-denied-page
以上是关于What is the best way to handle Invalid CSRF token found in the request when session times out in Spr的主要内容,如果未能解决你的问题,请参考以下文章
The best way to learn a programming language
How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
Latest GPYC Exam Cram - Best Way To Pass The GPYC Questions
Latest GCFA Exam Cram - Best Way To Pass The GCFA Questions
Latest SIAMF Exam Cram - Best Way To Pass The SIAMF Questions
Latest 352-011 Exam Cram - Best Way To Pass The 352 011 Questions