【发布时间】:2016-11-19 16:10:04
【问题描述】:
我正在尝试将过期时间设置为这样的 jwt 令牌:
class JsonWebToken
def self.encode(payload)
payload[:exp] = (2).minutes.from_now.to_i #expire in 2 minutes
JWT.encode(payload, Rails.application.secrets.secret_key_base)
end
def self.decode(token)
return HashWithIndifferentAccess.new(JWT.decode(token, Rails.application.secrets.secret_key_base)[0])
rescue
nil
end
end
但是当我尝试访问 url 时,令牌始终有效。此外,如果我解码令牌,我永远不会得到 exp 键:哈希值。
任何建议
更新
我正在使用jwt gem
这就是我验证用户的方式。
def authenticate_user
user = User.find_for_database_authentication(email: params[:email])
if user.valid_password?(params[:password])
render json: payload(user)
else
render json: {errors: ['Invalid Username/Password']}, status: :unauthorized
end
end
private
def payload(user)
return nil unless user and user.id
{
auth_token: JsonWebToken.encode({user_id: user.id}),
user: {id: user.id, email: user.email}
}
end
使用 curl 的示例:
curl -X POST -d email="a@a.com" -d password="changeme" http://localhost:3000/auth_user
这个卷曲返回:
{"auth_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxfQ.wPPX7T6WJ5K8ucjZF_l8-9mG7IzabcusLeWw1UOhhTM","user":{"id":1,"email":"a@a.com"}}
然后在我的 Rails 控制台上:
JWT.decode("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxfQ.wPPX7T6WJ5K8ucjZF_l8-9mG7IzabcusLeWw1UOhhTM", Rails.application.secrets.secret_key_base)
然后得到:
[{"user_id"=>1}, {"typ"=>"JWT", "alg"=>"HS256"}]
您可以看到令牌始终有效,即使我在此行设置了过期时间:
def self.encode(payload)
payload[:exp] = (2).minutes.from_now.to_i #expire in 2 minutes <<--- This one
JWT.encode(payload, Rails.application.secrets.secret_key_base)
end
【问题讨论】:
-
您使用的是什么 JWT gem?您能否分享一个创建令牌然后对其进行解码并且过期无效的最小示例? (例如,你可以使用上面的类。)
-
@smarx 刚刚为您更新了我的问题。提前致谢
-
您还没有分享一个显示问题的最小示例。例如,某处有代码解析出
Authorization标头并(大概?)解码 JWT 并应该拒绝带有无效令牌的请求,但我在您共享的代码中看不到任何类似的东西。我所能做的就是说 JWT gem 似乎没有被破坏,所以你的代码中的问题出在其他地方。 -
@smarx 请看我的 curl 示例。您的代码有效,但我不明白为什么我的代码无效
-
我的假设:这实际上不是您为创建令牌而运行的代码。尝试添加一些日志记录?
标签: ruby-on-rails ruby token jwt