-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict automatic CORS configuration to UrlBasedCorsConfigurationSource #15444
Restrict automatic CORS configuration to UrlBasedCorsConfigurationSource #15444
Conversation
e6995e4
to
0f3c2c2
Compare
Hi @baezzys, thanks for the PR. Can you please target the |
Map<String, CorsConfigurationSource> corsConfigurationSources = this.context | ||
.getBeansOfType(CorsConfigurationSource.class); | ||
|
||
boolean hasUrlBasedCorsConfigurationSource = corsConfigurationSources.values() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you please adopt another strategy instead of using Stream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have refactored the code to avoid using Streams. PTAL. Thanks.
0f3c2c2
to
cdda121
Compare
Hi @marcusdacoregio, I have completed the rebase and retargeted the PR to the 6.2.x branch. |
66e4c96
to
1d41596
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @baezzys, I've left some feedback inline.
|
||
for (CorsConfigurationSource source : corsConfigurationSources.values()) { | ||
if (source instanceof UrlBasedCorsConfigurationSource) { | ||
http.cors(withDefaults()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should check if the instance is UrlBasedCorsConfigurationSource
and if the bean name is corsConfigurationSource
, since this is the bean name used by the CorsConfigurer
, to avoid picking up the wrong CorsConfigurationSource
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the code to check if there are any beans of type UrlBasedCorsConfigurationSource using getBeanNamesForType. If such beans exist, CORS configuration is applied.
If this is not what you intended, please feel free to provide further feedback.
|
||
this.mockMvc.perform(formLogin()).andExpect(header().doesNotExist("Access-Control-Allow-Origin")); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a test that verifies if the header Vary
is not present? In summary, simulate the problem reported in #15378 and assert that it is fixed.
You can add the issue number in the test, like so:
// gh-15378
@Test
void ...() {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the existing test code to verify if the Vary header is not present. Thank you for your feedback!
…onSource - Update CORS configuration logic to automatically enable .cors() only if a UrlBasedCorsConfigurationSource bean is present. - Modify applyCorsIfAvailable method to check for UrlBasedCorsConfigurationSource instances.
1d41596
to
62935fb
Compare
Thanks @baezzys, this is now merged into |
Closes gh-15378