Seesion在C++服務端是怎麼使用的?

Seesion在C++服務端是怎麼使用的?

Snipaste_2020-02-16_17-29-47.png前面介紹了cookie和session兩種機制的產生和使用過程(能夠關注CPP後臺服務器公衆號查看),可是,彷佛在咱們C++後臺開發過程當中碰見的不多;ios

那session在咱們服務端是怎麼使用的呢?c++

首先,咱們看一個需求:redis

  • 客戶第一次設置登錄後,之後再次登錄的時候,想要使用快捷登錄或者是一鍵登錄,好比咱們使用指紋登錄,便可獲取咱們的帳戶信息數組

根據這個需求咱們作一個方案進行解決,底層實現咱們可使用session的思想;

方案:安全

1.png2.png

說明bash

  • 用戶快捷登錄時,根據快捷登錄的ID碼在redis中查找服務器

  • 若是再redis中不存在,則建立session,與用戶的id綁定;若是存在,則登錄成功,調用相關功能顯示用戶信息微信

  • session的產生通常每一個公司都會有本身改造的一套方案,這樣能夠提高安全性,這裏就使用原生的MD5接口cookie


關於redis鍵值對的設計,通常都比較簡單,建議你們能夠本身設計一套,而且實現這個功能;session

這裏,簡單展現一下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()
{
    //數組初始化
    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)
{

    //初始化Md5
    MD5_Init(&ctx);
    //計算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));
    //這裏使用name+id的方式,生成最終的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)複製代碼

想了解學習更多C++後臺服務器方面的知識,請關注:微信公衆號:====CPP後臺服務器開發====

舒適提示

若是你喜歡本文,請分享到朋友圈,想要得到更多信息,請關注我。

掃碼關注

更多精彩

相關文章
相關標籤/搜索