Skip to content

Commit 6ab7b18

Browse files
authored
CORS Configuration for ECD API Services (#100)
* implemented cors all api * fix:added cors error api fix * fix:vary added * fix: ci properties changed
1 parent 4cfa5d7 commit 6ab7b18

25 files changed

Lines changed: 127 additions & 90 deletions

src/main/environment/ecd_ci.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ logging.file.name=@env.ECD_API_LOGGING_FILE_NAME@
2929
springdoc.api-docs.enabled=@env.SWAGGER_DOC_ENABLED@
3030
springdoc.swagger-ui.enabled=@env.SWAGGER_DOC_ENABLED@
3131

32-
spring.redis.host=@env.REDIS_HOST@
32+
spring.redis.host=@env.REDIS_HOST@
33+
34+
cors.allowed-origins=@env.CORS_ALLOWED_ORIGINS@
35+

src/main/environment/ecd_example.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ springdoc.swagger-ui.enabled=true
2323
jwt.secret=my-32-character-ultra-secure-and-ultra-long-secret
2424
#If both properties are set, only logging.file.name takes effect.
2525
logging.path=logs/
26-
logging.file.name=logs/ecd-api.log
26+
logging.file.name=logs/ecd-api.log
27+
28+
cors.allowed-origins=http://localhost:*

src/main/java/com/iemr/ecd/config/WebConfiguration.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,33 @@
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.web.servlet.config.annotation.CorsRegistry;
2626
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
27+
import java.util.Arrays;
28+
import org.springframework.beans.factory.annotation.Value;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2731

2832
@Configuration
2933
public class WebConfiguration implements WebMvcConfigurer {
3034

35+
private static final Logger logger = LoggerFactory.getLogger(WebConfiguration.class);
36+
37+
@Value("${cors.allowed-origins}")
38+
private String allowedOrigins;
39+
3140
@Override
3241
public void addCorsMappings(CorsRegistry registry) {
33-
registry.addMapping("/**").allowedMethods("*");
34-
}
42+
String[] originPatterns = Arrays.stream(allowedOrigins.split(","))
43+
.map(String::trim)
44+
.toArray(String[]::new);
3545

46+
logger.info("Initializing CORS configuration with allowed origins: {}", Arrays.toString(originPatterns));
47+
48+
registry.addMapping("/**")
49+
.allowedOriginPatterns(originPatterns)
50+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
51+
.allowedHeaders("*")
52+
.exposedHeaders("Authorization", "Jwttoken")
53+
.allowCredentials(true)
54+
.maxAge(3600);
55+
}
3656
}

src/main/java/com/iemr/ecd/controller/associate/AutoPreviewDialingController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.http.HttpStatus;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.CrossOrigin;
30+
3131
import org.springframework.web.bind.annotation.GetMapping;
3232
import org.springframework.web.bind.annotation.PathVariable;
3333
import org.springframework.web.bind.annotation.PostMapping;
@@ -47,7 +47,6 @@
4747

