Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of cross-domain configuration of springboot2.4

Shulou Source: shulou.com Published: 2022-05-31 13:38:03 09月13日 Update

This article introduces the relevant knowledge of "how to solve the problem of cross-domain configuration of springboot2.4". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. If it is just a simple springboot demo, use the following configuration

Create a new config class

```import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer / * * @ author yk * @ date, 2021-7-19 14:36 * / @ Configurationpublic class WebConfig implements WebMvcConfigurer {@ Override public void addCorsMappings (CorsRegistry registry) {registry.addMapping ("/ *") .allowedOriginPatterns ("*") .allowedOriginPatterns ("*") .maxAge (3600) .allowCredentials (true);}} ```

2. But in actual development, we need to combine spring-security, oauth2, and so on, and we will find that the above configuration is invalid. That is because the previous Filter priority is too high, so we can take the following configuration.

````import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter; / * * @ author yk * @ date 16:21 on 2021-7-19 * / @ Configurationpublic class CrosConfig {@ Bean public FilterRegistrationBean corsFilter () {CorsConfiguration config = new CorsConfiguration () Config.setAllowCredentials (true); config.addAllowedOriginPattern ("*"); config.addAllowedHeader ("*"); config.addAllowedMethod ("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (); source.registerCorsConfiguration ("/ * *", config); FilterRegistrationBean bean = new FilterRegistrationBean (new CorsFilter (source)); / / set the highest priority bean.setOrder (Ordered.HIGHEST_PRECEDENCE) here Return bean;}} "how to solve the problem of cross-domain configuration of springboot2.4" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Configuration questions priorities content reality more knowledge practicality supreme learning and then just dilemmas situations articles cases for use editing websites industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Microsoft Linux Xiaomi