【发布时间】:2022-01-09 09:52:34
【问题描述】:
我有一个 Spring Boot 应用程序,它需要在 HTTP Authorization 标头中传递一个 JwtAuthenticationToken。标头本身提供了不记名令牌; Spring 正在做一些我目前不知道的将承载令牌字符串转换为JwtAuthenticationToken 的魔术。我有一些代码可以从令牌中提取用户 ID,用于在服务器上定位正确的资源。
private String getUidFromToken(JwtAuthenticationToken token) {
// this is placeholder code to demonstrate what I'm doing with the token
}
public ResponseEntity<String> getUserProfile(JwtAuthenticationToken token) {
String uid = getUidFromToken(token);
// rest of the code
}
由于我在几个不同的地方需要这些令牌,我决定考虑将getUidFromToken 代码移动到HandlerMethodArgumentResolver。我遇到的问题是我需要一个JwtAuthenticationToken,但我只从Authorization 标头中获取承载令牌作为字符串。
我是否可以得到 JwtAuthenticationToken 而不是字符串?
【问题讨论】:
标签: java spring-boot spring-security