Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable CORS on Sonatype Nexus?

I want to develop a Monitoring-WebApp for different things with AngularJS as Frontend. One of the core-elements is showing an overview of Nexus-Artifacts/Repositories. When I request the REST-API I'm getting following error back:

No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:9090' is therefore not allowed access.

To fix this error, I need to modify the response headers to enable CORS.

It would be great if anyone is familiar with that type of problem and could give me an answer!

like image 258
timschmolka Avatar asked Sep 04 '15 08:09

timschmolka


1 Answers

The CORS headers are present in the response of the system you are trying to invoke. (Those are checked on the client side [aka browser this case], you can implement a call on your backend to have those calls and there you can ignore those headers, but that could become quite hard to maintain.) To change those you'll need a proxy. So your application will not call the url directly like

fetch("http://localhost:9090/api/sometest")

There are at least two ways: one to add a proxy directly before the sonar server and modify the headers for everyone. I do not really recommend this because of security reasons. :)

The other more maintaneable solution is to go through the local domain of the monitoring web app as follows:

fetch("/proxy/nexus/api/sometest")

To achieve this you need to setup a proxy where your application is running. This could map the different services which you depend on, and modify the headers if necessary.

I do not know which application http server are you going to use, but here are some proxy configuration documentations on the topic:

For Apache HTTPD mod_proxy you could use a configuration similar to this:

ProxyPass "/proxy/nexus/" "http://localhost:9090/"
ProxyPassReverse "/proxy/nexus/" "http://localhost:9090/"

It is maybe necessary to use the cookies as well so you may need to take a look at the following configurations:

  • ProxyPassReverseCookiePath
  • ProxyPassReverseCookieDomain

For Nginx location you could employ something as follows

location /proxy/nexus/ {
    proxy_pass http://localhost:9090/;
}

For node.js see documentation: https://github.com/nodejitsu/node-http-proxy

module.exports = (req, res, next) => {

  proxy.web(req, res, {
    target: 'http://localhost:4003/',
    buffer: streamify(req.rawBody)
  }, next);

};
like image 149
Hash Avatar answered Sep 28 '22 02:09

Hash