Skip to content

Commit 7aa4549

Browse files
committed
Fix: setup CORS for API
1 parent 4ed7923 commit 7aa4549

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

.github/workflows/master_bellchat.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
22
# More GitHub Actions for Azure: https://github.com/Azure/actions
3-
43
name: Build and deploy JAR app to Azure Web App - bellchat
54

65
on:
@@ -32,6 +31,7 @@ jobs:
3231
echo "AZURE_BLOBSTORAGE_CONTAINER_NAME=${{ secrets.AZURE_BLOBSTORAGE_CONTAINER_NAME }}" >> $GITHUB_ENV
3332
echo "MAINSITE_JWT_SECRET=${{ secrets.MAINSITE_JWT_SECRET }}" >> $GITHUB_ENV
3433
echo "MAINSITE_JWT_EXPIRATION=${{ secrets.MAINSITE_JWT_EXPIRATION }}" >> $GITHUB_ENV
34+
echo "APP_ALLOWED_ORIGINS=${{ secrets.APP_ALLOWED_ORIGINS }}" >> $GITHUB_ENV
3535
3636
- name: Build with Maven
3737
run: mvn clean install
@@ -50,13 +50,13 @@ jobs:
5050
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
5151
permissions:
5252
id-token: write #This is required for requesting the JWT
53-
53+
5454
steps:
5555
- name: Download artifact from build job
5656
uses: actions/download-artifact@v3
5757
with:
5858
name: java-app
59-
59+
6060
- name: Login to Azure
6161
uses: azure/login@v1
6262
with:
@@ -75,6 +75,7 @@ jobs:
7575
echo "AZURE_BLOBSTORAGE_CONTAINER_NAME=${{ secrets.AZURE_BLOBSTORAGE_CONTAINER_NAME }}" >> $GITHUB_ENV
7676
echo "MAINSITE_JWT_SECRET=${{ secrets.MAINSITE_JWT_SECRET }}" >> $GITHUB_ENV
7777
echo "MAINSITE_JWT_EXPIRATION=${{ secrets.MAINSITE_JWT_EXPIRATION }}" >> $GITHUB_ENV
78+
echo "APP_ALLOWED_ORIGINS=${{ secrets.APP_ALLOWED_ORIGINS }}" >> $GITHUB_ENV
7879
7980
- name: Deploy to Azure Web App
8081
id: deploy-to-webapp
@@ -83,4 +84,3 @@ jobs:
8384
app-name: 'bellchat'
8485
slot-name: 'Production'
8586
package: '*.jar'
86-

src/main/java/com/vietle/mychatapi/MychatapiApplication.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vietle.mychatapi;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56
import org.springframework.context.annotation.Bean;
@@ -8,6 +9,8 @@
89

910
@SpringBootApplication
1011
public class MychatapiApplication {
12+
@Value("${app.allowed.origins}")
13+
private String[] allowedOrigins;
1114

1215
public static void main(String[] args) {
1316
SpringApplication.run(MychatapiApplication.class, args);
@@ -19,7 +22,7 @@ public WebMvcConfigurer corsConfigurer() {
1922
@Override
2023
public void addCorsMappings(CorsRegistry registry) {
2124
registry.addMapping("/**")
22-
.allowedOrigins("*")
25+
.allowedOrigins(allowedOrigins)
2326
.allowedHeaders("*")
2427
.allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS");
2528
}

src/main/java/com/vietle/mychatapi/config/WebSocketConfig.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.vietle.mychatapi.config;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.context.annotation.Configuration;
45
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
56
import org.springframework.web.socket.WebSocketHandler;
@@ -14,9 +15,12 @@
1415
@Configuration
1516
@EnableWebSocketMessageBroker
1617
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
18+
@Value("${app.allowed.origins}")
19+
private String[] allowedOrigins;
20+
1721
@Override
1822
public void registerStompEndpoints(StompEndpointRegistry registry) {
19-
registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS();
23+
registry.addEndpoint("/ws").setAllowedOriginPatterns(allowedOrigins).withSockJS();
2024
}
2125

2226
@Override

src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring.application.name=mychatapi
22
server.port=8080
3-
3+
app.allowed.origins=${APP_ALLOWED_ORIGINS}
44
spring.jackson.property-naming-strategy=SNAKE_CASE
55

66
### Database connection

0 commit comments

Comments
 (0)