跨域问题出现的原因:同源策略;
解决方法1、(局部解决跨域问题)
对于某一个请求,解决他的跨域问题,在对应的controller上添加一个注解,如下
@RestController
@RequestMapping("movie")
//@CrossOrigin(origins = "http://localhost", allowCredentials = "true")
@CrossOrigin
public class MovieController {
@GetMapping("list")
public ResultVO getMovies(){
System.out.println("进入movie/list");
return new ResultVO();
}
}
解决方法二:全局解决跨域问题:添加一个过滤器,解决所有请求的跨域问题,如下
package com.woniuxy.cateyes.auth.shiro.filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;