|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
37 | 37 | public class CorsEndpointProperties {
|
38 | 38 |
|
39 | 39 | /**
|
40 |
| - * Comma-separated list of origins to allow. '*' allows all origins. When not set, |
41 |
| - * CORS support is disabled. |
| 40 | + * Comma-separated list of origins to allow. '*' allows all origins. When credentials |
| 41 | + * are allowed, '*' cannot be used and origin patterns should be configured instead. |
| 42 | + * When no allowed origins or allowed origin patterns are set, CORS support is |
| 43 | + * disabled. |
42 | 44 | */
|
43 | 45 | private List<String> allowedOrigins = new ArrayList<>();
|
44 | 46 |
|
| 47 | + /** |
| 48 | + * Comma-separated list of origin patterns to allow. Unlike allowed origins which only |
| 49 | + * supports '*', origin patterns are more flexible (for example |
| 50 | + * 'https://*.example.com') and can be used when credentials are allowed. When no |
| 51 | + * allowed origin patterns or allowed origins are set, CORS support is disabled. |
| 52 | + */ |
| 53 | + private List<String> allowedOriginPatterns = new ArrayList<>(); |
| 54 | + |
45 | 55 | /**
|
46 | 56 | * Comma-separated list of methods to allow. '*' allows all methods. When not set,
|
47 | 57 | * defaults to GET.
|
@@ -78,6 +88,14 @@ public void setAllowedOrigins(List<String> allowedOrigins) {
|
78 | 88 | this.allowedOrigins = allowedOrigins;
|
79 | 89 | }
|
80 | 90 |
|
| 91 | + public List<String> getAllowedOriginPatterns() { |
| 92 | + return this.allowedOriginPatterns; |
| 93 | + } |
| 94 | + |
| 95 | + public void setAllowedOriginPatterns(List<String> allowedOriginPatterns) { |
| 96 | + this.allowedOriginPatterns = allowedOriginPatterns; |
| 97 | + } |
| 98 | + |
81 | 99 | public List<String> getAllowedMethods() {
|
82 | 100 | return this.allowedMethods;
|
83 | 101 | }
|
@@ -119,12 +137,13 @@ public void setMaxAge(Duration maxAge) {
|
119 | 137 | }
|
120 | 138 |
|
121 | 139 | public CorsConfiguration toCorsConfiguration() {
|
122 |
| - if (CollectionUtils.isEmpty(this.allowedOrigins)) { |
| 140 | + if (CollectionUtils.isEmpty(this.allowedOrigins) && CollectionUtils.isEmpty(this.allowedOriginPatterns)) { |
123 | 141 | return null;
|
124 | 142 | }
|
125 | 143 | PropertyMapper map = PropertyMapper.get();
|
126 | 144 | CorsConfiguration configuration = new CorsConfiguration();
|
127 | 145 | map.from(this::getAllowedOrigins).to(configuration::setAllowedOrigins);
|
| 146 | + map.from(this::getAllowedOriginPatterns).to(configuration::setAllowedOriginPatterns); |
128 | 147 | map.from(this::getAllowedHeaders).whenNot(CollectionUtils::isEmpty).to(configuration::setAllowedHeaders);
|
129 | 148 | map.from(this::getAllowedMethods).whenNot(CollectionUtils::isEmpty).to(configuration::setAllowedMethods);
|
130 | 149 | map.from(this::getExposedHeaders).whenNot(CollectionUtils::isEmpty).to(configuration::setExposedHeaders);
|
|
0 commit comments