使用 fastify 从 jwt-token 检索用户名
retrieve username from jwt-token using fastify
我可以创建一个 jwt 令牌:
fastify.post('/signup', (req, reply) => {
const token = fastify.jwt.sign({
payload,
})
reply.send({ token })
})
可以 return 类似于:
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjM3MDgyMzF9.HZqqiL7wwPaEQihUGoF7Y42Ia67HgKJ-1Ms38Nvcsmw"}
但是如果我尝试从令牌中解码用户名
fastify.get('/decode', async (request, reply) => {
const auth = request.headers.authorization;
const token = auth.split(' ')[1]
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info('username : ' + decoded.username)
reply.send({
foo: decoded,
})
})
})
响应是:
{"foo":{"iat":1523660987}}
这是满足您需要的工作示例。请注意您签署的内容:
const fastify = require('fastify')({ logger: true })
const fastifyJwt = require('fastify-jwt')
async function customJwtAuth(fastify, opts) {
fastify.register(fastifyJwt, { secret: 'asecretthatsverylongandimportedfromanenvfile' })
fastify.get('/signup', (req, reply) => {
const token = fastify.jwt.sign({ username: 'John Doo', hello: 'world' })
reply.send({ token })
})
fastify.get('/decode', async (request, reply) => {
const auth = request.headers.authorization;
const token = auth.split(' ')[1]
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info('username : ' + decoded.username)
reply.send({ foo: decoded })
})
})
}
fastify.register(customJwtAuth)
fastify.listen(3000)
curl http://localhost:3000/signup
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkpvaG4gRG9vIiwiaGVsbG8iOiJ3b3JsZCIsImlhdCI6MTU0OTg2ODk3MX0.T8kv8jbyp-3ianO8-CsfxZ5gePZG9PSjY8NvhdNV7uM"}
curl 'http://localhost:3000/decode' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkpvaG4gRG9v
IiwiaGVsbG8iOiJ3b3JsZCIsImlhdCI6MTU0OTg2ODk3MX0.T8kv8jbyp-3ianO8-CsfxZ5gePZG9PSjY8NvhdNV7uM'
{"foo":{"username":"John Doo","hello":"world","iat":1549868971}}
我可以创建一个 jwt 令牌:
fastify.post('/signup', (req, reply) => {
const token = fastify.jwt.sign({
payload,
})
reply.send({ token })
})
可以 return 类似于:
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1MjM3MDgyMzF9.HZqqiL7wwPaEQihUGoF7Y42Ia67HgKJ-1Ms38Nvcsmw"}
但是如果我尝试从令牌中解码用户名
fastify.get('/decode', async (request, reply) => {
const auth = request.headers.authorization;
const token = auth.split(' ')[1]
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info('username : ' + decoded.username)
reply.send({
foo: decoded,
})
})
})
响应是:
{"foo":{"iat":1523660987}}
这是满足您需要的工作示例。请注意您签署的内容:
const fastify = require('fastify')({ logger: true })
const fastifyJwt = require('fastify-jwt')
async function customJwtAuth(fastify, opts) {
fastify.register(fastifyJwt, { secret: 'asecretthatsverylongandimportedfromanenvfile' })
fastify.get('/signup', (req, reply) => {
const token = fastify.jwt.sign({ username: 'John Doo', hello: 'world' })
reply.send({ token })
})
fastify.get('/decode', async (request, reply) => {
const auth = request.headers.authorization;
const token = auth.split(' ')[1]
fastify.jwt.verify(token, (err, decoded) => {
if (err) fastify.log.error(err)
fastify.log.info('username : ' + decoded.username)
reply.send({ foo: decoded })
})
})
}
fastify.register(customJwtAuth)
fastify.listen(3000)
curl http://localhost:3000/signup
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkpvaG4gRG9vIiwiaGVsbG8iOiJ3b3JsZCIsImlhdCI6MTU0OTg2ODk3MX0.T8kv8jbyp-3ianO8-CsfxZ5gePZG9PSjY8NvhdNV7uM"}
curl 'http://localhost:3000/decode' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkpvaG4gRG9v IiwiaGVsbG8iOiJ3b3JsZCIsImlhdCI6MTU0OTg2ODk3MX0.T8kv8jbyp-3ianO8-CsfxZ5gePZG9PSjY8NvhdNV7uM'
{"foo":{"username":"John Doo","hello":"world","iat":1549868971}}