前端跨域方法 和 后端跨域方法 总和

1前端

  

2 后台一共有4种

package com.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author lbh
 * @date 2019/3/20 10:29
 * @description:
 */

//以下是非springboot项目的全局CORS配置,解决前后端分离跨域问题。
/*@Configuration
@EnableWebMvc
@Slf4j
public class CrosWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("==========非springboot项目的全局CORS配置,解决前后端分离跨域问题=========");
        registry.addMapping("/**");
    }
}*/


//以下是springboot项目的全局CORS配置,解决前后端分离跨域问题。
@Slf4j
@Configuration
public class CrosWebConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                log.info("==========springboot项目的全局CORS配置,解决前后端分离跨域问题=========");
                registry.addMapping("/**");
            }
        };
    }
}

 2

package com.common.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lbh
 * @date 2019/3/20 9:56
 * @description:设置server的header来设置浏览器对于服务器跨域的限制,解决前后端分离跨域问题。
 */
//统一过滤器设置
@Slf4j
@WebFilter(urlPatterns = "/*",filterName = "CharacterEncodingFilter")
public class CrosDomainFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @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-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        log.info("=========设置server的header来设置浏览器对于服务器跨域的限制,解决前后端分离跨域问题。=====================");
        chain.doFilter(req, res);
    }

    @Override
    public void destroy() {

    }

    //spring boot过滤器设置
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CrosDomainFilter domainFilter = new CrosDomainFilter();
        registrationBean.setFilter(domainFilter);
        List<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

}

 3

//    前后端分离跨域问题解决方案四。【全局配置】
    //如果您正在使用Spring Security,请确保在Spring安全级别启用CORS,并允许它利用Spring MVC级别定义的配置。
/*@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()...
    }*/
}

4

在Controller层类级别或者接口级别配置注解@CrossOrigin解决跨域问题

  

猜你喜欢

转载自www.cnblogs.com/gfweb/p/10620355.html