[GIS] Openlayers 3 getting 401 error from geoserver with secured layers

geoserveropenlayersSecurity

I have an OL3 webmap that is attempting to access services from Geoserver that is running on a server on our internal network that uses LDAP logins using windows credentials. I found this link (Accessing secure Geoserver layer with username password) and this link (https://stackoverflow.com/questions/10950747/authenticate-in-geoserver-with-asp-net-and-iis/13123640#13123640) and tried both but it is not resolving the 401 error issue.

This is the code I have from the second link:

//login function
function login (options) {
// url del servlet del geoserver
var url = options.server + "/geoserver/j_spring_security_check";
// parametros para el login
params = "username=" + options["user"] + "&password="
            + options["password"];

var contentType = "application/x-www-form-urlencoded";
//se inicializa la peticiĆ³n ajax
var ajax = $.ajax({
    data : params,
    type : "POST",
    contentType : contentType,
    url : url
});
// se ejecuta cuando la peticion finaliza
ajax.done(function() {

    if ($.cookie("JSESSIONID") != null && options && options.success) {
        options.success();
    }
});
// si ocurrio un error al realizar la peticion
ajax.fail(function(data) {
    if (options && options.failure) {
        options.failure(data);
    }
});
// se ejecuta siempre al final de la peticiĆ³n, sin importar que esta
// haya fallado
ajax.always(function() {
    if (options && options.always) {
        options.always();
    }
});
};
var un = prompt("enter your username","");
var pw = prompt("enter your password","");
login({
    user: un, //geoserver user  
    password: pw, 
    server : "http://(host server here):8080", //geoserver host
    success : function(){
        alert("Login OK!");
    },
    failure : function(){
        alert("Login fail!");
    }
});

Also, if I log into the geoserver admin webpage in a separate tab, I can then open the webmapping application with no issue. I would like to ideally remove this step by putting in the geoserver login into the actual webpage (and preferably not store the login credentials anywhere for security purposes).

Best Answer

I'm not familar with GeoServer, but I found this.

More generally spoken: HTTP-Status 401 means, that the server which receives your request is secured by "Basic Authentication" (hopefully via HTTPS). You have to put the credentials in the HTTP-Request Header "authorization" (this term is a bit misleading).

The content of header "authorization" looks like
Basic dXNlcjAxOnRvcFNlY3JldA==
The second word is the Base64 encoded presentation of user01:topSecret , with ':' as separator between username and password.

Related Question