Merge pull request #7277 from eugenp/BAEL-3036_CORS
BAEL-3036 Update Cors with Spring article and create the code
This commit is contained in:
		
						commit
						a6de2a93fb
					
				| @ -0,0 +1,9 @@ | ||||
| package com.baeldung.cors; | ||||
| 
 | ||||
| public class Account { | ||||
|     private Long id; | ||||
| 
 | ||||
|     public Account(Long id) { | ||||
|         this.id = id; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,24 @@ | ||||
| package com.baeldung.cors; | ||||
| 
 | ||||
| import org.springframework.web.bind.annotation.CrossOrigin; | ||||
| import org.springframework.web.bind.annotation.PathVariable; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
| 
 | ||||
| @CrossOrigin(maxAge = 3600) | ||||
| @RestController | ||||
| @RequestMapping("/account") | ||||
| public class AccountController { | ||||
| 
 | ||||
|     @CrossOrigin("http://example.com") | ||||
|     @RequestMapping("/{id}") | ||||
|     public Account retrieve(@PathVariable Long id) { | ||||
|         return new Account(id); | ||||
|     } | ||||
| 
 | ||||
|     @RequestMapping(method = RequestMethod.DELETE, path = "/{id}") | ||||
|     public void remove(@PathVariable Long id) { | ||||
|         // ... | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,16 @@ | ||||
| package com.baeldung.cors; | ||||
| 
 | ||||
| import org.springframework.context.annotation.Configuration; | ||||
| import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||||
| import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||||
| 
 | ||||
| @Configuration | ||||
| @EnableWebMvc | ||||
| public class WebConfig implements WebMvcConfigurer { | ||||
| 
 | ||||
|     @Override | ||||
|     public void addCorsMappings(CorsRegistry registry) { | ||||
|         registry.addMapping("/**"); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,25 @@ | ||||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||||
|        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|        xmlns:mvc="http://www.springframework.org/schema/mvc" | ||||
|        xsi:schemaLocation="http://www.springframework.org/schema/beans | ||||
|         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd | ||||
|         http://www.springframework.org/schema/mvc | ||||
|         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> | ||||
| 
 | ||||
|     <mvc:cors> | ||||
| 
 | ||||
|         <mvc:mapping path="/api/**" | ||||
|                      allowed-origins="http://domain1.com, http://domain2.com" | ||||
|                      allowed-methods="GET, PUT" | ||||
|                      allowed-headers="header1, header2, header3" | ||||
|                      exposed-headers="header1, header2" allow-credentials="false" | ||||
|                      max-age="123" /> | ||||
| 
 | ||||
|         <mvc:mapping path="/resources/**" | ||||
|                      allowed-origins="http://domain1.com" /> | ||||
| 
 | ||||
|     </mvc:cors> | ||||
| 
 | ||||
|     <mvc:view-controller path="/welcome.html"/> | ||||
| 
 | ||||
| </beans> | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user