Spring Boot跨域配置 – MyApplication和WebConfig

huangapple go评论151阅读模式
英文:

Springboot Cors config - MyApplication and WebConfig

问题

  1. 我的Spring Boot应用程序最终有两个类,它们都有CORS配置。
  2. 我认为其中一个是不必要的。有人能解释为什么会有两个吗?
  3. MyApplication类:
  4. @EntityScan("com.nz.myapp")
  5. @SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
  6. public class MyApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(MyApplication.class, args);
  9. }
  10. public WebMvcConfigurer corsConfigurer() {
  11. return new WebMvcConfigurer() {
  12. @Override
  13. public void addCorsMappings(CorsRegistry registry) {
  14. registry.addMapping("/**");
  15. }
  16. };
  17. }
  18. }
  19. WebConfig类:
  20. @Configuration
  21. @EnableWebMvc
  22. public class WebConfig extends WebMvcConfigurerAdapter {
  23. @Override
  24. public void addCorsMappings(CorsRegistry registry) {
  25. registry.addMapping("/**");
  26. }
  27. }
英文:

My Spring Boot application has ended up with two classes that both have cors configuration in them.

I presume one of them is unnecessary. Can someone explain why there is both?

MyApplication:

  1. @EntityScan("com.nz.myapp")
  2. @SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
  3. public class MyApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(MyApplication.class, args);
  6. }
  7. public WebMvcConfigurer corsConfigurer() {
  8. return new WebMvcConfigurer() {
  9. @Override
  10. public void addCorsMappings(CorsRegistry registry) {
  11. registry.addMapping("/**");
  12. }
  13. };
  14. }

WebConfig

  1. @Configuration
  2. @EnableWebMvc
  3. public class WebConfig extends WebMvcConfigurerAdapter {
  4. @Override
  5. public void addCorsMappings(CorsRegistry registry) {
  6. registry.addMapping("/**");
  7. }
  8. }

答案1

得分: 1

WebMvcConfigurerAdapter已被弃用,不应再使用它。只需使用WebMvcConfigurer的实现:

  1. @Configuration(proxyBeanMethods = false)
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addCorsMappings(CorsRegistry registry) {
  5. registry.addMapping("/**");
  6. }
  7. }

然而,以后您可能仍然会在SecurityFilterChain中设置CORS配置,因此WebMvcConfigurer更可能用于设置资源处理程序和格式化程序。

英文:

The WebMvcConfigurerAdapter is deprecated, you shouldn't use it anymore. Just stick with an implementation of WebMvcConfigurer:

  1. @Configuration(proxyBeanMethods = false)
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addCorsMappings(CorsRegistry registry) {
  5. registry.addMapping("/**");
  6. }
  7. }

However, later on you might set CORS configuration inside your SecurityFilterChain anyway, so the WebMvcConfigurer might more likely be used to set up resource handlers and formatters.

huangapple
  • 本文由 发表于 2023年8月5日 10:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定