• SpringBoot解决跨域问题


    SpringBoot解决跨域问题----使用Filter

    1. 创建一个拦截器
    2. 配置启动参数

    创建一个Filter,配置相关的域信息

    主要是`response.addHeader("Access-Control-Allow-Origin", "xxxx");`
    
    package com.bigorang;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class CrosFilter implements javax.servlet.Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) servletResponse;
    
            //支持所有域的跨域调用
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            String origin = request.getHeader("Origin");
            if (origin != null && !"".equals(origin)) {
                response.addHeader("Access-Control-Allow-Origin", origin);
            }
    
            //前端自定义头header时,需要设置Access-Control-Allow-Headers
            //支持所有带头的跨域调用
            String headers = request.getHeader("Access-Control-Allow-Headers");
            if (headers != null && !"".equals(headers)) {
                response.addHeader("Access-Control-Allow-Headers", headers);
            }
    
    
            //带Cookie的时候,origin必须是全匹配,不能使用*,或者使用上方的获取后再设置,支持所有的域
    //        response.addHeader("Access-Control-Allow-Origin","*");
            response.addHeader("Access-Control-Allow-Method", "GET");
    //        response.addHeader("Access-Control-Allow-Headers","Content-Type");
            response.addHeader("Access-Control-Max-Age", "3600");
            response.addHeader("Access-Control-Allow-Credential", "true");
    
    
            filterChain.doFilter(servletRequest, response);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    

    配置应用启动参数

    ```
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class AjaxServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AjaxServerApplication.class, args);
        }
    
    
    
    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.addUrlPatterns("/*");
        bean.setFilter(new CrosFilter());
        return bean;
        }
    }
    ```
  • 相关阅读:
    软件测试的术语及解释总结
    测试工具
    数据库客户端连接
    应用程序发生错误怎么修改
    软件测试容易忽略的缺陷
    LR中错误解决方法
    LR结果分析2
    LR结果分析
    Hive
    zookeeper
  • 原文地址:https://www.cnblogs.com/bigorang/p/9905527.html
Copyright © 2020-2023  润新知