【发布时间】:2021-09-06 20:03:43
【问题描述】:
我正在使用SpringBoot 2.2.6 和Angular 7。由于某些原因,我需要在前端没有转发任何呼叫的情况下通过后端调用前端。
代码流应该从应该调用Angular 服务(或其他东西)的后端REST Controller 开始,我已经读过我必须使用Socket 但我找不到反映的示例或教程我的需求。
你能给我一个正确的解决方案或教程吗?
------------------- 更新 ------
感谢您的所有回复,我尝试了不同的解决方案,但没有人按预期工作。我找到了以下TypeScript 代码来拦截来自Angular 的事件:
export class LoginComponent implements OnInit {
ngOnInit() {
this.connect();
}
connect(): void {
let source = new EventSource('http://localhost:8080/auth/streamsource');
source.addEventListener('message', function (event) {
let n: any;
n = JSON.parse(event.data);
console.log(event.data);
});
}
}
但是当 LoginComponent 被加载时,它会循环调用跟随后端端点:
@CrossOrigin
@GetMapping("/streamsource")
public Flux<String> streamEvents() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> "Flux - " + LocalTime.now().toString());
}
我也尝试过:
@CrossOrigin
@RequestMapping(path = "/streamsource", method = RequestMethod.GET)
public SseEmitter stream() throws IOException {
SseEmitter notifier = new SseEmitter();
notifier.send(SseEmitter.event().data("hello"));
return notifier;
}
结果相同.. 我想要实现的流程是:
- 客户开始(使用他自己的门户)一个 IDP 初始化的 SSO 并调用一个由
SpringSecurity过滤的端点 - 如果身份验证成功,Spring 将流程重定向到我指定的控制器
- 我想通过这个该死的控制器向我的前端发送一个令牌
有可能吗?
【问题讨论】:
-
初始连接总是由浏览器建立的……对吧..?
标签: java angular spring-boot websocket