其他分享
首页 > 其他分享> > 后端解决跨域问题

后端解决跨域问题

作者:互联网

package com.ebike.webapps.operator.config;

import com.google.common.net.HttpHeaders;
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;

/**
 * @author tupz
 * @version 1.0
 * @date 2021/7/15 18:03
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").
                    allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
                    allowedMethods("*"). //允许任何方法(post、get等)
                    allowedHeaders("*"). //允许任何请求头
                    allowCredentials(true). //带上cookie信息
                    exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
            }
        };
    }
}

标签:跨域,springframework,问题,WebMvcConfigurer,import,解决,org,config,annotation
来源: https://blog.csdn.net/weixin_49064195/article/details/118766310