Skip to content

Commit a7e4482

Browse files
committed
Enables CORS for spring security
1 parent 53bf9b5 commit a7e4482

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

basyx.common/basyx.authorization/src/main/java/org/eclipse/digitaltwin/basyx/authorization/CommonSecurityConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
3434
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
3535
import org.springframework.security.web.SecurityFilterChain;
36+
import org.springframework.web.cors.CorsConfigurationSource;
3637

3738
/**
3839
* Common configurations for security
@@ -43,9 +44,16 @@
4344
@ConditionalOnExpression("#{${" + CommonAuthorizationProperties.ENABLED_PROPERTY_KEY + ":false}}")
4445
public class CommonSecurityConfiguration {
4546

47+
private final CorsConfigurationSource corsConfigurationSource;
48+
49+
public CommonSecurityConfiguration(CorsConfigurationSource corsConfigurationSource) {
50+
this.corsConfigurationSource = corsConfigurationSource;
51+
}
52+
4653
@Bean
4754
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4855
http
56+
.cors(cors -> cors.configurationSource(corsConfigurationSource))
4957
.authorizeHttpRequests(authorize -> authorize
5058
.requestMatchers("/actuator/health/**").permitAll()
5159
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()

basyx.common/basyx.http/src/main/java/org/eclipse/digitaltwin/basyx/http/BaSyxHTTPConfiguration.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import org.springframework.context.annotation.Bean;
3636
import org.springframework.context.annotation.Configuration;
3737
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
38+
import org.springframework.web.cors.CorsConfiguration;
39+
import org.springframework.web.cors.CorsConfigurationSource;
40+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
3841
import org.springframework.web.servlet.config.annotation.CorsRegistry;
3942
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
4043

@@ -103,4 +106,45 @@ private void configureOrigins(String[] allowedOrigins, String[] allowedMethods,
103106
}
104107
};
105108
}
109+
110+
/**
111+
* Creates a {@link CorsConfigurationSource} that can be used by Spring Security
112+
* to apply CORS headers even on error responses (e.g., 401, 403).
113+
* This ensures consistent CORS behavior between Spring MVC and Spring Security.
114+
*
115+
* @param configurationUrlProviders
116+
* @param allowedOrigins
117+
* @param allowedMethods
118+
* @return
119+
*/
120+
@Bean
121+
public CorsConfigurationSource corsConfigurationSource(List<CorsPathPatternProvider> configurationUrlProviders,
122+
@Value("${basyx.cors.allowed-origins:}") String[] allowedOrigins,
123+
@Value("${basyx.cors.allowed-methods:}") String[] allowedMethods) {
124+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
125+
126+
if (allowedOrigins.length == 0 && allowedMethods.length == 0) {
127+
return source;
128+
}
129+
130+
CorsConfiguration configuration = new CorsConfiguration();
131+
132+
if (allowedOrigins.length > 0) {
133+
configuration.setAllowedOriginPatterns(Arrays.asList(allowedOrigins));
134+
}
135+
136+
if (allowedMethods.length > 0) {
137+
configuration.setAllowedMethods(Arrays.asList(allowedMethods));
138+
}
139+
140+
configuration.setAllowedHeaders(List.of("*"));
141+
configuration.setAllowCredentials(true);
142+
143+
for (CorsPathPatternProvider provider : configurationUrlProviders) {
144+
logger.info("Registering CORS configuration for path pattern: " + provider.getPathPattern());
145+
source.registerCorsConfiguration(provider.getPathPattern(), configuration);
146+
}
147+
148+
return source;
149+
}
106150
}

0 commit comments

Comments
 (0)