问题描述
JWT 令牌的大多数示例都使用 clj-time,现在已弃用它以支持原生 java.time。我正在尝试使用 java-time 和 buddy 来签署/验证令牌,但我一直在尝试将 exp 声明传递给我的令牌。这是我所拥有的示例:
(ns test-app.test-ns
(:require
[buddy.sign.jwt :as jwt]
[buddy.auth.backends.token :as bat]
[java-time :as t]))
(def secret "myfishysecret")
(def auth-backend (bat/jws-backend {:secret secret
:options {:alg :hs512}}))
(def token (jwt/sign {:user "slacker"
:exp (t/plus (t/local-date-time) (t/seconds 60))
} secret {:alg :hs512}))
当我试图测试我是否可以取消令牌时
(jwt/unsign token secret {:alg :hs512})
我收到以下错误:
执行错误 (JsonGenerationException) 在 cheshire.generate/generate (generate.clj:152)。无法 JSON 编码 类对象:类 java.time.LocalDateTime: 2021-01-22T12:37:52.206456
因此,我尝试通过将 (t/plus ...) 的调用封装在 (str) 中来传递相同的内容,但随后出现此错误:
类 java.lang.String 不能转换为类 java.lang.Number
(java.lang.String 和 java.lang.Number 在模块 java.base 中
加载程序'引导程序')
所以,我被卡住了,因为我真的不知道如何使用 java-time 生成有效的 exp 数值(根据 this 问题,格式应该是自纪元以来的秒数)。 Older examples using clj-time 刚刚将 exp 声明值传递为
(clj-time.core/plus (clj-time.core/Now) (clj-time.core/seconds 3600))
非常感谢任何帮助。
编辑:Alan Thompson 的回答完美无缺,因为使用 java-time 包装器这将是等价的:
(t/plus (t/instant) (t/seconds 60))
解决方法
这里有两种方法:
(let [now+60 (-> (Instant/now)
(.plusSeconds 60))
now+60-unixsecs (.getEpochSecond now+60)]
(jwt/sign {:user "slacker" :exp now+60 } secret {:alg :hs512})
(jwt/sign {:user "slacker" :exp now+60-unixsecs} secret {:alg :hs512}))
我们有 now
结果:
now+60 => <#java.time.Instant #object[java.time.Instant 0x7ce0054c "2021-01-22T19:04:51.905586442Z"]>
now+60-unixsecs => <#java.lang.Long 1611342291>
因此您可以选择方法。看起来 buddy 知道如何从 java.time.Instant
转换,所以一直到 unix-seconds 是不必要的。
您可能还对这个 library of helper and convenience functions 感兴趣,以便与 java.time
合作。