如何在Sonatype Nexus上启用CORS?

tim*_*lka 9 repository cross-domain nexus maven cors

我想使用AngularJS作为前端开发针对不同事物的Monitoring-WebApp。核心元素之一是Nexus工件/存储库的概述。当我请求REST-API时,出现以下错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:9090' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

要解决此错误,我需要修改响应标头以启用CORS。

如果有人熟悉这种类型的问题并可以给我答案,那就太好了!

Has*_*ash 4

CORS头存在于您尝试调用的系统的响应中。(这些在客户端进行检查[在本例中也称为浏览器],您可以在后端实现一个调用来进行这些调用,并且您可以忽略这些标头,但这可能会变得很难维护。)要更改那些您'将需要一个proxy. 所以你的应用程序不会像这样直接调用url

fetch("http://localhost:9090/api/sometest")
Run Code Online (Sandbox Code Playgroud)

至少有两种方法:一种是直接在声纳服务器之前添加代理并修改每个人的标头。出于安全原因,我真的不推荐这样做。:)

另一个更易于维护的解决方案是通过监控 Web 应用程序的本地域,如下所示:

fetch("/proxy/nexus/api/sometest")
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,您需要在运行应用程序的地方设置一个代理。这可以映射您所依赖的不同服务,并在必要时修改标头。

我不知道http server您要使用哪个应用程序,但这里有一些关于该主题的代理配置文档:

对于Apache HTTPD, mod_proxy您可以使用类似于以下的配置:

ProxyPass "/proxy/nexus/" "http://localhost:9090/"
ProxyPassReverse "/proxy/nexus/" "http://localhost:9090/"
Run Code Online (Sandbox Code Playgroud)

可能还需要使用 cookie,因此您可能需要查看以下配置:

对于Nginx 位置,您可以使用如下内容

location /proxy/nexus/ {
    proxy_pass http://localhost:9090/;
}
Run Code Online (Sandbox Code Playgroud)

对于node.js,请参阅文档: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);

};
Run Code Online (Sandbox Code Playgroud)