I'm trying to do a POST request to my backend REST API using the browser's Fetch API like this:
submitRegistration() {
const formElement = document.getElementById('register-form');
fetch('http://localhost:8080/register', {
method: 'POST',
body: new FormData(formElement),
});
}
I'm taking the data from this form:
<v-form id="register-form" @submit.prevent="submitRegistration" class="text-center" ref="form" v-model="valid" lazy-validation>
<v-text-field type="text" name="username" v-model="form.username" :counter="25" :rules="usernameRules" label="Username" required></v-text-field>
<v-text-field type="password" name="userPassword" v-model="form.password" :counter="25" :rules="passwordRules" label="Password" required></v-text-field>
<v-text-field type="text" name="userEmail" v-model="form.email" :rules="emailRules" label="E-mail" required></v-text-field>
<v-btn type="submit" text class="primary">Register</v-btn>
</v-form>
However, as a response I get 403 Forbidden by my backend server. The problem is the CSRF token but I have no idea how to generate it to include it in the POST request to the backend API.
EDIT: In Spring Boot, my CORS mapping is configured like this:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:9000").allowCredentials(true);
}
};
}
I'm posting this because the Response
object that is returned gives me a 403 status and is of type "cors" even though the error is resolved if I completely disable CSRF using HttpSecurity - http.csrf().disable();