All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.servlet.config.annotation.CorsRegistration Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.servlet.config.annotation;

import java.util.Arrays;

import org.springframework.web.cors.CorsConfiguration;

/**
 * Assists with the creation of a {@link CorsConfiguration} instance mapped to
 * a path pattern. By default all origins, headers, and credentials for
 * {@code GET}, {@code HEAD}, and {@code POST} requests are allowed while the
 * max age is set to 30 minutes.
 *
 * @author Sebastien Deleuze
 * @author Rossen Stoyanchev
 * @author Sam Brannen
 * @since 4.2
 * @see CorsConfiguration
 * @see CorsRegistry
 */
public class CorsRegistration {

	private final String pathPattern;

	private final CorsConfiguration config;


	/**
	 * Create a new {@link CorsRegistration} that allows all origins, headers, and
	 * credentials for {@code GET}, {@code HEAD}, and {@code POST} requests with
	 * max age set to 1800 seconds (30 minutes) for the specified path.
	 * @param pathPattern the path that the CORS configuration should apply to;
	 * exact path mapping URIs (such as {@code "/admin"}) are supported as well
	 * as Ant-style path patterns (such as {@code "/admin/**"}).
	 */
	public CorsRegistration(String pathPattern) {
		this.pathPattern = pathPattern;
		// Same implicit default values as the @CrossOrigin annotation + allows simple methods
		this.config = new CorsConfiguration().applyPermitDefaultValues();
	}


	/**
	 * Set the origins to allow, e.g. {@code "https://domain1.com"}.
	 * 

The special value {@code "*"} allows all domains. *

By default, all origins are allowed. *

Note: CORS checks use values from "Forwarded" * (RFC 7239), * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers, * if present, in order to reflect the client-originated address. * Consider using the {@code ForwardedHeaderFilter} in order to choose from a * central place whether to extract and use, or to discard such headers. * See the Spring Framework reference for more on this filter. */ public CorsRegistration allowedOrigins(String... origins) { this.config.setAllowedOrigins(Arrays.asList(origins)); return this; } /** * Set the HTTP methods to allow, e.g. {@code "GET"}, {@code "POST"}, etc. *

The special value {@code "*"} allows all methods. *

By default "simple" methods {@code GET}, {@code HEAD}, and {@code POST} * are allowed. */ public CorsRegistration allowedMethods(String... methods) { this.config.setAllowedMethods(Arrays.asList(methods)); return this; } /** * Set the list of headers that a pre-flight request can list as allowed * for use during an actual request. *

The special value {@code "*"} may be used to allow all headers. *

A header name is not required to be listed if it is one of: * {@code Cache-Control}, {@code Content-Language}, {@code Expires}, * {@code Last-Modified}, or {@code Pragma} as per the CORS spec. *

By default all headers are allowed. */ public CorsRegistration allowedHeaders(String... headers) { this.config.setAllowedHeaders(Arrays.asList(headers)); return this; } /** * Set the list of response headers other than "simple" headers, i.e. * {@code Cache-Control}, {@code Content-Language}, {@code Content-Type}, * {@code Expires}, {@code Last-Modified}, or {@code Pragma}, that an * actual response might have and can be exposed. *

Note that {@code "*"} is not supported on this property. *

By default this is not set. */ public CorsRegistration exposedHeaders(String... headers) { this.config.setExposedHeaders(Arrays.asList(headers)); return this; } /** * Configure how long in seconds the response from a pre-flight request * can be cached by clients. *

By default this is set to 1800 seconds (30 minutes). */ public CorsRegistration maxAge(long maxAge) { this.config.setMaxAge(maxAge); return this; } /** * Whether user credentials are supported in which case the browser should * include any cookies associated with the domain of the request being * annotated. *

By default this is {@code false} and user credentials are not allowed. */ public CorsRegistration allowCredentials(boolean allowCredentials) { this.config.setAllowCredentials(allowCredentials); return this; } protected String getPathPattern() { return this.pathPattern; } protected CorsConfiguration getCorsConfiguration() { return this.config; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy