一、可以在配置类中通过配置一个@Bean,直接实现全局的跨域,不需要在每个controller上加@CrossOrigin注解了。
package cn.edu.tju.config;
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.WebMvcConfigurer;
@Configuration(proxyBeanMethods = false)
public class MyCorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("
try {
OutputStream os = response.getOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
response.setHeader("Content-Length", String.valueOf(bais.available()));
byte[] b = new byte[1024];
while ((bais.read(b)) > 0) {
os.write(b);
}
bais.close();
os.flush();
os.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}



