|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.graphql; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.temporal.ChronoUnit; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 25 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 26 | +import org.springframework.boot.convert.DurationUnit; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.util.CollectionUtils; |
| 29 | +import org.springframework.web.cors.CorsConfiguration; |
| 30 | + |
| 31 | +/** |
| 32 | + * Configuration properties for GraphQL endpoint's CORS support. |
| 33 | + * |
| 34 | + * @author Andy Wilkinson |
| 35 | + * @author Brian Clozel |
| 36 | + * @since 2.7.0 |
| 37 | + */ |
| 38 | +@ConfigurationProperties(prefix = "spring.graphql.cors") |
| 39 | +public class GraphQlCorsProperties { |
| 40 | + |
| 41 | + /** |
| 42 | + * Comma-separated list of origins to allow with '*' allowing all origins. When |
| 43 | + * allow-credentials is enabled, '*' cannot be used, and setting origin patterns |
| 44 | + * should be considered instead. When neither allowed origins nor allowed origin |
| 45 | + * patterns are set, cross-origin requests are effectively disabled. |
| 46 | + */ |
| 47 | + private List<String> allowedOrigins = new ArrayList<>(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Comma-separated list of origin patterns to allow. Unlike allowed origins which only |
| 51 | + * support '*', origin patterns are more flexible, e.g. 'https://*.example.com', and |
| 52 | + * can be used with allow-credentials. When neither allowed origins nor allowed origin |
| 53 | + * patterns are set, cross-origin requests are effectively disabled. |
| 54 | + */ |
| 55 | + private List<String> allowedOriginPatterns = new ArrayList<>(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Comma-separated list of HTTP methods to allow. '*' allows all methods. When not |
| 59 | + * set, defaults to GET. |
| 60 | + */ |
| 61 | + private List<String> allowedMethods = new ArrayList<>(); |
| 62 | + |
| 63 | + /** |
| 64 | + * Comma-separated list of HTTP headers to allow in a request. '*' allows all headers. |
| 65 | + */ |
| 66 | + private List<String> allowedHeaders = new ArrayList<>(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Comma-separated list of headers to include in a response. |
| 70 | + */ |
| 71 | + private List<String> exposedHeaders = new ArrayList<>(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Whether credentials are supported. When not set, credentials are not supported. |
| 75 | + */ |
| 76 | + @Nullable |
| 77 | + private Boolean allowCredentials; |
| 78 | + |
| 79 | + /** |
| 80 | + * How long the response from a pre-flight request can be cached by clients. If a |
| 81 | + * duration suffix is not specified, seconds will be used. |
| 82 | + */ |
| 83 | + @DurationUnit(ChronoUnit.SECONDS) |
| 84 | + private Duration maxAge = Duration.ofSeconds(1800); |
| 85 | + |
| 86 | + public List<String> getAllowedOrigins() { |
| 87 | + return this.allowedOrigins; |
| 88 | + } |
| 89 | + |
| 90 | + public void setAllowedOrigins(List<String> allowedOrigins) { |
| 91 | + this.allowedOrigins = allowedOrigins; |
| 92 | + } |
| 93 | + |
| 94 | + public List<String> getAllowedOriginPatterns() { |
| 95 | + return this.allowedOriginPatterns; |
| 96 | + } |
| 97 | + |
| 98 | + public void setAllowedOriginPatterns(List<String> allowedOriginPatterns) { |
| 99 | + this.allowedOriginPatterns = allowedOriginPatterns; |
| 100 | + } |
| 101 | + |
| 102 | + public List<String> getAllowedMethods() { |
| 103 | + return this.allowedMethods; |
| 104 | + } |
| 105 | + |
| 106 | + public void setAllowedMethods(List<String> allowedMethods) { |
| 107 | + this.allowedMethods = allowedMethods; |
| 108 | + } |
| 109 | + |
| 110 | + public List<String> getAllowedHeaders() { |
| 111 | + return this.allowedHeaders; |
| 112 | + } |
| 113 | + |
| 114 | + public void setAllowedHeaders(List<String> allowedHeaders) { |
| 115 | + this.allowedHeaders = allowedHeaders; |
| 116 | + } |
| 117 | + |
| 118 | + public List<String> getExposedHeaders() { |
| 119 | + return this.exposedHeaders; |
| 120 | + } |
| 121 | + |
| 122 | + public void setExposedHeaders(List<String> exposedHeaders) { |
| 123 | + this.exposedHeaders = exposedHeaders; |
| 124 | + } |
| 125 | + |
| 126 | + @Nullable |
| 127 | + public Boolean getAllowCredentials() { |
| 128 | + return this.allowCredentials; |
| 129 | + } |
| 130 | + |
| 131 | + public void setAllowCredentials(Boolean allowCredentials) { |
| 132 | + this.allowCredentials = allowCredentials; |
| 133 | + } |
| 134 | + |
| 135 | + public Duration getMaxAge() { |
| 136 | + return this.maxAge; |
| 137 | + } |
| 138 | + |
| 139 | + public void setMaxAge(Duration maxAge) { |
| 140 | + this.maxAge = maxAge; |
| 141 | + } |
| 142 | + |
| 143 | + @Nullable |
| 144 | + public CorsConfiguration toCorsConfiguration() { |
| 145 | + if (CollectionUtils.isEmpty(this.allowedOrigins) && CollectionUtils.isEmpty(this.allowedOriginPatterns)) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + PropertyMapper map = PropertyMapper.get(); |
| 149 | + CorsConfiguration config = new CorsConfiguration(); |
| 150 | + map.from(this::getAllowedOrigins).to(config::setAllowedOrigins); |
| 151 | + map.from(this::getAllowedOriginPatterns).to(config::setAllowedOriginPatterns); |
| 152 | + map.from(this::getAllowedHeaders).whenNot(CollectionUtils::isEmpty).to(config::setAllowedHeaders); |
| 153 | + map.from(this::getAllowedMethods).whenNot(CollectionUtils::isEmpty).to(config::setAllowedMethods); |
| 154 | + map.from(this::getExposedHeaders).whenNot(CollectionUtils::isEmpty).to(config::setExposedHeaders); |
| 155 | + map.from(this::getMaxAge).whenNonNull().as(Duration::getSeconds).to(config::setMaxAge); |
| 156 | + map.from(this::getAllowCredentials).whenNonNull().to(config::setAllowCredentials); |
| 157 | + return config; |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments