springboot项目支持cookie跨域的过滤器配置

2021-09-26 11:02:21 浏览数 (1)

代码语言:javascript复制
@Component
public class FilterConfig implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        //支持跨域请求
        response.setHeader("Access-Control-Allow-Origin",request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "*");
        //是否支持cookie跨域
        response.setHeader("Access-Control-Allow-Credentials", "true");
        //Origin, X-Requested-With, Content-Type, Accept,Access-Token
        response.setHeader("Access-Control-Allow-Headers",
                "Authorization,Origin, X-Requested-With, Content-Type, Accept,Access-Token");
        return true;
    }
}

0 人点赞