Skip to content

Commit d99bdfe

Browse files
fix spring security cors handling
1 parent d73b317 commit d99bdfe

5 files changed

Lines changed: 150 additions & 77 deletions

File tree

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,51 @@ Will produce the following component images:
1010
- Submodel Registry: `basyx.aasdiscoveryservice/basyx.aasdiscoveryservice.component/target/basyx.aasdiscoveryservice.component-VERSION.jar`
1111
- AAS Discovery: `basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem/target/basyx.submodelregistry-service-release-log-mem-VERSION.jar`
1212

13-
# Building the docker images
13+
# Building and pushing the docker images
1414
## AAS Environment
1515
```shell
1616
PROJECT_ROOT=~/workspace/twinmap/letsdev-basyx-java-server-sdk
17-
AAS_ENVIRONMENT=0.3.5
17+
AAS_ENVIRONMENT=0.3.8
1818
cd ${PROJECT_ROOT}/basyx.aasenvironment/basyx.aasenvironment.component
19-
docker build -t docker-twinmap.letsdev.de/aas-env-oauth:${AAS_ENVIRONMENT} .
19+
docker build --platform linux/amd64 -t docker-twinmap.letsdev.de/aas-env-oauth:${AAS_ENVIRONMENT} .
2020
docker tag docker-twinmap.letsdev.de/aas-env-oauth:${AAS_ENVIRONMENT} docker-twinmap.letsdev.de/aas-env-oauth:${AAS_ENVIRONMENT}
2121
docker push docker-twinmap.letsdev.de/aas-env-oauth:${AAS_ENVIRONMENT}
2222
```
23+
24+
## AAS Discovery
25+
```shell
26+
PROJECT_ROOT=~/workspace/twinmap/letsdev-basyx-java-server-sdk
27+
AAS_DISCOVERY_VERSION=0.3.9
28+
cd ${PROJECT_ROOT}/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice.component
29+
docker build --platform linux/amd64 -t docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION} .
30+
docker tag docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION} docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION}
31+
docker push docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION}
32+
```
33+
2334
## AAS Registry
2435
```shell
2536
PROJECT_ROOT=~/workspace/twinmap/letsdev-basyx-java-server-sdk
26-
AAS_REGISTRY=0.3.10
27-
cd ${PROJECT_ROOT}/basyx.aasregistry/basyx.aasregistry-service-release-log-mem
28-
docker build -t docker-twinmap.letsdev.de/aas-registry-log-mem-oauth:${AAS_REGISTRY} .
37+
AAS_REGISTRY=0.3.13
38+
FINAL_NAME=basyx.aasregistry-service-release-log-mem-2.0.1-SNAPSHOT
39+
cp ${PROJECT_ROOT}/basyx.aasregistry/basyx.aasregistry-service-release-log-mem/target/${FINAL_NAME}.jar ${PROJECT_ROOT}/basyx.aasregistry/basyx.aasregistry-service-release-log-mem/src/main/docker/${FINAL_NAME}.jar
40+
cd ${PROJECT_ROOT}/basyx.aasregistry/basyx.aasregistry-service-release-log-mem/src/main/docker
41+
docker build --platform linux/amd64 -t docker-twinmap.letsdev.de/aas-registry-log-mem-oauth:${AAS_REGISTRY} --build-arg FINAL_NAME=${FINAL_NAME} .
2942
docker tag docker-twinmap.letsdev.de/aas-registry-log-mem-oauth:${AAS_REGISTRY} docker-twinmap.letsdev.de/aas-registry-log-mem-oauth:${AAS_REGISTRY}
3043
docker push docker-twinmap.letsdev.de/aas-registry-log-mem-oauth:${AAS_REGISTRY}
44+
rm ${PROJECT_ROOT}/basyx.aasregistry/basyx.aasregistry-service-release-log-mem/src/main/docker/${FINAL_NAME}.jar
3145
```
46+
3247
## Submodel Registry
3348
```shell
3449
PROJECT_ROOT=~/workspace/twinmap/letsdev-basyx-java-server-sdk
35-
SM_REGISTRY_VERSION=0.3.10
36-
cd ${PROJECT_ROOT}/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice.component
37-
docker build -t docker-twinmap.letsdev.de/aas-submodel-registry-log-mem-oauth:${SM_REGISTRY_VERSION} .
50+
SM_REGISTRY_VERSION=0.3.12
51+
FINAL_NAME=basyx.submodelregistry-service-release-log-mem-2.0.1-SNAPSHOT
52+
cp ${PROJECT_ROOT}/basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem/target/${FINAL_NAME}.jar ${PROJECT_ROOT}/basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem/src/main/docker/${FINAL_NAME}.jar
53+
cd ${PROJECT_ROOT}/basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem/src/main/docker
54+
docker build --platform linux/amd64 -t docker-twinmap.letsdev.de/aas-submodel-registry-log-mem-oauth:${SM_REGISTRY_VERSION} .
3855
docker tag docker-twinmap.letsdev.de/aas-submodel-registry-log-mem-oauth:${SM_REGISTRY_VERSION} docker-twinmap.letsdev.de/aas-submodel-registry-log-mem-oauth:${SM_REGISTRY_VERSION}
3956
docker push docker-twinmap.letsdev.de/aas-submodel-registry-log-mem-oauth:${SM_REGISTRY_VERSION}
40-
```
41-
## AAS Discovery
42-
```shell
43-
PROJECT_ROOT=~/workspace/twinmap/letsdev-basyx-java-server-sdk
44-
AAS_DISCOVERY_VERSION=0.3.3
45-
cd ${PROJECT_ROOT}/basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem
46-
docker build -t docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION} .
47-
docker tag docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION} docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION}
48-
docker push docker-twinmap.letsdev.de/aas-discovery-oauth:${AAS_DISCOVERY_VERSION}
57+
rm ${PROJECT_ROOT}/basyx.submodelregistry/basyx.submodelregistry-service-release-log-mem/src/main/docker/${FINAL_NAME}.jar
4958
```
5059

