Fix new Sonar smells

This commit is contained in:
Artem Bilan
2020-07-20 11:05:23 -04:00
parent 8d3d871996
commit 44a4523586
6 changed files with 87 additions and 78 deletions

View File

@@ -148,45 +148,50 @@ public class WebFluxIntegrationRequestMappingHandlerMapping extends RequestMappi
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
CrossOrigin crossOrigin = ((BaseHttpInboundEndpoint) handler).getCrossOrigin();
if (crossOrigin != null) {
CorsConfiguration config = new CorsConfiguration();
for (RequestMethod requestMethod : crossOrigin.getMethod()) {
config.addAllowedMethod(requestMethod.name());
}
config.setAllowedHeaders(Arrays.asList(crossOrigin.getAllowedHeaders()));
config.setExposedHeaders(Arrays.asList(crossOrigin.getExposedHeaders()));
Boolean allowCredentials = crossOrigin.getAllowCredentials();
config.setAllowCredentials(allowCredentials);
List<String> allowedOrigins = Arrays.asList(crossOrigin.getOrigin());
if (Boolean.TRUE.equals(allowCredentials)
&& CollectionUtils.contains(allowedOrigins.iterator(), CorsConfiguration.ALL)) {
config.setAllowedOriginPatterns(allowedOrigins);
}
else {
config.setAllowedOrigins(allowedOrigins);
}
if (crossOrigin.getMaxAge() != -1) {
config.setMaxAge(crossOrigin.getMaxAge());
}
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
config.addAllowedMethod(allowedMethod.name());
}
}
if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
for (NameValueExpression<String> headerExpression :
mappingInfo.getHeadersCondition().getExpressions()) {
if (!headerExpression.isNegated()) {
config.addAllowedHeader(headerExpression.getName());
}
}
}
return config.applyPermitDefaultValues();
return buildCorsConfiguration(crossOrigin, mappingInfo);
}
return null;
}
private static CorsConfiguration buildCorsConfiguration(CrossOrigin crossOrigin, RequestMappingInfo mappingInfo) {
CorsConfiguration config = new CorsConfiguration();
for (RequestMethod requestMethod : crossOrigin.getMethod()) {
config.addAllowedMethod(requestMethod.name());
}
config.setAllowedHeaders(Arrays.asList(crossOrigin.getAllowedHeaders()));
config.setExposedHeaders(Arrays.asList(crossOrigin.getExposedHeaders()));
Boolean allowCredentials = crossOrigin.getAllowCredentials();
config.setAllowCredentials(allowCredentials);
List<String> allowedOrigins = Arrays.asList(crossOrigin.getOrigin());
if (Boolean.TRUE.equals(allowCredentials)
&& CollectionUtils.contains(allowedOrigins.iterator(), CorsConfiguration.ALL)) {
config.setAllowedOriginPatterns(allowedOrigins);
}
else {
config.setAllowedOrigins(allowedOrigins);
}
if (crossOrigin.getMaxAge() != -1) {
config.setMaxAge(crossOrigin.getMaxAge());
}
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
config.addAllowedMethod(allowedMethod.name());
}
}
if (CollectionUtils.isEmpty(config.getAllowedHeaders())) {
for (NameValueExpression<String> headerExpression :
mappingInfo.getHeadersCondition().getExpressions()) {
if (!headerExpression.isNegated()) {
config.addAllowedHeader(headerExpression.getName());
}
}
}
return config.applyPermitDefaultValues();
}
/**
* {@link org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport}s
* may depend on auto-created {@code requestChannel}s, so MVC Handlers detection should be postponed