Class CORSService


  • public class CORSService
    extends Object
    See http://www.w3.org/TR/cors/ for further information. Once constructed, this service serves a number of purposes:
    • Validates requests are CORS (by analysis of the Origin header).
    • Determines if a request is to follow the preflight or actual request flow.
    • Verifies the requests are valid in this configuration of CORS (through comparison against pre- configured values.
    • Alters the responses such that they are valid for CORS.
    • Constructor Detail

      • CORSService

        public CORSService​(boolean enabled,
                           List<String> acceptedOrigins,
                           List<String> acceptedMethods,
                           List<String> acceptedHeaders,
                           List<String> exposedHeaders,
                           int maxAge,
                           boolean allowCredentials,
                           String expectedHostname)
        Constructor setting all necessary values to create a usable CORSService. If the maxAge parameter is less than zero, we will set it to zero here. The acceptedHeaders and exposedHeaders lists must contain at least one entry each.
        Parameters:
        acceptedOrigins - A list of origins from which we accept CORS requests
        acceptedMethods - A list of HTTP methods of whose type we accept CORS requests
        acceptedHeaders - (Optional) A list of HTTP headers which - if included - in the request don't make it abort
        exposedHeaders - (Optional) The list of headers which the user-agent can expose to its CORS-client
        maxAge - The maximum cache-length of the pre-flight response by the CORS-client in seconds
        allowCredentials - Whether we include the allow credentials header
        expectedHostname - (Optional) the name of the host the request should be headed to in its Host header
        Throws:
        IllegalArgumentException - If the acceptedOrigins or acceptedMethods params are null or empty
    • Method Detail

      • handleRequest

        public boolean handleRequest​(jakarta.servlet.http.HttpServletRequest req,
                                     jakarta.servlet.http.HttpServletResponse res)
                              throws IOException
        Entry point for classes using the service. Validates requests against the CORS specification, and add headers required to the response.
        Parameters:
        req - CORS HTTP request
        res - HTTP response
        Returns:
        true if the caller is to continue processing the request
        Throws:
        IOException - if an I/O error occurs while handling the response