如何使用 OpenLayers 从 Geoserver 访问私有层?

How to access with OpenLayers a private layer from Geoserver?

我可以使用以下代码来显示 public 层,但如果它需要身份验证,我不能(正如预期的那样)。

var untiled = new ol.layer.Image({
  source: new ol.source.ImageWMS({
    ratio: 1,
    url: 'http://url/geoserver/workspacename/wms',
    params: {
      'FORMAT': format,
      'VERSION': '1.1.1',
      STYLES: '',
      LAYERS: 'workspacename:layername'
    }
  })
});

如何通过 OpenLayers 请求进行身份验证?有什么方法可以展示私有层,或者如果我想使用 OpenLayers,我必须让所有层都打开吗?提前致谢。

我最终找到了解决这个问题的两个方法。 Mike in the comments and the other one I get it in the Reddit.

指出了一个

基本上,在第一个中,您必须更改 imageLoadFunction 以在每个申请中发送 header 中的用户名和密码。不过,这种方法存在一个问题,凭据将在源代码中提供。

另一种选择是在服务器端处理Geoserver认证。