简体   繁体   中英

How to access with OpenLayers a private layer from Geoserver?

I can use the following code to show public layers, but if it demands authentication, I can't (as expected).

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'
    }
  })
});

How can I authenticate via OpenLayers request? There's any way to exhibit the private layers or if I want to use OpenLayers I must let all my layers open? Thanks in advance.

I end up finding two solutions to this problem. One was point out by the Mike in the comments and the other one I get it in the Reddit .

Basically, in the first one, you have to change the imageLoadFunction to send in every requisition the username and the password in the header. There's a problem in this approach, though, the credentials will be available in the source code.

The other option is to deal with the Geoserver authentication in the server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM