• SpringBoot解决跨域问题


    方法一: 创建一个filter解决跨域,此方法也支持springmvc。

    @Component
    public class SimpleCORSFilter implements Filter {
    
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
            chain.doFilter(req, res);
        }
    
        public void init(FilterConfig filterConfig) {}
    
        public void destroy() {}
    } 
    

    方法二:基于继承WebMvcConfigurerAdapter或者实现WebMvcConfigurer接口配置加入Cors的跨域,此方法常用于springboot。

    import org.springframework.context.annotation.Configuration; 
    import org.springframework.web.servlet.config.annotation.CorsRegistry; 
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
    
    @Configuration 
    public class CorsConfig extends WebMvcConfigurerAdapter { 
        @Override 
        public void addCorsMappings(CorsRegistry registry) { 
            registry.addMapping("/**") 
                    .allowedOrigins("*") 
                    .allowCredentials(true) 
                    .allowedMethods("GET", "POST", "DELETE", "PUT") 
                    .maxAge(3600); 
        } 
    }
    import com.common.constant.TokenConstants;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
                    // 设置允许跨域的路由
            registry.addMapping("/**")
                    // 设置允许跨域请求的域名
                    .allowedOriginPatterns("*")
                    // 是否允许证书(cookies)
                    .allowCredentials(true)
                    // 设置允许的方法
                    .allowedMethods("*")
                    // 跨域允许时间
                    .maxAge(3600);
        }
    } 

    方法三:设置特别控制层允许跨域

    @CrossOrigin(origins = "http://192.168.1.10:8080", maxAge = 3600)
    @RequestMapping("/index")
    @RestController
    public class IndexController{
    }
    

    @CrossOrigin这个注解在controller类中使用,就可以指定该controller中所有方法都能处理来自http:19.168.1.10:8080中的请求。

  • 相关阅读:
    【笔记篇】(理论向)快速傅里叶变换(FFT)学习笔记w
    【学术篇】bzoj2440 [中山市选2011]完全平方数
    【笔记篇】斜率优化dp(五) USACO08MAR土地购(征)买(用)Land Acquisition
    【笔记篇】斜率优化dp(四) ZJOI2007仓库建设
    【笔记篇】斜率优化dp(三) APIO2010特别行动队
    【笔记篇】斜率优化dp(二) SDOI2016征途
    【笔记篇】斜率优化dp(一) HNOI2008玩具装箱
    【笔记篇】单调队列优化dp学习笔记&&luogu2569_bzoj1855股票交♂易
    usr/include/php5/ext/pcre/php_pcre.h:29:18: fatal error: pcre.h 错误解决
    ubuntu 使用apt-get install 安装php5.6--php7
  • 原文地址:https://www.cnblogs.com/wffzk/p/15818073.html
Copyright © 2020-2023  润新知