mercredi 22 avril 2015

QT : Accessing secured resources only when login is complete via REST

I am working in a QT application in which I am accessing secured resources over REST. To access secured resources, I am initially logging-in and then saving the cookie in cookieManager. Once that is done, the manager is requesting via GET the secure object.

The problem I am currently facing is because the login is not complete, and I am requesting secure resource, I am getting Access Denied in the debug logs. Because of this I guess the request is getting repeated, and I am getting the ouput multiple times.

I want to only access secure resource when the login is successful while maintaining the contents of cookieManager and not make multiple requests. I tried to do that in SLOT when request is finished, but it goes in infinite loop.

Qt code :

void Person::loginAndAccess (){

    QString url_str = "http://localhost:8080/j_spring_security_check";
    QString canvasUrl = "http://localhost:8080/rest/jsonlist";

 QNetworkAccessManager *manager = new QNetworkAccessManager();
    manager->setCookieJar (new QNetworkCookieJar(manager));
    connect (manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(slotRequestFinished(QNetworkReply*)));
    QNetworkRequest request;
    request.setUrl (url_str);
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    QByteArray postData;
    postData.append ("j_username=email@email.de");
    postData.append ("&j_password=password");
    manager->post (request,postData);
// From here I am accessing secured resource
    request.setUrl (canvasUrl);
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    manager->get (request);
}

void Person::slotRequestFinished(QNetworkReply *reply)
{

    reply->deleteLater();

        if(reply->error() == QNetworkReply::NoError) {
            // Get the http status code
            int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            if (v >= 200 && v < 300) // Success
            {
                 // Here we got the final reply
                qDebug() << "Request was successful";
                QString replyText = reply->readAll();
                qDebug() << replyText;
                return;
            }
            else if (v >= 300 && v < 400) // Redirection
            {
                // Get the redirection url
                QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
                // Because the redirection url can be relative,
                // we have to use the previous one to resolve it
                newUrl = reply->url().resolved(newUrl);

                QNetworkAccessManager *manager = reply->manager();
                QNetworkRequest redirection(newUrl);
                QNetworkReply *newReply = manager->get(redirection);
                QString reply = newReply->readAll ();
                qDebug() << reply;

                return; // to keep the manager for the next request
            }
        }
        else
        {
            // Error
            qDebug() << reply->errorString();

        }

        reply->manager()->deleteLater();
}

Output in QT :

""
""
Request was successful
"
// REPLY from Server ommitted from output

"
Request was successful
""
Request was successful
"
// Again reply from server cut off

"
Request was successful
""
Request was successful
"

// So on and so-forth.

What am I doing wrong. I am not that much expert in QT, any help would be nice. Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire