BackEnd: Spring security JWT,
@CrossOrigin(origins = "http://localhost:4444", allowCredentials = "true")
@GetMapping("/")
public Flux<ServerSentEvent<List<UserResponse>>> getAll() throws CancelQueryException {
return Flux.interval(Duration.ofSeconds(3))
.flatMap(sequence -> {
List<UserResponse> data = null;
try {
data = userService.getAllUsers();
System.out.println("data: " + data);
} catch (CancelQueryException e) {
// LOGGER.error("Error occurred while retrieving users", e);
throw new RuntimeException(e);
}
return Mono.just(ServerSentEvent.<List<UserResponse>>builder()
.id(String.valueOf(sequence))
.event("update")
.data(data)
.build());
});
}
FrontEnd: React
useEffect(() => {
const token =
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJld2ZhZXJnZSIsImlhdCI6MTY4ODEwNTg4MCwiZXhwIjoxNjg4NzEwNjgwfQ.V3d_1n7QElQZpPshH_UkkR707sLJlxqz7fYmqSJ3V-4";
const config = {
withCredentials: true,
headers: {
Authorization: `Bearer ${token}`,
},
};
const url = "http://localhost:8558/monitoring-location/api/v1/admin/user/";
try {
const eventSource = new EventSource(url, config);
eventSource.onmessage = (event) => {
const receivedEvent = JSON.parse(event.data);
console.log(receivedEvent);
};
console.log("running");
return () => {
console.log("close connecttion");
eventSource.close(); // Đóng kết nối SSE khi component bị hủy
};
} catch (error) {
console.log("error ----", error);
}
}, []);
token is correct, but response is ERROR 403
please help me, thanks
I am using the SSE protocol to send data from the server to the FE, but I am getting error 403