How is Seesion used in C + + server?

Keywords: Session Redis OpenSSL

How is Seesion used in C + + server?

The two mechanisms of cookie and session are introduced and used in the official account (CPP background server public view), but it seems that we seldom encounter in the C++ background development process.

How is session used in our server?

First, let's look at a requirement:

  • After the first login, when the customer logs in again, they want to use quick login or one click login. For example, we can use fingerprint login to get our account information

According to this requirement, we make a solution, and we can use the idea of session in the bottom implementation;

 

The plan:

Explain:

  • When users log in quickly, search redis according to the ID code of quick login

  • If it does not exist in redis, the session is created and bound with the user's id; if it exists, the login is successful and relevant functions are called to display the user information

  • In general, every company will have its own scheme for session generation, which can improve security. Here, the native MD5 interface is used

 

As for the design of redis key value pairs, it is generally relatively simple. It is suggested that you can design a set by yourself and implement this function;

Here, we briefly show the generation of sessionid:

#pragma once

#include <iostream>
#include <openssl/md5.h>
#include <string.h>

using namespace std;

class Md5
{
public:
    Md5();
    ~Md5();
    bool SetMd5(string data);
    unsigned char* GetMd5();
private:
    MD5_CTX ctx;
    unsigned char outMd5[16];
};
#include "Md5.h"

Md5::Md5()
{

}

Md5::~Md5()
{

}

unsigned char* Md5::GetMd5()
{
    //Array initialization
    memset(outMd5,0x00,sizeof(outMd5));
    int res = MD5_Final(outMd5,&ctx);
    if(res != 1)
    {
        cout<<"Md5_Final is errpr"<<endl;
    }
    return outMd5;
}

bool Md5::SetMd5(string data)
{

    //Initialize Md5
    MD5_Init(&ctx);
    //Calculate Md5
    int res = MD5_Update(&ctx,data.c_str(),5);
    if(res != 1)
    {
        cout<<"Md5_Update is errpr"<<endl;
        return false;
    }
    return true;
}
#pragma once
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "string.h"
#include "Md5.h"

using namespace std;

class Session
{
public:
    Session();
    ~Session();
    Session(string UserName,int ID);
    bool SetId();
    int GetId();
    bool SetUserName();
    string GetUserName();
    bool SetSessionId();
    bool SetSessionData();
    string GetSessionData();
    unsigned char* GetSessionId();
private:
    string name;
    int id;
    string SessionData;
    Md5 md5;
};
#include "session.h"

Session::Session()
{

}
Session::~Session()
{

}

Session::Session(string UserName,int ID)
{
    this->id = ID;
    this->name = UserName;
}

int Session::GetId()
{
    return this->id;
}

string Session::GetUserName()
{
    return this->name;
}

bool Session::SetSessionData()
{
    char str[20];
    memset(str,0,sizeof(str));
    //Here, name+id is used to generate the final sessionid
    sprintf(str,"%d",GetId());
    SessionData = GetUserName()+str;
    return true;

}

string Session::GetSessionData()
{
    if(!SessionData.empty())
        return SessionData;
}

unsigned char* Session::GetSessionId()
{
    return md5.GetMd5();
}

bool Session::SetSessionId()
{
    bool res = md5.SetMd5(GetSessionData());
    if(!res)
        return false;
    return true;
}
#include "session.h"

int main()
{
    unsigned char* str = new unsigned char[16];
  
    Session session("test",10);
    session.SetSessionData();
    session.SetSessionId();
    str = session.GetSessionId();
    for(int i=0;i<16;i++)
    {
        printf("%02X",str[i]);
    }
    printf("\n");
    return 0;
}
CXX = g++ -std=c++11 
CFLAG = -g -lssl -lcrypto

target = test
OBJ = Md5.cpp main.cpp session.cpp

$(target):$(OBJ)
 $(CXX) -o $@ $^ $(CFLAG)

clean:
 rm -f $(target)

To learn more about C++ background server, please pay attention to: WeChat official account: ====CPP backstage server development = = = =

Reminder

If you like this article, please share it with your friends. For more information, please follow me.

Scan code concern

More exciting

 

 

197 original articles published, praised 68, visited 70000+
Private letter follow

Posted by neveriwas on Sat, 22 Feb 2020 20:09:54 -0800