V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenlee9876
V2EX  ›  Java

请教一下各位老哥 Cors 的配置问题

  •  
  •   chenlee9876 · 2021-02-05 22:36:21 +08:00 · 1008 次点击
    这是一个创建于 1168 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Java 初学,在跟着网课搭网站 目前在学跨域这一块 这是我的配置文件

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

    在 idea 里 configSource 下面会报 error 错误提示

    'org.apache.catalina.filters.CorsFilter' 中的 'CorsFilter()' 无法应用于 '(org.springframework.web.cors.UrlBasedCorsConfigurationSource)'
    

    请教下这个是啥意思啊 在网上搜了半天也找不到对应的问题(包括 Stackflow 这个问题是不是太基础了 我看别人都是这么写的啊= =

    chendy
        1
    chendy  
       2021-02-05 22:50:10 +08:00   ❤️ 1
    编译错误?
    CorsFilter 有俩
    你引了 org.apache.catalina.filters 下的
    需要 org.springframework.web.filter 下的
    ardour
        2
    ardour  
       2021-02-05 23:04:27 +08:00
    node 项目里我倒是经常配 cors 让服务器裸奔解决跨域. java...恕我路过一下
    chenlee9876
        3
    chenlee9876  
    OP
       2021-02-05 23:46:01 +08:00
    @chendy 感谢老哥回复,确实是这个问题
    好新手的错误啊,学习了
    liian2019
        4
    liian2019  
       2021-02-07 11:58:13 +08:00   ❤️ 1
    补充下,addAllowedHeader 现在好像不支持*了,需要指明
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2774 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:11 · PVG 20:11 · LAX 05:11 · JFK 08:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.