生成token使用secret

This commit is contained in:
李运家 2024-05-22 18:03:45 +08:00
parent 2594ef6dd1
commit 3f23a66f62

View File

@ -28,7 +28,7 @@ fn generate(claim: Claim) -> String {
let token = jsonwebtoken::encode( let token = jsonwebtoken::encode(
&jsonwebtoken::Header::default(), &jsonwebtoken::Header::default(),
&claim, &claim,
&jsonwebtoken::EncodingKey::from_secret(b"secret"), &jsonwebtoken::EncodingKey::from_secret(config!().jwt.secret.as_bytes()),
); );
token.unwrap_or_else(|e| { token.unwrap_or_else(|e| {
tracing::error!(error =?e, "生成Token失败"); tracing::error!(error =?e, "生成Token失败");
@ -39,7 +39,7 @@ fn generate(claim: Claim) -> String {
pub fn verify_token(token: &str) -> Result<Claim, jsonwebtoken::errors::Error> { pub fn verify_token(token: &str) -> Result<Claim, jsonwebtoken::errors::Error> {
jsonwebtoken::decode::<Claim>( jsonwebtoken::decode::<Claim>(
token, token,
&jsonwebtoken::DecodingKey::from_secret(b"secret"), &jsonwebtoken::DecodingKey::from_secret(config!().jwt.secret.as_bytes()),
&jsonwebtoken::Validation::default(), &jsonwebtoken::Validation::default(),
) )
.map(|data| data.claims) .map(|data| data.claims)