必须配置个filter,在filter设置
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
搞定了。 
之后还报了个小错误,认证
httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authentication");
搞定了。
重要的事情说三次: 
必须在filter配置!! 
必须在filter配置!! 
必须在filter配置!!
直接在controller写和inteceptor写都是没鸟用的!!!!!!!!!!!!!!!!!
具体:
1.写个filter
2.配置在web.xml
问题解决。
另外Spring4.2也支持了跨域请求,直接使用**@CrossOrigin注解即可。**
Spring源码:
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CrossOrigin {
    String[] DEFAULT_ORIGINS = { "*" };
    String[] DEFAULT_ALLOWED_HEADERS = { "*" };
    boolean DEFAULT_ALLOW_CREDENTIALS = true;
    long DEFAULT_MAX_AGE = 1800;
    /**
     * 同origins属性一样
     */
    @AliasFor("origins")
    String[] value() default {};
    /**
     * 所有支持域的集合,例如"http://domain1.com"。
     * <p>这些值都显示在请求头中的Access-Control-Allow-Origin
     * "*"代表所有域的请求都支持
     * <p>如果没有定义,所有请求的域都支持
     * @see #value
     */
    @AliasFor("value")
    String[] origins() default {};
    /**
     * 允许请求头重的header,默认都支持
     */
    String[] allowedHeaders() default {};
    /**
     * 响应头中允许访问的header,默认为空
     */
    String[] exposedHeaders() default {};
    /**
     * 请求支持的方法,例如"{RequestMethod.GET, RequestMethod.POST}"}。
     * 默认支持RequestMapping中设置的方法
     */
    RequestMethod[] methods() default {};
    /**
     * 是否允许cookie随请求发送,使用时必须指定具体的域
     */
    String allowCredentials() default "";
    /**
     * 预请求的结果的有效期,默认30分钟
     */
    long maxAge() default -1;
}
在Controller上使用@CrossOrigin注解
@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
    @RequestMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }
    @RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}
这里指定当前的AccountController中所有的方法可以处理http://domain2.com域上的请求,
在方法上使用@CrossOrigin注解
@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/account")
public class AccountController {
    @CrossOrigin("http://domain2.com")
    @RequestMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }
    @RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}
在这个例子中,AccountController类上也有@CrossOrigin注解,retrieve方法上也有注解,Spring会合并两个注解的属性一起使用。
CORS全局配置
除了细粒度基于注解的配置,你可能会想定义一些全局CORS的配置。这类似于使用过滤器,但可以在Spring MVC中声明,并结合细粒度@CrossOrigin配置。默认情况下所有的域名和GET、HEAD和POST方法都是允许的。
基于JAVA的配置
看下面例子:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
            .allowedOrigins("http://domain2.com")
            .allowedMethods("PUT", "DELETE")
            .allowedHeaders("header1", "header2", "header3")
            .exposedHeaders("header1", "header2")
            .allowCredentials(false).maxAge(3600);
    }
}
如果你使用Spring Boot,你可以通过这种方式方便的进行配置。
基于XML的配置
<mvc:cors>
    <mvc:mapping path="/**" />
</mvc:cors>
这个配置和上面Java方式的第一种作用一样。
同样,你可以做更复杂的配置:
<mvc:cors>
    <mvc:mapping path="/api/**"
        allowed-origins="http://domain1.com, http://domain2.com"
        allowed-methods="GET, PUT"
        allowed-headers="header1, header2, header3"
        exposed-headers="header1, header2" allow-credentials="false"
        max-age="123" />
    <mvc:mapping path="/resources/**"
        allowed-origins="http://domain1.com" />
</mvc:cors>
 
  
  
  
 
 
  
 
 
 