I try to override csrf token creation with CsrfTokenRepository
. The solution works well with spring-boot 2.x but does not work with 3.x:
Configuration class:
@Configuration
@EnableWebSecurity
public class ProjectConfig {
@Bean
public CsrfTokenRepository customTokenRepository() {
return new CustomCsrfTokenRepository();
}
@Bean
public SecurityFilterChain configuration(HttpSecurity http) throws Exception {
http
.csrf(c -> {
c.csrfTokenRepository(customTokenRepository());
})
.authorizeHttpRequests()
.anyRequest().permitAll();
return http.build();
}
- CustomCsrfTokenRepository:
public class CustomCsrfTokenRepository implements CsrfTokenRepository {
@Override
public CsrfToken generateToken(HttpServletRequest request){
log.info(1);
{
@Override
public void saveToken(CsrfToken token, HttpServletRequest request, HttpServletResponse response){
log.info(2);
}
@Override
public CsrfToken loadToken(HttpServletRequest request){
log.info(3);
}
}
And I created a simple endpoint:
@GetMapping("/hello")
public String getHello() {
return "Get Hello!";
}
Now, when I get /hello endpoint I expect that code gets to the CustomCsrfTokenRepository#loadToken
method (as with spring-boot 2.x), but I receive only the "Get Hello!" response.
I determined the difference between 2.x and 3.x spring boot in that the CsrfTokenRepository
imports different HttpServlet...
instances: for 2.x from javax.servlet.http...
library, for 3.x - from jakatra..
.
How to correctly repeat this solution in spring-boot 3.x?