Support for placeholders in @CrossOrigin attributes

Issue: SPR-14010
This commit is contained in:
Juergen Hoeller
2016-03-11 15:04:16 +01:00
parent b4de66ff9a
commit 8852a5e178
2 changed files with 73 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -109,7 +109,7 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
this.embeddedValueResolver = resolver;
}
@Override
@@ -314,19 +314,19 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
return;
}
for (String origin : annotation.origins()) {
config.addAllowedOrigin(origin);
config.addAllowedOrigin(resolveCorsAnnotationValue(origin));
}
for (RequestMethod method : annotation.methods()) {
config.addAllowedMethod(method.name());
}
for (String header : annotation.allowedHeaders()) {
config.addAllowedHeader(header);
config.addAllowedHeader(resolveCorsAnnotationValue(header));
}
for (String header : annotation.exposedHeaders()) {
config.addExposedHeader(header);
config.addExposedHeader(resolveCorsAnnotationValue(header));
}
String allowCredentials = annotation.allowCredentials();
String allowCredentials = resolveCorsAnnotationValue(annotation.allowCredentials());
if ("true".equalsIgnoreCase(allowCredentials)) {
config.setAllowCredentials(true);
}
@@ -334,8 +334,8 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
config.setAllowCredentials(false);
}
else if (!allowCredentials.isEmpty()) {
throw new IllegalStateException("@CrossOrigin's allowCredentials value must be \"true\", \"false\", "
+ "or an empty string (\"\"); current value is [" + allowCredentials + "].");
throw new IllegalStateException("@CrossOrigin's allowCredentials value must be \"true\", \"false\", " +
"or an empty string (\"\"): current value is [" + allowCredentials + "]");
}
if (annotation.maxAge() >= 0 && config.getMaxAge() == null) {
@@ -343,4 +343,8 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
}
}
private String resolveCorsAnnotationValue(String value) {
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
}
}