Solve cross-domain problems with only one click through springboot

Solve cross-domain problems with only one click through springboot

This article explains how to solve cross-domain problems only through springboot.
Just add the CorsConfig class

package com.example.examandquestion.config;


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

@Configuration
public class CorsConfig {
    
    
  @Bean
  public CorsFilter corsFilter() {
    
    
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.addAllowedOrigin("http://localhost:8080");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    config.setAllowCredentials(true);
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
  }
}

insert image description here
Then there is no cross-domain problem for the front end.
insert image description here

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/131143780