java - Spring security 阻止从后台线程导航到另一个 View

标签 java spring vaadin vaadin-flow vaadin23

我在从 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/

相关文章:

java - 在 netbeans 上运行项目时选择 "Remember permanently"后尝试更改服务器

vaadin - 在 Cuba 框架中对嵌套的 collectionDatasource 进行排序

java - Selenium WebDriver 找不到 WebElements

java - 使用 itext 将多文本添加到现有 pdf

java - Spring Boot JPA Lazy Fetch 不起作用

java - 在 java webapp 中哪里放置可写属性文件?

java - 线程 "main"javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException 中的异常:找不到受信任的证书

java - Intellij Idea 中的 Github 克隆

java - 避免与数据对象重复

java - 如何通过过滤计算 Vaadin 8 网格页脚中的总数