CrosConfig.java 1.0 KB

1234567891011121314151617181920
  1. package com.customer.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class CrosConfig implements WebMvcConfigurer {
  7. static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };
  8. @Override
  9. public void addCorsMappings(CorsRegistry registry) {
  10. registry.addMapping("/**") // 所有的当前站点的请求地址,都支持跨域访问。
  11. .allowedOriginPatterns("*") // 所有的外部域都可跨域访问。 如果是localhost则很难配置,因为在跨域请求的时候,外部域的解析可能是localhost、127.0.0.1、主机名
  12. .allowCredentials(true) // 是否支持跨域用户凭证
  13. .allowedMethods(ORIGINS) // 当前站点支持的跨域请求类型是什么
  14. .maxAge(3600); // 超时时长设置为1小时。 时间单位是秒。
  15. }
  16. }