CORS issue with Tomcat and Android Webview

后端 未结 5 788
南笙
南笙 2021-02-15 12:38

I am facing a strange problem with Tomcat 8 and CORS. I am developing a Hybrid web app using ionicframework, AngularJS, Cordova as front end and Tomcat 8 and Spring 3 as back-en

5条回答
  •  太阳男子
    2021-02-15 13:27

    Thank you JuergenB, I spent lot of hours looking for this solution for Apache Isis Framework that uses Swagger for API. The problem with Ionic app when you try to connect to API URL is always the same:

    XMLHttpRequest cannot load http://localhost:8080/restful/services/Object/actions/listAll/invoke
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    Origin 'http://localhost:8100' is therefore not allowed access
    

    Here I share the complete code that I implemented

    \webapp\pom.xml

        
        
            org.ebaysf.web
            cors-filter
            1.0.1
        
    

    \webapp\src\main\webapp\WEB-INF\web.xml

    
        
            CORS Filter
            org.ebaysf.web.cors.CORSFilter
            
                cors.allowed.origins
                http://192.168.1.100:8100,http://localhost,https://localhost,http://localhost:8100,https://localhost:8100,file://
            
            
                cors.allowed.methods
                GET,POST,HEAD,OPTIONS,PUT
            
            
                cors.allowed.headers
                Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
            
            
                cors.exposed.headers
                Access-Control-Allow-Origin,Access-Control-Allow-Credentials,Authorization
            
            
                cors.support.credentials
                true
            
            
                cors.preflight.maxage
                1800
            
            
                cors.logging.enabled
                true
            
        
        
            CORS Filter
            /restful/*
        
    

    And then in Ionic project you can do this command

    ionic serve --address=192.168.1.100
    change with your PC IP
    

    You can test your app compiling it to a platform Android/IOS or simply access through your phone to http://yourip:8100

提交回复
热议问题