5160

basyx.common/basyx.authorization/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
<groupId>org.springframework.boot</groupId>
2929
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
3030
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-web</artifactId>
34+
</dependency>
3135
<dependency>
3236
<groupId>org.eclipse.digitaltwin.basyx</groupId>
3337
<artifactId>basyx.http</artifactId>
@@ -69,4 +73,4 @@
6973
</dependency>
7074

7175
</dependencies>
72-
</project>
76+
</project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.eclipse.digitaltwin.basyx.authorization.letsdev;
2+
3+
import org.eclipse.digitaltwin.basyx.http.CorsPathPatternProvider;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.cors.CorsConfiguration;
8+
import org.springframework.web.cors.CorsConfigurationSource;
9+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
@Configuration
18+
public class CorsConfig {
19+
20+
private static final Logger logger = LoggerFactory.getLogger(CorsConfig.class);
21+
22+
@Bean
23+
public CorsConfigurationSource corsConfigurationSource(
24+
List<CorsPathPatternProvider> configurationUrlProviders,
25+
@Value("${basyx.cors.allowed-origins:}") String[] allowedOrigins,
26+
@Value("${basyx.cors.allowed-methods:}") String[] allowedMethods,
27+
@Value("${basyx.cors.allowed-headers:}") String[] allowedHeaders) {
28+
29+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
30+
31+
if (allowedOrigins.length == 0 && allowedMethods.length == 0) {
32+
logger.info("No CORS configuration applied (empty origins/methods).");
33+
return source; // return empty config so app still starts
34+
}
35+
36+
logger.info("---- Configuring CORS ----");
37+
for (CorsPathPatternProvider provider : configurationUrlProviders) {
38+
String pathPattern = provider.getPathPattern();
39+
40+
logger.info("{} configured with allowedOriginPatterns {}", pathPattern, Arrays.toString(allowedOrigins));
41+
logger.info(
42+
allowedMethods.length == 0
43+
? "No allowed methods configured"
44+
: "{} configured with allowedMethods {}",
45+
pathPattern, Arrays.toString(allowedMethods));
46+
47+
CorsConfiguration cors = new CorsConfiguration();
48+
cors.setAllowedOriginPatterns(Arrays.asList(allowedOrigins));
49+
cors.setAllowedMethods(Arrays.asList(allowedMethods));
50+
cors.setAllowedHeaders(Arrays.asList(allowedHeaders));
51+
cors.setAllowCredentials(true);
52+
cors.setExposedHeaders(List.of());
53+
54+
source.registerCorsConfiguration(pathPattern, cors);
55+
}
56+
57+
return source;
58+
}
59+
}

