Неверный токен CSRF, найденной для http: // localhost: 8080/api/v1/login
Когда я выполняю пост из почты на этой конечной точке http: // localhost: 8080/api/v1/login
Это контроллер.
Код: Выделить всё
@RestController
class FooController() {
@PostMapping("/api/v1/foo")
fun login(): ResponseEntity {
return ResponseEntity("I have returned something", HttpStatus.OK)
}
}
< /code>
И это конфигурация Spring Security < /p>
@Configuration
@EnableWebSecurity
class SecurityConfig {
@Bean
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
http
.csrf { csrf -> csrf.disable() }
.cors { cors -> cors.configurationSource(corsConfigurationSource()) }
.authorizeHttpRequests { auth ->
auth.requestMatchers("/**").permitAll()
.anyRequest().authenticated()
}
return http.build()
}
@Bean
fun corsConfigurationSource(): CorsConfigurationSource {
val configuration = CorsConfiguration()
configuration.allowedHeaders = listOf("*")
configuration.allowedMethods = listOf("GET", "POST", "OPTIONS", "HEAD")
configuration.allowedOrigins = listOf("*")
val source = UrlBasedCorsConfigurationSource()
source.registerCorsConfiguration("/**", configuration)
return source
}
}
< /code>
Это полная ошибка < /p>
2025-05-05t14:46:23.146+02:00 отладка 64528 --- [foobar] Токен найден для http: // localhost: 8080/api/v1/login
2025-05-05T14:46:23.146+02:00 отладки 64528 --- [forobar] [nio-808080-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex-ex. o.s.s.w.access.accessdeniedHandlerImpl: ответ с кодом состояния 403
2025-05-05T14:46:23.151+02:00 Отладка 64528 --- [FOOBAR] [NIO-8080- EXCEC-1] O.S.Security.Web.FILTERCHAINPROXIPXY: SECKUNTION < /PRIRMECITIOR < /pRIRMECER < /PRIRMECER < /PRIRMECER < /PRIRCHINSIPROXIPXY: PRIRCURESY (SECTURIN /> < /blockquote>
2025-05-05t14:46:23.155+02:00 отладки 64528 --- [FOOBAR] [Nio-8080-EXEC-1] O.S.S.W.A.AnonymousateNticationFilter: SETCERCENTEXTEXTEXTOUS /> < /blockquote>
2025-05-05t14:46:23.158++02:00 отладка 64528 --- foobar] [Nio-8080-Exec-1] s.w.a.delegatingAuthenticationEntrypoint: Попытка в матче и [не будет [expectedHeaderName=X-Requested-With, expectedHeaderValue=XMLHttpRequest]], MediaTypeRequestMatcher [contentNegotiationStrategy=org.springframework.web.accept.ContentNegotiationManager@580c4c0b, matchingMediaTypes=[application/xhtml+xml, image/, Text/html, text/plain], useequals = false, игнорированныемедедиатипы = [/*]]]
2025-05-05T14:46:23.158+02:00 DEBUG 64528 --- ФОБАР] [NIO-8080-EXEC-1] S.W.A.DelegatingAuthenticationEntryPoint: Попытка соответствовать с использованием или [requestheaderRequestmatcher [wededheadername = x-requested-with, weardheadervalue = xmlhttprequest] и [не [Mediatyperequestmatcher = xmlhttprequest], а не [ContentNegotiationStrategy=org.springframework.web.accept.contentnegotiationmanager@580c4c0b, matchmediatypes = [text/html], usesequals = false, игнорируемые медиатип = []], MediaTeperequestmatcher [contentNegotiationStrategy=org.springframework.web.accept.ContentNegotiationManager@580c4c0b, matchingMediaTypes=[application/atom+xml, application/x-www-form-urlencoded, application/json, application/octet-stream, application/xml, multipart/form-data, text/xml], useEquals=false, Игнорированныемедиатипа = [/]]], MediaTyPerequestMatcher [contentNegotiationStrategy=org.springframework.web.accept.contentnegotiationmanager@580c4c0b, matchingmediatypes = [/], useeavals = true, igno -crowntytypes = [/], usereavals = ignoredItiTypes = [/], usereavals = vigno -come -indytytypes = [/], useeavals = vigredItiTypes /> < /blockquote>
2025-05-05t14:46:23.159+02:00 отладка 64528 --- [foobar] [Nio-8080-Exec-1] S.W.A.DelegatingAuthenticationEntryPoint: Матч найден! Выполнение org.springframework.security.web.authentication.delegatingAuthenticationEntrypoint@2812368 < /p>
< /blockquote>
2025-05-05T14:46:23.159+0202025-05-05T14:46:23.1590202025. [NIO-8080-EXEC-1] S.W.A.DelegatingAuthenticationEntryPoint: Попытка соответствовать с использованием requestHeaderRequestmatcher [wededHeadername = x-requested-with, weardheadervalue = xmlhttprequest] < /p>
< /blockheadervalue = xmlhttprequest] < /p>
< /blockheadervalue>
/>2025-05-05T14:46:23.159+02:00 Отладка 64528 --- [FOOBAR] [NIO-8080-EXEC-1] S.W.A.DelegatingAuthenticationEntrypoint: не найдено совпадения. Использование точки входа по умолчанию org.springframework.security.web.authentication.www.basicauthenticationEntrypoint@357f98b5 < /p>
< /blockquote>
Это зависимости < /p>
plugins {
kotlin("jvm") version "1.9.25"
kotlin("plugin.spring") version "1.9.25"
id("org.springframework.boot") version "3.4.5"
id("io.spring.dependency-management") version "1.1.7"
kotlin("plugin.jpa") version "1.9.25"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.flywaydb:flyway-core")
implementation("org.flywaydb:flyway-database-postgresql")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.6")
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... gh-csrf-ar