Page 1 of 1

CORS Issue on REST WebService

PostPosted:Tue Sep 22, 2015 12:51 pm
by Jahangir
Hi,
I am facing CORS issue when trying to access REST WS of openKM from another application hosted on different port.
Code: Select all
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('okmAdmin' + ':' + 'admin');
$http.get('http://localhost:8081/OpenKM/services/rest/folder/getChildren?fldId=8c910ef3-2075-49ba-9e83-7237d4ee45fd').then(
  function(value) {
     alert('success value' + value);
  }, function(reason) {
    alert('failure value'+reason);	
  }, function(value) {
  }
ERROR:
Code: Select all
XMLHttpRequest cannot load http://localhost:8081/OpenKM/services/rest/folder/getChildren?fldId=8c910ef3-2075-49ba-9e83-7237d4ee45fd. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
I have added CORS Filter to Web.xml of Tomcat but its still not working.

I can access simple file hosted on http://localhost:8081/xyz.xml but unable to access openKM REST service

Re: CORS Issue on REST WebService

PostPosted:Thu Sep 24, 2015 9:32 am
by jllort
Which language are you using for accessing webservices ? php ?

Re: CORS Issue on REST WebService

PostPosted:Mon Sep 28, 2015 7:41 am
by Jahangir
I am using Angular JS and also tried simple Ajax for accessing rest webservice

Re: CORS Issue on REST WebService

PostPosted:Wed Sep 30, 2015 10:09 am
by jllort
I do not success accessing webservices across javascript, some months ago I tryed several strategies and I do not success with no one.

Re: CORS Issue on REST WebService

PostPosted:Fri Oct 02, 2015 1:12 pm
by Jahangir
I finally managed to run $ajax.get script from different domain with CORS settings by adding apache CORS filter to Web.xml. The only important point here is that you need to add these lines just above springSecurityChainFilter and it will work fine.
you can also change * with specific domain for more security.
Code: Select all
<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>  
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>
  </init-param>     
</filter>

<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Re: CORS Issue on REST WebService

PostPosted:Tue Nov 17, 2015 12:31 pm
by pavila
Have you created this JIRA http://jira.openkm.com/browse/OCM-6 ?

Re: CORS Issue on REST WebService

PostPosted:Thu Dec 17, 2015 5:19 am
by Jahangir
Nope i didnt create it

Sorry for replying late.

Re: CORS Issue on REST WebService

PostPosted:Tue May 04, 2021 2:41 am
by lalitpareshan
Followed the steps but still failing , if you can help

Error
Code: Select all
project_view.html:1 Access to XMLHttpRequest at 'http://okmAdmin:admin@139.168.136.192:8080/OpenKM/services/rest/document/getContent?docId=93cbc827-66b0-4ef6-b2c2-f3261c583004' from origin 'http://139.168.136.192:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
app.js:1599 
okmAdmin:admin@139.168.136.192:8080/OpenKM/services/rest/document/getContent?docId=93cbc827-66b0-4ef6-b2c2-f3261c583004:1 Failed to load resource: net::ERR_FAILED
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/js/popper.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/js/bootstrap.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools failed to load SourceMap: Could not load content for http://139.168.136.192:5000/web/assets/css/bootstrap.min.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

Re: CORS Issue on REST WebService

PostPosted:Sat May 08, 2021 5:34 pm
by jllort
I do not understanding why are you sharing the web.xml file?
Can you try using one of the SDK's ( JAVA, NET or PHP )? -> here you'll find information about them https://docs.openkm.com/kcenter/

Re: CORS Issue on REST WebService

PostPosted:Fri Jul 16, 2021 3:14 pm
by guilleinti
I really dont understand what's causing the error :?

Re: CORS Issue on REST WebService

PostPosted:Sun Jul 18, 2021 10:50 am
by jllort
Hi guilleinti are you getting an issue accessing from webservices?