spring - 在返回 SseEmitter 的方法中使用 @PreAuthorize 会返回 HTTP 406
问题描述
我正在使用 jwt 进行身份验证,我怀疑它与具有令牌的请求标头有关。
在不添加 @PreAuthorize 的情况下接收服务器发送的事件就可以了:
@GetMapping("/sse")
public SseEmitter serverSentEvent() throws IOException
{
SseEmitter emitter = new SseEmitter(2592000000L);
emitter.send("OK");
return emitter;
}
但是,当我添加 @PreAuthorize 以检查使用角色时,我得到 406 响应(无法在http://localhost:8080/sse建立与服务器的连接)
@GetMapping("/sse")
@PreAuthorize("hasRole('ADMIN')")
public SseEmitter serverSentEvent() throws IOException
{
SseEmitter emitter = new SseEmitter(2592000000L);
emitter.send("OK");
return emitter;
}
解决方案
显然,JWT 拦截器没有在 EventSource 请求的授权标头中添加令牌,因此我使用了允许在 EventSource 中自定义 HTTP 标头的ng-event-source 。
推荐阅读
- r - 从 R 中的 reddit 抓取表格
- typo3 - Typo3 9.5.0 中的真实 URL
- ios - 无法在 IOS 设备版本 12.0.1 上运行 Nativescript
- javascript - Enzyme/Jest:如何测试是否调用了模拟函数
- laravel - 在 Eloquent 中的 WHERE 子句中选择计数
- django - Django REST Framework - 无法通过序列化程序更新模型的字段
- php - 使用 if - PHP 在表中显示记录
- javascript - 反应条件渲染
- qt - Qt Creator 自定义构建步骤无法复制文件
- android - Android Kotlin 测试。lateinit 属性 _db 尚未初始化