chenlee9876
V2EX  ›  Java

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

  •  
  •   chenlee9876 · Feb 5, 2021 · 1816 views
    This topic created in 1936 days ago, the information mentioned may be changed or developed.

    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  
       Feb 5, 2021   ❤️ 1
    编译错误?
    CorsFilter 有俩
    你引了 org.apache.catalina.filters 下的
    需要 org.springframework.web.filter 下的
    ardour
        2
    ardour  
       Feb 5, 2021
    node 项目里我倒是经常配 cors 让服务器裸奔解决跨域. java...恕我路过一下
    chenlee9876
        3
    chenlee9876  
    OP
       Feb 5, 2021
    @chendy 感谢老哥回复,确实是这个问题
    好新手的错误啊,学习了
    liian2019
        4
    liian2019  
       Feb 7, 2021   ❤️ 1
    补充下,addAllowedHeader 现在好像不支持*了,需要指明
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3318 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    ♥ Do have faith in what you're doing.