java - Spring Security doesnt inject the csrf parameters on thymeleaf - Stack Overflow

I have the following form:<form th:action="@{login}" method="post" th:object=&q

I have the following form:
<form th:action="@{/login}" method="post" th:object="${usuario}">
            <!-- This is the hidden which give troubles -->
            <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
        
            <label for="username">Usuario</label>
            <input type="text" th:field="*{username}"/>
            <label for="password">Contrasenia</label>
            <input type="password" th:field="*{password}"/>
            <input type="submit" value="Login"/>
</form>

I have configured the security with the next configuration:

          @Bean
            public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
                http
                    .authorizeExchange()
                        .pathMatchers("/login").permitAll() 
                        .pathMatchers("/api/alumnos/getAlumnos","/listar","/","/index").hasRole("USER")
                        .pathMatchers("/api/alumnos/saveAlumno","/saveAlumno").hasRole("FUNCIONAL")
                        .anyExchange().authenticated()
                    .and().csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
                    .httpBasic()  // Usamos autenticación básica HTTP (puedes configurarlo con JWT si lo prefieres)
                    .and()
                    .formLogin()
                    .loginPage("/login");  // Si usas autenticación OAuth2 también puedes configurarlo aquí
        
                return http.build();
            }
    

But when I run it. It gives me the next message: EL1007E: Property or field 'parameterName' cannot be found on null

So the problem is that SpringBoot isnt injecting the _csrm to thymeleaf. I can delete that line and it will work but sending the form will give me an error saying that csrf cant be found and it will leave.

I have this on the controller. I dont think its relevant.

@PreAuthorize("hasRole('USER')")
@GetMapping({"/listar","/","/index"})
public Mono<String> getAlumnosActivos(Model model) {
            Flux<Alumno> alumnos=alumnoService.getActiveAlumnos();
            alumnos.subscribe(al->log.info(String.format("Alumno: %s", al.getNombre())));
            model.addAttribute("alumnos", alumnos);
            model.addAttribute("titulo","Listado de Alumnos");
            
            return Mono.just("index");
}

And other data which may be relevant is that Im using Spring Web Flux. Thank you for your help.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744133007a4559925.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信