编程语言
首页 > 编程语言> > Java后端解决跨域问题

Java后端解决跨域问题

作者:互联网

编写一个config配置类,内容如下:

package com.gk.book.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 解决跨域问题
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

标签:web,Java,跨域,后端,springframework,org,import,config,annotation
来源: https://www.cnblogs.com/gekun154/p/15947518.html