Table of Contents前端
咱們在開發過程當中常常會遇到先後端分離而致使的跨域問題,致使沒法獲取返回結果。跨域就像分離前端和後端的一道鴻溝,君在這邊,她在那邊,兩兩不能往來.java
跨域(CORS)是指不一樣域名之間相互訪問。跨域,指的是瀏覽器不能執行其餘網站的腳本,它是由瀏覽器的同源策略所形成的,是瀏覽器對於JavaScript所定義的安全限制策略。react
以上三個條件中有一個條件不一樣就會產生跨域問題。nginx
使用Filter過濾器來過濾服務請求,向請求端設置Response Header(響應頭部)的Access-Control-Allow-Origin屬性聲明容許跨域訪問。web
@WebFilter public class CorsFilter implements Filter { @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", "*"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "*"); response.setHeader("Access-Control-Allow-Credentials", "true"); chain.doFilter(req, res); } }
@Component public class CrossInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "*"); response.setHeader("Access-Control-Allow-Credentials", "true"); return true; } }
@Configuration @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") public class AppConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 攔截全部的請求 .allowedOrigins("http://www.abc.com") // 可跨域的域名,能夠爲 * .allowCredentials(true) .allowedMethods("*") // 容許跨域的方法,能夠單獨配置 .allowedHeaders("*"); // 容許跨域的請求頭,能夠單獨配置 } }
location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; if ($request_method = 'OPTIONS') { return 204; } }
@CrossOrgin
註解若是隻是想部分接口跨域,且不想使用配置來管理的話,能夠使用這種方式spring
在Controller使用後端
@CrossOrigin @RestController @RequestMapping("/user") public class UserController { @GetMapping("/{id}") public User get(@PathVariable Long id) { } @DeleteMapping("/{id}") public void remove(@PathVariable Long id) { } }
在具體接口上使用跨域
@RestController @RequestMapping("/user") public class UserController { @CrossOrigin @GetMapping("/{id}") public User get(@PathVariable Long id) { } @DeleteMapping("/{id}") public void remove(@PathVariable Long id) { } }
spring: cloud: gateway: globalcors: cors-configurations: '[/**]': # 容許跨域的源(網站域名/ip),設置*爲所有 # 容許跨域請求裏的head字段,設置*爲所有 # 容許跨域的method, 默認爲GET和OPTIONS,設置*爲所有 allow-credentials: true allowed-origins: - "http://xb.abc.com" - "http://sf.xx.com" allowed-headers: "*" allowed-methods: - OPTIONS - GET - POST - DELETE - PUT - PATCH max-age: 3600
注意: 經過gateway
轉發的其餘項目,不要進行配置跨域配置瀏覽器
有時即便配置了也不會起做用,這時你能夠根據瀏覽器控制的錯誤輸出來查看問題,若是提示是 response
中 header 出現了重複的 Access-Control-*
請求頭,能夠進行以下操做安全
import java.util.ArrayList; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @Component("corsResponseHeaderFilter") public class CorsResponseHeaderFilter implements GlobalFilter, Ordered { @Override public int getOrder() { // 指定此過濾器位於NettyWriteResponseFilter以後 // 即待處理完響應體後接着處理響應頭 return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1; } @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return chain.filter(exchange).then(Mono.defer(() -> { exchange.getResponse().getHeaders().entrySet().stream() .filter(kv -> (kv.getValue() != null && kv.getValue().size() > 1)) .filter(kv -> ( kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN) || kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS) || kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS) || kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS) || kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_MAX_AGE))) .forEach(kv -> { kv.setValue(new ArrayList<String>() {{ add(kv.getValue().get(0)); }}); }); return chain.filter(exchange); })); } }