Skip to content

Global CrossOrigin Implemented #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Employee> getAllEmployees(){
return employeeRepository.findAll();
Expand All @@ -37,7 +37,7 @@ public List <Employee> getAllEmployees(){


//create
@CrossOrigin(origins = "http://localhost:4200")
// @CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee employee)
{
Expand All @@ -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<Employee> getByID(@PathVariable Long id) {
Employee employee = employeeRepository.findById(id).
Expand All @@ -56,7 +56,7 @@ public ResponseEntity<Employee> getByID(@PathVariable Long id) {


//update data
@CrossOrigin(origins = "http://localhost:4200")
// @CrossOrigin(origins = "http://localhost:4200")
@PutMapping ("/employees/{id}")
public ResponseEntity<Employee> updateEmployeeByID(@PathVariable Long id, @RequestBody Employee employeeDetails){
Employee employee = employeeRepository.findById(id).
Expand All @@ -78,7 +78,7 @@ public ResponseEntity<Employee> updateEmployeeByID(@PathVariable Long id, @Reque



@CrossOrigin(origins = "http://localhost:4200")
// @CrossOrigin(origins = "http://localhost:4200")
@DeleteMapping("/employees/{id}")
public ResponseEntity <Map<String, Boolean> >deleteEmployee(@PathVariable Long id){

Expand Down