• springBoot 跨域处理


    首先喝水不忘挖井人,博客参考:https://www.cnblogs.com/nananana/p/8492185.html

    方式一:新增一个configration类 或 在Application中加入CorsFilter和CorsConfiguration方法

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    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;
    
    @Configuration
    @SpringBootApplication
    public class FileProApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(FileProApplication.class, args);
        }
    
        
        
        private CorsConfiguration buildConfig() {  
            CorsConfiguration corsConfiguration = new CorsConfiguration();  
            corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
            corsConfiguration.addAllowedHeader("*"); // 2允许任何头
            corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等) 
            return corsConfiguration;  
        }  
      
        @Bean  
        public CorsFilter corsFilter() {  
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
            source.registerCorsConfiguration("/**", buildConfig()); // 4  
            return new CorsFilter(source);  
        }  
    
    }

    但是我在和前端人员调试的过程中,发现此方法,给pc端有用,但是微信开发,还是会报跨域问题,所以我用了第二种方法,并且加以改善:

    我把接收到的跨域请求头部,重新转发回去。这样就解决了微信端的跨域问题。

    方式二:使用过滤器方式,接收到头部,转发回去

    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.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class CorsFilter implements Filter{
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;  
            String header = request.getHeader("Origin");
            HttpServletResponse response = (HttpServletResponse) res;  
            if (header != null || !"".equals(header)) {//header不为空,就直接将拿到的转发回去
                response.setHeader("Access-Control-Allow-Origin", header);
            }else {
                response.setHeader("Access-Control-Allow-Origin", "*");
            }
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "*");
            response.setHeader("Access-Control-Allow-Credentials","true");
            chain.doFilter(req, res);  
        }
        
        public void init(FilterConfig filterConfig) {}  
        public void destroy() {} 
    
    }
  • 相关阅读:
    npm 一些常用的命令
    Angular Encapsulation
    隐藏scrollbar
    Vue生命周期详解(1)- 单个组件
    如何自己制作iconfont
    day07-2018-10--25 深浅拷贝
    day06-2018-10--24 小数据池和编码
    day05-2018-10--23 字典
    day04-2018-10--22python基础
    day03-2018-10-19-python基础
  • 原文地址:https://www.cnblogs.com/zhyStudy/p/10472558.html
Copyright © 2020-2023  润新知