SpringBoot 跨域 Access

Stella981
• 阅读 765

https://blog.csdn.net/taoism\_jerry/article/details/79695336

************************************************************

方式一:
在过滤器中设置响应头

@Component
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;

        HttpServletRequest reqs = (HttpServletRequest) req;

        // response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void destroy() {}

方式二:
对SpringBoot进行配置

@Configuration  
public class CorsConfig extends WebMvcConfigurerAdapter {  

    @Override  
    public void addCorsMappings(CorsRegistry registry) {  
        registry.addMapping("/**")  
                .allowedOrigins("*")  
                .allowCredentials(true)  
                .allowedMethods("GET", "POST", "DELETE", "PUT")  
                .maxAge(3600);  
    }  

}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
室与 室与
3年前
Cors跨域解决
一、浏览器跨域问题产生1、跨源资源共享(CORS)中文文档:https://developer.mozilla.org/zhCN/docs/Web/HTTP/Access_control_CORS2、什么是浏览器跨域问题指的是浏览器不能执行其他网站的脚本。JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象,即同源策略。就好比我
Easter79 Easter79
3年前
vue 实现 leaflet的测绘,测距,测面
参考1:https://blog.csdn.net/lonly\_maple/article/details/83658608参考2:https://blog.csdn.net/xtfge0915/article/details/80275094\_60前提:vue项目在安装leaflet的基础上 cnpminstallleafletdra
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
kelly kelly
3年前
Ngnix入门教程
(https://blog.csdn.net/Dawn____Dawn/article/details/114730487?utm_mediumdistribute.pc_category.nonetaskbloghot13.nonecase&dist_request_id&depth_1utm_sourcedistribute.pc_c
DevOpSec DevOpSec
3年前
解读阿里巴巴开发规范之MySQL
(https://blog.csdn.net/weixin_37686415/article/details/110276789?utm_mediumdistribute.pc_relevant.nonetaskblogOPENSEARCH6.control&dist_request_id&depth_1utm_sourcedistrib
Stella981 Stella981
3年前
Notepad++和MinGW的安装和配置
http://blog.csdn.net/cclovepl/article/details/70568313http://blog.csdn.net/cclovepl/article/details/70520412http://blog.csdn.net/guang670248515/article/details/70186454http:/
Stella981 Stella981
3年前
JVM性能调优
http://blog.csdn.net/rodesad/article/details/51545109http://blog.csdn.net/rodesad/article/details/51544977http://blog.csdn.net/rodesad/article/details/51545039ht
Wesley13 Wesley13
3年前
mysql远程备份
相关链接:https://blog.csdn.net/LiuHuan\_study/article/details/81512831https://www.cnblogs.com/ryanzheng/p/8444128.html1.跨服务器备份原理:服务器A192.168.100.200mysqlroot123456
Wesley13 Wesley13
3年前
ubuntu lapack安装与使用
https://blog.csdn.net/zouyu1746430162/article/details/53374693https://blog.csdn.net/mlnotes/article/details/9676269https://blog.csdn.net/yuanxing14/article/details/41744461ht