开源网站:https://jwt.io/
开源代码地址:https://github.com/pokowaka/jwt-cpp
由于cmake不熟悉,添加如下makefile进行编译验证
1 BASE_CPP = g++ 2 BASE_CC = gcc 3 CPPFLAGS += -std=c++11 -Wall -Isrc/include 4 LDFLAGS += -lcrypto 5 #Q:=@ 6 7 TARGET = sign validate rsaSign 8 TARGET_DIR ?= obj 9 TARGET_SRC_DIR = samples 10 11 SRC_DIRS = src/base64 src/jwt src/validators src/validators/claims 12 13 #$(info "--1---TARGET=[$(TARGET)] [$(TARGET:%=$(TARGET_DIR)/%.dpp)]") 14 15 .PHONY:all build clean 16 17 CPP_SRCS := $(CPP_SRCS) $(wildcard *.cpp) 18 C_SRCS := $(C_SRCS) $(wildcard *.c) 19 CPP_DEP_FILES = $(CPP_SRCS:%.cpp=$(TARGET_DIR)/%.dpp) $(TARGET:%=$(TARGET_DIR)/%.dpp) 20 C_DEP_FILES = $(C_SRCS:%.c=$(TARGET_DIR)/%.d) 21 #$(info "--1---CPP_DEP_FILES=[$(CPP_DEP_FILES)]") 22 23 ### search target src cpp dir #### 24 vpath %.cpp $(TARGET_SRC_DIR) 25 ifneq ($(SRC_DIRS),) 26 CPP_SRCS := $(CPP_SRCS) $(notdir $(wildcard $(SRC_DIRS:%=%/*.cpp))) 27 vpath %.cpp $(SRC_DIRS:%=% : ) 28 C_SRCS := $(C_SRCS) $(notdir $(wildcard $(SRC_DIRS:%=%/*.c))) 29 vpath %.c $(SRC_DIRS:%=% : ) 30 endif 31 32 OBJS = $(CPP_SRCS:%.cpp=%.o) $(C_SRCS:%.c=%.o) 33 TARGET_DIRS = $(TARGET_DIR) $($(dir $(CPP_SRCS)):%=$(TARGET_DIR)/%) $($(dir $(C_SRCS)):%=$(TARGET_DIR)/%) 34 OBJ_DEPS = $(OBJS:%.o=$(TARGET_DIR)/%.lo) 35 #$(info "--2---CPP_DEP_FILES=[$(CPP_DEP_FILES)]") 36 37 all: build 38 $(Q)echo "complete!!" 39 40 build: $(TARGET_DIR) $(TARGET) 41 42 $(sort $(TARGET_DIRS)): 43 mkdir -p $@ 44 45 ##### multi target rules ######## 46 $(TARGET):%:$(OBJ_DEPS) $(TARGET_DIR)/%.lo 47 $(Q)echo "Linking $(@F)" 48 $(Q)$(BASE_CPP) -o $@ $(TARGET_DIR)/$(@F).lo $(OBJ_DEPS) $(LDFLAGS) 49 50 $(TARGET_DIR)/%.lo: %.cpp 51 $(Q)echo "Compiling $<" 52 $(Q)$(BASE_CPP) -o $@ -c $< $(CPPFLAGS) -MMD -MP -MT $@ -MF $(basename $@).dpp 53 54 $(TARGET_DIR)/%.lo: %.c 55 $(Q)echo "Compiling $<" 56 $(Q)$(BASE_CC) -o $@ -c $< $(CFLAGS) -MMD -MP -MT $@ -MF $(basename $@).d 57 58 -include $(CPP_DEP_FILES) $(C_DEP_FILES) 59 60 clean: 61 $(Q)-rm -rf $(TARGET_DIR) 62 $(Q)-rm -f $(TARGET)
执行编译遇到如下错误
1 Compiling samples/validate.cpp 2 g++ -o obj/validate.lo -c samples/validate.cpp -std=c++11 -Wall -Isrc/include -MMD -MP -MT obj/validate.lo -MF obj/validate.dpp 3 In file included from /usr/include/c++/4.8.2/functional:55:0, 4 from /usr/include/c++/4.8.2/memory:79, 5 from src/include/jwt/allocators.h:25, 6 from src/include/jwt/jwt_all.h:25, 7 from samples/validate.cpp:2: 8 /usr/include/c++/4.8.2/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base() [with long unsigned int _Idx = 1ul; _Head = nlohmann::basic_json<>&]’: 9 /usr/include/c++/4.8.2/tuple:253:29: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl() [with long unsigned int _Idx = 1ul; _Head = nlohmann::basic_json<>&; _Tail = {}]’ 10 /usr/include/c++/4.8.2/type_traits:706:28: required from ‘struct std::__is_default_constructible_impl<std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&> >’ 11 /usr/include/c++/4.8.2/type_traits:116:12: required from ‘struct std::__and_<std::__not_<std::is_void<std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&> > >, std::__is_default_constructible_impl<std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&> > >’ 12 /usr/include/c++/4.8.2/type_traits:721:12: required from ‘struct std::__is_default_constructible_atom<std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&> >’ 13 /usr/include/c++/4.8.2/type_traits:742:12: required from ‘struct std::__is_default_constructible_safe<std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&>, false>’ 14 /usr/include/c++/4.8.2/type_traits:748:12: [ skipping 6 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ] 15 src/include/jwt/json.hpp:958:23: required from ‘constexpr const bool nlohmann::detail::has_from_json<nlohmann::basic_json<>, std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&>, void>::value’ 16 src/include/jwt/json.hpp:15380:45: required by substitution of ‘template<class T, class U> using get_template_function = decltype (declval<T>().get<U>()) [with T = const nlohmann::basic_json<>&; U = const std::_Tuple_impl<1ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&>]’ 17 src/include/jwt/json.hpp:15684:1: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul; _Head = nlohmann::basic_json<>&; _Tail = {nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&}]’ 18 /usr/include/c++/4.8.2/tuple:533:30: required from ‘constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = nlohmann::basic_json<>&; _T2 = nlohmann::basic_json<>&]’ 19 /usr/include/c++/4.8.2/tuple:1045:44: required from ‘std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>}]’ 20 samples/validate.cpp:19:33: required from here 21 /usr/include/c++/4.8.2/tuple:132:22: error: value-initialization of reference type ‘nlohmann::basic_json<>&’ 22 : _M_head_impl() { } 23 ^ 24 /usr/include/c++/4.8.2/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base() [with long unsigned int _Idx = 0ul; _Head = nlohmann::basic_json<>&]’: 25 /usr/include/c++/4.8.2/type_traits:706:28: required from ‘struct std::__is_default_constructible_impl<std::_Head_base<0ul, nlohmann::basic_json<>&, false> >’ 26 /usr/include/c++/4.8.2/type_traits:116:12: required from ‘struct std::__and_<std::__not_<std::is_void<std::_Head_base<0ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&, false> > >, std::__is_default_constructible_impl<std::_Head_base<0ul, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&, false> > >’ 27 /usr/include/c++/4.8.2/type_traits:721:12: required from ‘struct std::__is_default_constructible_atom<std::_Head_base<0ul, nlohmann::basic_json<>&, false> >’ 28 /usr/include/c++/4.8.2/type_traits:742:12: required from ‘struct std::__is_default_constructible_safe<std::_Head_base<0ul, nlohmann::basic_json<>&, false>, false>’ 29 /usr/include/c++/4.8.2/type_traits:748:12: required from ‘struct std::is_default_constructible<std::_Head_base<0ul, nlohmann::basic_json<>&, false> >’ 30 src/include/jwt/json.hpp:1156:44: [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ] 31 src/include/jwt/json.hpp:958:23: required from ‘constexpr const bool nlohmann::detail::has_from_json<nlohmann::basic_json<>, std::_Head_base<0ul, nlohmann::basic_json<>&, false>, void>::value’ 32 src/include/jwt/json.hpp:15380:45: required by substitution of ‘template<class T, class U> using get_template_function = decltype (declval<T>().get<U>()) [with T = const nlohmann::basic_json<>&; U = const std::_Head_base<0ul, nlohmann::basic_json<>&, false>]’ 33 src/include/jwt/json.hpp:15684:1: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul; _Head = nlohmann::basic_json<>&; _Tail = {nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>&}]’ 34 /usr/include/c++/4.8.2/tuple:533:30: required from ‘constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = nlohmann::basic_json<>&; _T2 = nlohmann::basic_json<>&]’ 35 /usr/include/c++/4.8.2/tuple:1045:44: required from ‘std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>, nlohmann::basic_json<std::map, std::vector, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long int, long unsigned int, double, std::allocator, nlohmann::adl_serializer>}]’ 36 samples/validate.cpp:19:33: required from here 37 /usr/include/c++/4.8.2/tuple:132:22: error: value-initialization of reference type ‘nlohmann::basic_json<>&’ 38 make: *** [obj/validate.lo] Error 1
经咨询C++牛人朋友凯歌是validate.cpp tie make_tuple 返回赋值 是C++14 才支持的特性,我当前用的编译器version 是4.8.2 还不支持,需要升级gcc版本,朋友推荐网址如下
https://www.vpser.net/manage/centos-6-upgrade-gcc.html
https://gcc.gnu.org/projects/cxx-status.html#cxx14
gcc升级到6.3.1-3后果然编译通过,非常感谢这位C++牛人
验证rsaSign添加如下代码
1 #include <iostream> 2 #include "jwt/jwt_all.h" 3 using json = nlohmann::json; 4 using namespace std; 5 6 const char *pubkey = 7 "-----BEGIN PUBLIC KEY----- " 8 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7ZveLF8NEMmjQHcnZwXX " 9 "f2Z2IL2e47t2oQvFO4AqkKIRp2aQ+t66cRFzzk7QBJKePkq9q9VFtapJCXcMKyp/ " 10 "CVToJP9WdF7uavt6e6p9a87ZcLhsXYwj6BdQT0+dkV8wppPCdN5miGxkn+Zi2BUP " 11 "41PIIu/s0EkSSnqkBp6FOKMWnxqZrE6ZApPZBgfrupHJ+UeskscfKXBSvfGdJDkb " 12 "JemFlMu38nkh1wnels+eRxs4IUbYlJjMmLnQa7N0mLUQSl3RJP+/VsYxE6EcshVD " 13 "J6jRatuaZilWrpzo2DXI6RoliqQ2b7XFyBthURxFh2ViKNTGj51KuEPder6yhJVS " 14 "6QIDAQAB " 15 "-----END PUBLIC KEY-----"; 16 17 const char *privkey = 18 "-----BEGIN RSA PRIVATE KEY----- " 19 "-----END RSA PRIVATE KEY-----"; 20 21 int main() { 22 // Setup a signer 23 RS256Validator signer(pubkey, privkey); 24 25 // Create the json payload that expires 01/01/2017 @ 12:00am (UTC) 26 // json payload = {{"sub", "subject"}, {"exp", 1483228800}}; 27 json payload = {{"installationParam", {{{"indoorDeployment", false},{"heightType", "AGL"},{"latitude", 68.53534},{"antennaModel", "External"},{"antennaGain", 0},{"longitude", -158.53972},{"height", 0}}}}, {"fccId","2AG32EG7010A"},{"cbsdSerialNumber","1202000240194DP0032"},{"professionalInstallerData", {{{"cpiId","cpi123"},{"cpiName","CPI_User_123"},{"installCertificationTime","2019-07-15T00:00:00Z"}}}}}; 28 29 // Let's encode the token to a string 30 auto token = JWT::Encode(signer, payload); 31 32 std::cout << token << std::endl; 33 }
生成如下token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjYnNkU2VyaWFsTnVtYmVyIjoiMTIwMjAwMDI0MDE5NERQMDAzMiIsImZjY0lkIjoiMkFHMzJFRzcwMTBBIiwiaW5zdGFsbGF0aW9uUGFyYW0iOlt7ImFudGVubmFHYWluIjowLCJhbnRlbm5hTW9kZWwiOiJFeHRlcm5hbCIsImhlaWdodCI6MCwiaGVpZ2h0VHlwZSI6IkFHTCIsImluZG9vckRlcGxveW1lbnQiOmZhbHNlLCJsYXRpdHVkZSI6NjguNTM1MzQsImxvbmdpdHVkZSI6LTE1OC41Mzk3Mn1dLCJwcm9mZXNzaW9uYWxJbnN0YWxsZXJEYXRhIjpbeyJjcGlJZCI6ImNwaTEyMyIsImNwaU5hbWUiOiJDUElfVXNlcl8xMjMiLCJpbnN0YWxsQ2VydGlmaWNhdGlvblRpbWUiOiIyMDE5LTA3LTE1VDAwOjAwOjAwWiJ9XX0.vXsgXZoLelOZMpmVlKTZeTDRReiiLcOUB5XEQrcYTxpGzT1hn3_5TGkjudsG4zavMuHfXLsIJfiPsJJrACfzu0kJUrYjJKGIGwotv05Sf-_deOFHZQgFX-qkrCaAEio6bcqGZltlDRJUjjubd13NMg-56HMEIOwDVssr4Gjq674v70XnpSxkTMHhpnob2p-B39nsWJWfplS278OgOdEcTGJdUfWhhCc9h4A9qttr3p07y3ri-o1C2lfSqpNMAMWTiJ8en1vHN7EwTaBv_aLP9NboKkW2nIiy8NcIp2q9rFL692DYPQ_yEgKtkGmO4MNkpMP7YB3wOF1CI7qQ-WbVHg
在jwt.io网址上验证可以通过。