Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report