Java Spring WebApi跨域问题解决

WebApi跨域问题解决


添加以下代码即可解决跨域问题

package com.jet.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    
    
    @Bean
    public CorsFilter corsFilter() {
    
    
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        //注册跨域配置
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }

}

方法 说明
addAllowedOrigin(“*”) *代表允许访问所有源地址
addAllowedMethod(“*”) *代表允许访问所有源请求方法
addAllowedHeader(“*”) *代表允许访问所有源请求头

猜你喜欢

转载自blog.csdn.net/weixin_44792145/article/details/129090899