/* * Copyright (C) 2020 Hal Perkins. All rights reserved. Permission is * hereby granted to students registered for University of Washington * CSE 331 for use solely during Winter Quarter 2021 for purposes of * the course. No other use, copying, distribution, or modification * is permitted without prior written consent. Copyrights for * third-party components of this work must be honored. Instructors * interested in reusing these course materials should contact the * author. */ package sparkDemo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import spark.Filter; import spark.Request; import spark.Response; import spark.Spark; import java.util.HashMap; // Based on code from // https://stackoverflow.com/questions/45295530/spark-cors-access-control-allow-origin-error /** * The CORSFilter disables browser-side Cross-Origin Request security features, * to allow the React application (with origin localhost:3000) to make requests * of the Spark server (with origin localhost:4567) without them being rejected. *
* Note that this is generally a security vulnerability for production applications
* (see "Cross-Site Scripting" or "XSS"), but it isn't a problem for our specific
* application.
*/
public class CORSFilter {
// ===============
// You don't need to understand how this works at all, but it's something that's necessary
// for our two-server system to work nicely together. If you're curious, read the comments
// in this class and do some searching for "CORS", "Cross-Site Scripting", and "XSS" online.
// ===============
/**
* Contains all the headers that need to be added
*/
private final HashMap