How to solve Cross-domain problems through yue-library in SpringBoot
Today, I will talk to you about how to solve cross-domain problems through yue-library in SpringBoot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Cross-domain description
Under the separation of front and rear, cross-domain has become a clich é topic, but many small partners still often face this problem, and the solutions are varied. Here is a simple and direct back-end solution.
Solve cross-domain (server) / * * CorsConfig- cross-domain * * @ author ylyue * @ since November 26th, 2018 * / @ Slf4j@Configurationpublic class CorsFilterConfig {/ / CorsConfig- cross-domain @ Bean @ ConditionalOnMissingBean public CorsFilter corsFilter () {final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (); final CorsConfiguration config = new CorsConfiguration () Config.setAllowCredentials (true); config.setAllowedHeaders (Arrays.asList ("*")); config.setAllowedMethods (Arrays.asList ("*")); config.setAllowedOrigins (Arrays.asList ("*")); config.setMaxAge (3600L) / / set the Headers// config.setExposedHeaders (exposedHeaders) allowed by response to expose; / / config.addExposedHeader ("token"); source.registerCorsConfiguration ("/ *", config) Log.info ("[initialize configuration-Cross-Domain] allows cross-domain access by default. Initialized. ") ; return new CorsFilter (source);}} Cross-domain solution based on yue-library
The above code is extracted from yue-library, so yue-library-based projects have solved cross-domain problems by default. Yue-library is an enhanced library of SpringBoot. Cross-domain can be solved by introducing project dependency.
Maven project, add the following code to the pom.xml file, and replace ${version} with the corresponding version number:
Ai.ylyue yue-library-dependencies ${version}
Then introduce the required modules, such as the basic library: yue-library-base
Ai.ylyue yue-library-base...
Click to quickly learn about yue-library
Conclusion
Cross-domain problems are recommended to be solved by the server. There is no need to enable cross-domain security access restrictions. It can only prevent JS script injection in some browsers, but cannot achieve real security access restrictions. If you have any questions, please leave a message below.
After reading the above, do you have any further understanding of how to solve cross-domain problems through yue-library in SpringBoot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.