4848
@RestController
4949
@RequestMapping(value = "/autoPreviewDialing", headers = "Authorization")
50-
@CrossOrigin()
5150
public class AutoPreviewDialingController {
5251

5352
@Autowired

src/main/java/com/iemr/ecd/controller/associate/BeneficiaryCallHistoryController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.ResponseEntity;
28-
import org.springframework.web.bind.annotation.CrossOrigin;
28+
2929
import org.springframework.web.bind.annotation.GetMapping;
3030
import org.springframework.web.bind.annotation.PathVariable;
3131
import org.springframework.web.bind.annotation.PostMapping;
@@ -46,7 +46,6 @@
4646

4747
@RestController
4848
@RequestMapping(value = "/callHistory", headers = "Authorization")
49-
@CrossOrigin()
5049
public class BeneficiaryCallHistoryController {
5150
@Autowired
5251
private BeneficiaryCallHistoryImpl beneficiaryCallHistoryImpl;

src/main/java/com/iemr/ecd/controller/associate/BeneficiaryRegistrationController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.ResponseEntity;
28-
import org.springframework.web.bind.annotation.CrossOrigin;
28+
2929
import org.springframework.web.bind.annotation.PostMapping;
3030
import org.springframework.web.bind.annotation.RequestBody;
3131
import org.springframework.web.bind.annotation.RequestHeader;
@@ -43,14 +43,12 @@
4343

4444
@RestController
4545
@RequestMapping(value = "/beneficary", headers = "Authorization")
46-
@CrossOrigin()
4746

4847
public class BeneficiaryRegistrationController {
4948

5049
@Autowired
5150
private BeneficiaryRegistrationServiceImpl beneficiaryRegistrationServiceImpl;
5251

53-
@CrossOrigin()
5452
@PostMapping(value = "/registration", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
5553
@Operation(summary = "Create beneficiary registration", description = "Desc - Create Beneficiary registration")
5654
@ApiResponses(value = {
@@ -67,7 +65,6 @@ public ResponseEntity<Object> beneficiaryRegistration(@RequestBody RequestBenefi
6765
HttpStatus.OK);
6866
}
6967

70-
@CrossOrigin()
7168
@PostMapping(value = "/updateBeneficiaryDetails", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
7269
@Operation(summary = "Update beneficiary details", description = "Desc - Update Beneficiary details")
7370
@ApiResponses(value = {

src/main/java/com/iemr/ecd/controller/associate/CallClosureController.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.ResponseEntity;
28-
import org.springframework.web.bind.annotation.CrossOrigin;
28+
2929
import org.springframework.web.bind.annotation.PostMapping;
3030
import org.springframework.web.bind.annotation.RequestBody;
3131
import org.springframework.web.bind.annotation.RequestMapping;
@@ -42,12 +42,10 @@
4242

4343
@RestController
4444
@RequestMapping(value = "/closure", headers = "Authorization")
45-
@CrossOrigin()
4645
public class CallClosureController {
4746
@Autowired
4847
private CallClosureImpl callClosureImpl;
4948

50-
@CrossOrigin()
5149
@PostMapping(value = "/closeCall", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
5250
@Operation(summary = "Call closure", description = "Desc - Call closure")
5351
@ApiResponses(value = {

src/main/java/com/iemr/ecd/controller/callallocation/CallAllocationController.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.http.HttpStatus;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.CrossOrigin;
30+
3131
import org.springframework.web.bind.annotation.GetMapping;
3232
import org.springframework.web.bind.annotation.PathVariable;
3333
import org.springframework.web.bind.annotation.PostMapping;
@@ -50,7 +50,6 @@
5050

5151
@RestController
5252
@RequestMapping(value = "/callAllocation", headers = "Authorization")
53-
@CrossOrigin()
5453
public class CallAllocationController {
5554

5655
@Autowired
@@ -127,7 +126,7 @@ public ResponseEntity<Object> reAllocateCalls(@RequestBody RequestCallAllocation
127126
public ResponseEntity<String> updateAlerts(@RequestBody RequestCallAllocationDTO callAllocationDto) {
128127
return new ResponseEntity<>(callAllocationImpl.moveAllocatedCallsToBin(callAllocationDto), HttpStatus.OK);
129128
}
130-
129+
131130
@PostMapping(value = "/insertRecordsInOutboundCalls", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
132131
@Operation(summary = "Insert Records In OutboundCalls", description = "Desc - Insert Records In OutboundCalls")
133132
@ApiResponses(value = {
@@ -141,7 +140,7 @@ public ResponseEntity<String> insertRecordsInOutboundCalls(@RequestBody Outbound
141140
return new ResponseEntity<>(callAllocationImpl.insertRecordsInOutboundCalls(outboundCallsDTO), HttpStatus.OK);
142141

143142
}
144-
143+
145144
@GetMapping(value = "/getEligibleRecordsLanguageInfo/{psmId}/{phoneNoType}/{recordType}/{fDate}/{tDate}/{preferredLanguage}/{role}", produces = MediaType.APPLICATION_JSON_VALUE)
146145
@Operation(summary = "Fetch eligible Language records for allocation", description = "Desc - Fetch eligible records for allocation")
147146
@ApiResponses(value = {
@@ -153,9 +152,12 @@ public ResponseEntity<String> insertRecordsInOutboundCalls(@RequestBody Outbound
153152
@ApiResponse(responseCode = CustomExceptionResponse.BAD_REQUEST_SC_V, description = CustomExceptionResponse.BAD_REQUEST_SC) })
154153
public ResponseEntity<ResponseEligibleCallRecordsDTO> getEligibleRecordsLanguageInfo(@PathVariable int psmId,
155154
@PathVariable String phoneNoType, @PathVariable String recordType, @PathVariable String fDate,
156-
@PathVariable String tDate, @PathVariable String preferredLanguage,@PathVariable String role) throws ECDException {
155+
@PathVariable String tDate, @PathVariable String preferredLanguage, @PathVariable String role)
156+
throws ECDException {
157157
return new ResponseEntity<>(
158-
callAllocationImpl.getEligibleRecordsLanguageInfo(psmId, phoneNoType, recordType, fDate, tDate, preferredLanguage,role), HttpStatus.OK);
158+
callAllocationImpl.getEligibleRecordsLanguageInfo(psmId, phoneNoType, recordType, fDate, tDate,
159+
preferredLanguage, role),
160+
HttpStatus.OK);
159161

160162
}
161163

src/main/java/com/iemr/ecd/controller/callallocation/CallConfigurationController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.http.HttpStatus;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.http.ResponseEntity;
30-
import org.springframework.web.bind.annotation.CrossOrigin;
30+
3131
import org.springframework.web.bind.annotation.GetMapping;
3232
import org.springframework.web.bind.annotation.PathVariable;
3333
import org.springframework.web.bind.annotation.PostMapping;
@@ -48,7 +48,6 @@
4848

4949
@RestController
5050
@RequestMapping(value = "/callConfiguration", headers = "Authorization")
51-
@CrossOrigin()
5251
public class CallConfigurationController {
5352

5453
@Autowired

src/main/java/com/iemr/ecd/controller/dataupload/DataTemplateController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.http.HttpStatus;
3030
import org.springframework.http.MediaType;
3131
import org.springframework.http.ResponseEntity;
32-
import org.springframework.web.bind.annotation.CrossOrigin;
32+
3333
import org.springframework.web.bind.annotation.GetMapping;
3434
import org.springframework.web.bind.annotation.PathVariable;
3535
import org.springframework.web.bind.annotation.PostMapping;
@@ -48,7 +48,6 @@
4848

4949
@RestController
5050
@RequestMapping(value = "/dataTemplate", headers = "Authorization")
51-
@CrossOrigin()
5251
public class DataTemplateController {
5352

5453
@Autowired

0 commit comments

Comments
 (0)