I am currently learning flutter web and have created a simple spring boot api, but I keep receiving the error for any network request stating
Access to XMLHttpRequest at 'http://localhost:8080/api/v1/auth/authenticate' from origin 'http://localhost:53289' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have tried adding @CrossOrigin
to my controller and event attempted adding a filter in spring but not with any luck.
For reference here is my controller
@RestController
@RequestMapping("/api/v1/auth")
class AuthenticationController(
private val authenticationService: AuthenticationService
) {
@PostMapping("/register")
fun register(
@RequestBody request: RegisterRequestBody
): ResponseEntity<AuthenticationResponse> {
return ResponseEntity.ok(authenticationService.register(request))
}
@PostMapping("/authenticate")
fun register(
@RequestBody request: AuthenticationRequest
): ResponseEntity<AuthenticationResponse> {
return ResponseEntity.ok(authenticationService.authenticate(request))
}
}
And here is the network request code in flutter
class AuthenticationApi {
var uri = Uri.http("localhost:8080","/api/v1/auth/authenticate");
Future<String?> loginWithCredentials(String username, String password) async {
var body = jsonEncode(loginBody(username, password));
final response = await http.post(uri, body: body);
if (response.statusCode == 200) {
return response.body;
} else {
return null;
}
}
}
If anyone could help me see where I am gong wrong that would be great, thanks :)
I have tried adding @CrossOrigin
to my controller and event attempted adding a filter in spring but not with any luck.