日常提交
This commit is contained in:
@@ -652,3 +652,88 @@ public class ExampleAdvice2 {}
|
|||||||
public class ExampleAdvice3 {}
|
public class ExampleAdvice3 {}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## CORS
|
||||||
|
Spring MVC允许处理跨域问题。
|
||||||
|
> ### 跨域问题
|
||||||
|
> 处于对安全的考虑,浏览器禁止ajax访问非当前origin的资源。
|
||||||
|
> 对于简单请求,浏览器会直接通过请求和响应中特定header来判断当前资源是否能够被访问;对于非简单请求,则是在请求之前会发送一个预检请求(OPTIONS请求,判断当前资源能否被访问)。
|
||||||
|
|
||||||
|
在Spring MVC中,HandlerMapping实现提供了对CORS的支持。在实际将一个请求映射到handler后,HandlerMapping实现将会检查请求和handler的CORS配置并且做进一步的处理。
|
||||||
|
如果想要启用CORS,需要显式声明CORS配置,如果匹配的CORS配置未被找到,那么预检请求将会被拒绝。(CORS header将不会添加到简单请求和实际CORS请求的响应中,浏览器将会拒绝没有CORS header的响应)。
|
||||||
|
### @CrossOrigin
|
||||||
|
可以对handler method使用@CrossOrigin注解以允许跨域请求,使用示例如下:
|
||||||
|
```java
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/account")
|
||||||
|
public class AccountController {
|
||||||
|
|
||||||
|
@CrossOrigin
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Account retrieve(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void remove(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
默认情况下,@CrossOrigin会允许来自所有origin、含有任意header和所有http method。
|
||||||
|
- allowCredentials默认情况下没有启用,除非allowOrigins或allowOriginPatterns被指定了一个非"*"的值。
|
||||||
|
- maxAge默认会设置为30min
|
||||||
|
|
||||||
|
@CrossOrigin注解支持在类上使用,类上注解会被方法继承
|
||||||
|
```java
|
||||||
|
@CrossOrigin(origins = "https://domain2.com", maxAge = 3600)
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/account")
|
||||||
|
public class AccountController {
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Account retrieve(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void remove(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@CrossOrigin注解可同时在类和方法上使用
|
||||||
|
```java
|
||||||
|
@CrossOrigin(maxAge = 3600)
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/account")
|
||||||
|
public class AccountController {
|
||||||
|
|
||||||
|
@CrossOrigin("https://domain2.com")
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Account retrieve(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void remove(@PathVariable Long id) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### spring boot全局配置CORS
|
||||||
|
在spring boot中,可以通过如下方式全局配置CORS
|
||||||
|
```java
|
||||||
|
@Configuration
|
||||||
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
@Override
|
||||||
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
|
registry.addMapping("/api/**")
|
||||||
|
.allowedOrigins("http://localhost:4200")
|
||||||
|
.allowedMethods("*")
|
||||||
|
.allowedHeaders("*")
|
||||||
|
.allowCredentials(false)
|
||||||
|
.maxAge(3600);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user