basyx.common/basyx.authorization/src/main/java/org/eclipse/digitaltwin/basyx/authorization/letsdev/WebSecurityConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.http.HttpMethod;
6+
import org.springframework.security.config.Customizer;
67
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
78
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
89
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
@@ -31,14 +32,14 @@ public class WebSecurityConfig {
3132
private final LdSsoConfigProperties ldSsoConfigProperties;
3233

3334
public WebSecurityConfig(LdSsoConfigProperties ldSsoConfigProperties) {
34-
3535
this.ldSsoConfigProperties = ldSsoConfigProperties;
3636
}
3737

3838
@Bean
3939
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
4040

4141
http
42+
.cors(Customizer.withDefaults())
4243
.oauth2ResourceServer(oauth2ResourceServerCustomizer ->
4344
oauth2ResourceServerCustomizer.jwt(jwtCustomizer -> {
4445
jwtCustomizer.jwkSetUri(ldSsoConfigProperties.getBaseUrl() + JWKS_PATH);
Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/*******************************************************************************
22
* Copyright (C) 2023 the Eclipse BaSyx Authors
3-
*
3+
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
66
* "Software"), to deal in the Software without restriction, including
77
* without limitation the rights to use, copy, modify, merge, publish,
88
* distribute, sublicense, and/or sell copies of the Software, and to
99
* permit persons to whom the Software is furnished to do so, subject to
1010
* the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be
1313
* included in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1616
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1717
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1818
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1919
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2020
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2121
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-
*
22+
*
2323
* SPDX-License-Identifier: MIT
2424
******************************************************************************/
2525

@@ -43,7 +43,7 @@
4343
/**
4444
* Configuration class providing all relevant beans for HTTP payload
4545
* (de-)serialization
46-
*
46+
*
4747
* @author schnicke
4848
*
4949
*/
@@ -54,7 +54,7 @@ public class BaSyxHTTPConfiguration {
5454
/**
5555
* Returns a Jackson2ObjectMapperBuilder that is configured using the passed
5656
* list of {@link SerializationExtension}
57-
*
57+
*
5858
* @param serializationExtensions
5959
* @return
6060
*/
@@ -65,58 +65,58 @@ public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(List<Serializatio
6565
for (SerializationExtension serializationExtension : serializationExtensions) {
6666
serializationExtension.extend(builder);
6767
}
68-
68+
6969
return builder;
7070
}
7171

72-
/**
73-
* Collects a list of {@link CorsPathPatternProvider} and uses them to configure
74-
* CORS for the passed pathPatterns
75-
*
76-
* @param configurationUrlProviders
77-
* @param allowedOrigins
78-
* @param allowedMethods
79-
* @return
80-
*/
81-
@Bean
82-
public WebMvcConfigurer corsConfigurer(List<CorsPathPatternProvider> configurationUrlProviders,
83-
@Value("${basyx.cors.allowed-origins:}") String[] allowedOrigins,
84-
@Value("${basyx.cors.allowed-methods:}") String[] allowedMethods,
85-
@Value("${basyx.cors.allowed-headers:}") String[] allowedHeaders) {
86-
return new WebMvcConfigurer() {
87-
@Override
88-
public void addCorsMappings(CorsRegistry registry) {
89-
if (allowedOrigins.length == 0 && allowedMethods.length == 0)
90-
return;
91-
92-
logger.info("---- Configuring CORS ----");
93-
94-
for (CorsPathPatternProvider provider : configurationUrlProviders) {
95-
configureOrigins(
96-
allowedOrigins,
97-
allowedMethods,
98-
allowedHeaders,
99-
registry,
100-
provider.getPathPattern()
101-
);
102-
}
103-
}
104-
105-
private void configureOrigins(
106-
String[] allowedOrigins,
107-
String[] allowedMethods,
108-
String[] allowedHeaders,
109-
CorsRegistry registry,
110-
String pathPattern
111-
) {
112-
logger.info(pathPattern + " configured with allowedOriginPatterns " + Arrays.toString(allowedOrigins));
113-
logger.info(allowedMethods.length == 0 ? "No allowed methods configured" : pathPattern + " configured with allowedMethods " + Arrays.toString(allowedMethods));
114-
115-
registry.addMapping(pathPattern)
116-
.allowedOriginPatterns(allowedOrigins)
117-
.allowedMethods(allowedMethods)
118-
.allowedHeaders(allowedHeaders);
119-
}
120-
};
121-
}
72+
// /**
73+
// * Collects a list of {@link CorsPathPatternProvider} and uses them to configure
74+
// * CORS for the passed pathPatterns
75+
// *
76+
// * @param configurationUrlProviders
77+
// * @param allowedOrigins
78+
// * @param allowedMethods
79+
// * @return
80+
// */
81+
// @Bean
82+
// public WebMvcConfigurer corsConfigurer(List<CorsPathPatternProvider> configurationUrlProviders,
83+
// @Value("${basyx.cors.allowed-origins:}") String[] allowedOrigins,
84+
// @Value("${basyx.cors.allowed-methods:}") String[] allowedMethods,
85+
// @Value("${basyx.cors.allowed-headers:}") String[] allowedHeaders) {
86+
// return new WebMvcConfigurer() {
87+
// @Override
88+
// public void addCorsMappings(CorsRegistry registry) {
89+
// if (allowedOrigins.length == 0 && allowedMethods.length == 0)
90+
// return;
91+
//
92+
// logger.info("---- Configuring CORS ----");
93+
//
94+
// for (CorsPathPatternProvider provider : configurationUrlProviders) {
95+
// configureOrigins(
96+
// allowedOrigins,
97+
// allowedMethods,
98+
// allowedHeaders,
99+
// registry,
100+
// provider.getPathPattern()
101+
// );
102+
// }
103+
// }
104+
//
105+
// private void configureOrigins(
106+
// String[] allowedOrigins,
107+
// String[] allowedMethods,
108+
// String[] allowedHeaders,
109+
// CorsRegistry registry,
110+
// String pathPattern
111+
// ) {
112+
// logger.info(pathPattern + " configured with allowedOriginPatterns " + Arrays.toString(allowedOrigins));
113+
// logger.info(allowedMethods.length == 0 ? "No allowed methods configured" : pathPattern + " configured with allowedMethods " + Arrays.toString(allowedMethods));
114+
//
115+
// registry.addMapping(pathPattern)
116+
// .allowedOriginPatterns(allowedOrigins)
117+
// .allowedMethods(allowedMethods)
118+
// .allowedHeaders(allowedHeaders);
119+
// }
120+
// };
121+
// }
122122
}

0 commit comments

Comments
 (0)