zoukankan      html  css  js  c++  java
  • 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() {} 
    
    }
  • 相关阅读:
    类加载机制
    PTA(BasicLevel)-1094 谷歌的招聘
    PTA(BasicLevel)-1023 组个最小数
    异构图神经网络笔记-Heterogeneous Graph Neural Network(KDD19)
    PTA(BasicLevel)-1014 福尔摩斯的约会
    PTA(BasicLevel)-1013 数素数
    PTA(BasicLevel)-1012 数字分类
    PTA(BasicLevel)-1010 一元多项式求导
    PTA(BasicLevel)-1009 说反话
    PTA(BasicLevel)-1008数组元素循环右移问题
  • 原文地址:https://www.cnblogs.com/zhyStudy/p/10472558.html
Copyright © 2011-2022 走看看