1 回答
TA贡献1831条经验 获得超10个赞
根据@dur 评论,我将代码更改为使用 SecurityContextHolder 并获取当前线程上下文授权。
我现在的组件:
@Component
public class AuthRequestInfo {
private AuthorizationService authorizationService;
public AuthRequestInfo(AuthorizationService authorizationService) {
this.authorizationService = authorizationService;
}
public Authorization getAuthorization(){
Claims claims = (Claims)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String key = claims.get(KEY_CLAIM).toString();
return this.authorizationService.findByExampleOptional(Authorization.builder().key(key).build())
.orElseThrow(new UnauthorizedException(INVALID_TWOFACE_AUTHORIZATION_HEADER));
}
}
SecurityContextHolder 身份验证是在 GenericFilterBean 上设置的。现在我没有问题。
添加回答
举报