我在从 Vaadin 14 迁移到 Vaadin 23 时遇到问题。我有一个长时间运行的带有进度对话框的后台任务。使用 Vaadin 14 完成任务后,我可以使用以下代码导航到结果 View :
UI ui = UI.getCurrent();
final SecurityContext context = SecurityContextHolder.getContext();
ui.access(() -> {
SecurityContextHolder.setContext(context);
UI.navigate("work-finished-view");
});
使用 Vaadin 23 我收到此警告
(ViewAccessChecker.java:147) Preventing navigation to com.vaadin.flow.router.RouteNotFoundError because no HTTP request is available for checking access.
这是com.vaadin.flow.server.auth.ViewAccessChecker
中的代码
VaadinServletRequest vaadinServletRequest = VaadinServletRequest.getCurrent();
if (vaadinServletRequest == null) {
// This is in a background thread and we cannot access the request
// to check access
getLogger().warn("Preventing navigation to " + targetView.getName()
+ " because no HTTP request is available for checking access.");
beforeEnterEvent.rerouteToError(NotFoundException.class);
return;
}
从后台线程导航到另一个 View 的正确方法是什么?
最佳答案
您需要创建一个使用安全上下文持有者中的值的虚假请求,并使用 CurrentInstance.set(VaadinRequest.class, request);
设置该请求。完全可行,但不是很好。
我没有在这里写一个又长又古怪的示例来说明如何完成它,而是花时间创建一个拉取请求,以便将来可以开箱即用:https://github.com/vaadin/flow/pull/13675
关于java - Spring security 阻止从后台线程导航到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72100244/