前端vue项目调用后端SpringBoot接口,后端配置了跨域访问,但是出现了get请求能正常访问,但是post请求报403异常的情况。如下图在这里插入图片描述
最后发现是后端的问题,正常配置的后端跨域配置中需要加上一句:

	.allowedOriginPatterns("*")

具体原因:Springboot的版本问题

完整版跨域配置如下:

package com.example.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 WebMVCConfig implements WebMvcConfigurer {


    /**
     * 配置跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //跨域配置:前端是8080端口,后端是8888
        //要允许8080访问接口服务
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8080")
                .allowCredentials(true)
                .allowedMethods("GET","POST")
                .allowedOriginPatterns("*")
        .maxAge(3600)
        ;
    }

}

Logo

前往低代码交流专区

更多推荐