diff --git a/emp_backend/src/main/java/com/example/demo/EmpBackendApplication.java b/emp_backend/src/main/java/com/example/demo/EmpBackendApplication.java index 2b2e5b9..0a4a04c 100644 --- a/emp_backend/src/main/java/com/example/demo/EmpBackendApplication.java +++ b/emp_backend/src/main/java/com/example/demo/EmpBackendApplication.java @@ -2,11 +2,27 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.filter.CorsFilter; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; + @SpringBootApplication -@CrossOrigin(origins = "http://localhost:4200") +//@CrossOrigin(origins = "http://localhost:4200") public class EmpBackendApplication { + @Bean + public CorsFilter corsFilter() { + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + CorsConfiguration config = new CorsConfiguration(); + config.setAllowCredentials(true); + config.addAllowedOrigin("http://localhost:4200"); + config.addAllowedHeader("*"); + config.addAllowedMethod("*"); + source.registerCorsConfiguration("/**", config); + return new CorsFilter(source); + } public static void main(String[] args) { SpringApplication.run(EmpBackendApplication.class, args); diff --git a/emp_backend/src/main/java/com/example/demo/controller/EmployeeController.java b/emp_backend/src/main/java/com/example/demo/controller/EmployeeController.java index 60deb6a..f215a26 100644 --- a/emp_backend/src/main/java/com/example/demo/controller/EmployeeController.java +++ b/emp_backend/src/main/java/com/example/demo/controller/EmployeeController.java @@ -28,7 +28,7 @@ public class EmployeeController { private EmployeeRepository employeeRepository; //get all data - @CrossOrigin(origins = "http://localhost:4200") +// @CrossOrigin(origins = "http://localhost:4200") @GetMapping("/employees") public List getAllEmployees(){ return employeeRepository.findAll(); @@ -37,7 +37,7 @@ public List getAllEmployees(){ //create - @CrossOrigin(origins = "http://localhost:4200") +// @CrossOrigin(origins = "http://localhost:4200") @PostMapping("/employees") public Employee createEmployee(@RequestBody Employee employee) { @@ -46,7 +46,7 @@ public Employee createEmployee(@RequestBody Employee employee) // get data by id - @CrossOrigin(origins = "http://localhost:4200") +// @CrossOrigin(origins = "http://localhost:4200") @GetMapping("/employees/{id}") public ResponseEntity getByID(@PathVariable Long id) { Employee employee = employeeRepository.findById(id). @@ -56,7 +56,7 @@ public ResponseEntity getByID(@PathVariable Long id) { //update data - @CrossOrigin(origins = "http://localhost:4200") +// @CrossOrigin(origins = "http://localhost:4200") @PutMapping ("/employees/{id}") public ResponseEntity updateEmployeeByID(@PathVariable Long id, @RequestBody Employee employeeDetails){ Employee employee = employeeRepository.findById(id). @@ -78,7 +78,7 @@ public ResponseEntity updateEmployeeByID(@PathVariable Long id, @Reque - @CrossOrigin(origins = "http://localhost:4200") +// @CrossOrigin(origins = "http://localhost:4200") @DeleteMapping("/employees/{id}") public ResponseEntity >deleteEmployee(@PathVariable Long id){