Rails 7集成devise_token_auth 1.2.5:登录成功后返回401未授权
问题:修改devise_token_auth的token_lifespan为1周后触发401未授权错误
环境信息
- Rails 7
- devise_token_auth 1.2.5,此前默认配置运行完全正常
相关代码
会话控制器
class Api::V1::DeviseTokenAuth::SessionsController < ::DeviseTokenAuth::SessionsController protect_from_forgery with: :null_session, only: Proc.new { |c| c.request.format.json? } before_action :authenticate_user!, except: [ :create, :validate_token] end
用户模型
用户模型中已包含:include DeviseTokenAuth::Concerns::User
修改操作
为了将token生命周期从默认2周改为1周,在config/initializers/devise_token_auth.rb中添加配置:
config.token_lifespan = 1.week
问题现象
- 用户调用登录接口
POST /api/v1/auth/sign_in/返回200 OK,但后续调用GET /api/v1/auth/validate_token/时直接返回401 Unauthorized - 移除
config.token_lifespan = 1.week配置后,所有功能恢复正常 - 尝试用秒数设置
config.token_lifespan = 604800.seconds,同样出现401错误 - 若将token生命周期改为
3.weeks则运行正常,似乎只要生命周期低于默认的2周就会触发该问题,其他配置均未改动
错误日志
Started POST "/api/v1/auth/sign_in/" Processing by Api::V1::DeviseTokenAuth::SessionsController#create as JSON Completed 200 OK in 228ms (Views: 0.6ms | ActiveRecord: 21.5ms | Allocations: 704380) Started GET "/api/v1/auth/validate_token/" Processing by DeviseTokenAuth::TokenValidationsController#validate_token as JSON Filter chain halted as :authenticate_user! rendered or redirected Completed 401 Unauthorized in 1ms (Views: 0.1ms | ActiveRecord: 0.4ms | Allocations: 1365)
怀疑自己遗漏了某些必要配置,求指出问题所在?
内容的提问来源于stack exchange,提问作者swingfuture
相关产品推荐
相关产品推荐

