Seesion在C++服務(wù)端的使用方法
前面介紹了cookie和session兩種機(jī)制的產(chǎn)生和使用過(guò)程(可以關(guān)注 CPP后臺(tái)服務(wù)器 公眾號(hào)查看),但是,似乎在我們C++后臺(tái)開發(fā)過(guò)程中遇見(jiàn)的很少;
那session在我們服務(wù)端是怎么使用的呢?
首先,我們看一個(gè)需求:
客戶第一次設(shè)置登陸后,以后再次登陸的時(shí)候,想要使用快捷登陸或者是一鍵登陸,比如我們使用指紋登陸,即可獲取我們的賬戶信息
根據(jù)這個(gè)需求我們做一個(gè)方案進(jìn)行解決,底層實(shí)現(xiàn)我們可以使用session的思想;
方案:
說(shuō)明 :
- 用戶快捷登陸時(shí),根據(jù)快捷登陸的ID碼在redis中查找
- 如果再redis中不存在,則創(chuàng)建session,與用戶的id綁定;如果存在,則登陸成功,調(diào)用相關(guān)功能顯示用戶信息
- session的產(chǎn)生一般每個(gè)公司都會(huì)有自己改造的一套方案,這樣可以提升安全性,這里就使用原生的MD5接口
關(guān)于redis鍵值對(duì)的設(shè)計(jì),一般都比較簡(jiǎn)單,建議大家可以自己設(shè)計(jì)一套,并且實(shí)現(xiàn)這個(gè)功能;
這里,簡(jiǎn)單展示一下 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() { //數(shù)組初始化 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); //計(jì)算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)
補(bǔ)充知識(shí)點(diǎn):
session原理:
用戶使用瀏覽器第一次向服務(wù)器發(fā)送請(qǐng)求,服務(wù)器在接受到請(qǐng)求后,調(diào)用對(duì)應(yīng)的 Servlet 進(jìn)行處理。在處理過(guò)程中會(huì)給用戶創(chuàng)建一個(gè)session 對(duì)象,用來(lái)存儲(chǔ)用戶請(qǐng)求處理相關(guān)的公共數(shù)據(jù),并將此 session 對(duì)象的 JSESSIONID 以 Cookie 的形式存儲(chǔ)在瀏覽器中 (臨時(shí)存儲(chǔ),瀏覽器關(guān)閉即失效)。用戶在發(fā)起第二次請(qǐng)求及后續(xù)請(qǐng)求時(shí),請(qǐng)求信息中會(huì)附帶JSESSIONID,服務(wù)器在接收到請(qǐng)求后, 調(diào)用對(duì)應(yīng)的Servlet 進(jìn)行請(qǐng)求處理,同時(shí)根據(jù) JSESSIONID 返回其對(duì)應(yīng)的session 對(duì)象。
特點(diǎn):
由服務(wù)器進(jìn)行創(chuàng)建
每個(gè)用戶獨(dú)立擁有一個(gè)session
默認(rèn)存儲(chǔ)時(shí)間為 30 分鐘作用:
解決了一個(gè)用戶的不同請(qǐng)求的數(shù)據(jù)共享問(wèn)題。
使用:
創(chuàng)建Session 對(duì)象
存儲(chǔ)數(shù)據(jù)到session 對(duì)象獲取session 對(duì)象
獲取數(shù)據(jù)從session 對(duì)象
如果獲取session 中不存在的數(shù)據(jù)返回null。
總結(jié)
到此這篇關(guān)于Seesion在C++服務(wù)端的使用方法的文章就介紹到這了,更多相關(guān)session C++服務(wù)端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++利用PCL點(diǎn)云庫(kù)操作txt文件詳解
這篇文章主要為大家詳細(xì)介紹了C++如何利用PCL點(diǎn)云庫(kù)操作txt文件,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2024-01-01詳解C語(yǔ)言中free()函數(shù)與getpagesize()函數(shù)的使用
這篇文章主要介紹了詳解C語(yǔ)言中free()函數(shù)與getpagesize()函數(shù)的使用,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn)
自定義窗口控件的無(wú)邊框,窗口事件由于沒(méi)有系統(tǒng)自帶邊框,無(wú)法實(shí)現(xiàn)拖拽拉伸等事件的處理,本文主要介紹了Qt無(wú)邊框窗口拖拽和陰影的實(shí)現(xiàn),感興趣的可以了解一下2024-01-01Unreal學(xué)習(xí)之簡(jiǎn)單三角形的繪制詳解
之所以寫這個(gè)繪制簡(jiǎn)單三角形的實(shí)例其實(shí)是想知道如何在Unreal中通過(guò)代碼繪制自定義Mesh,如果你會(huì)繪制一個(gè)三角形,那么自然就會(huì)繪制復(fù)雜的Mesh了。所以這是很多圖形工作者的第一課,快跟隨小編一起學(xué)習(xí)起來(lái)吧2023-02-02完全掌握C++編程中構(gòu)造函數(shù)使用的超級(jí)學(xué)習(xí)教程
這篇文章主要介紹了C++中的構(gòu)造函數(shù),包括C++11標(biāo)準(zhǔn)中的新特性的介紹,十分推薦!需要的朋友可以參考下2016-01-01