1前端前端
2 後臺一共有4種java
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("/**"); } }; } }
2web
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; } }
3spring
// 先後端分離跨域問題解決方案四。【全局配置】 //若是您正在使用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解決跨域問題