• springboot配置请求跨域问题



    import
    org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; /** * Created by myz on 2017/7/10. * * 设置跨域请求 */ @Configuration public class CorsConfig { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址 corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头 corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法 return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置 return new CorsFilter(source); } }

     第二种配置方法

    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Component;
    
    /**
     * Created by myz on 2017/7/10.
     *
     * 设置跨域请求
     */
    @Component
    public class CorsConfig implements Filter{
    
        @Override
        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-Credentials", "true");
            response.setHeader("Access-control-Allow-Methods", "*");
            response.setHeader("Access-control-Max-Age", "3600");
            response.setHeader("Access-control-Allow-Headers", "Content-Type,Access-Token");
        }
        
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
    
        @Override
        public void destroy() {
        }
        
    }
  • 相关阅读:
    Burpsuite intruder模块 越过token进行爆破,包含靶场搭建
    burpsuiteb windows10 下载与安装
    sqlmap的命令总结
    Vue.js与jQuery混用
    IE低版本cors跨域请求
    window.open打开网址被拦截
    一图一知之python3数据类型
    一图一知-vue强大的slot
    一图一知-强大的js数组
    windows中git输错密码后不能修改问题
  • 原文地址:https://www.cnblogs.com/yuhuiqing/p/10692854.html
Copyright © 2020-2023  润新知