如何解决Vaadin 14 和 Push:在请求线程外访问 Spring SecurityContext 和身份验证
我遇到了和这个人一样的问题: https://vaadin.com/forum/thread/3383122/17008971
从服务器接收状态时,我尝试通过客户端上的推送(使用 ui.access)刷新内容。该内容需要当前校长的信息。
final SecurityContext securityContext = SecurityContextHolder.getContext();
final Authentication authentication = securityContext.getAuthentication();
这个
authentication
返回空值。
他使用 Vaadin Shared Security 解决了这个问题,但我找不到任何名为 Vaadin Shared Sec 的存储库或库。有没有其他方法可以解决这个问题?
解决方法
为什么不直接在视图构造函数中获取身份验证详细信息并将它们设置为类范围,或者让一个 bean 来执行此操作并设置其值?例如
@Route("some-view")
public class SomeView extends VerticalLayout {
Authentication authentication;
private void doHeavyStuff() {
try {
Thread.sleep(1500);
} catch (InterruptedException ex) {
// ignore
}
}
public SomeView() {
authentication = SecurityContextHolder.getContext().getAuthentication();
final Button button = new Button("Click me",e -> {
Notification.show("CLICKED");
getUI().ifPresent(ui -> {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
doHeavyStuff();
ui.access(() -> {
Notification.show("Calculation done");
});
});
});
});
add(button);
// simple link to the logout endpoint provided by Spring Security
Element logoutLink = ElementFactory.createAnchor("logout","Logout");
getElement().appendChild(logoutLink);
}
}
我在本教程中基于此答案(也对其进行了测试),如果您想了解更多信息: https://vaadin.com/learn/tutorials/securing-your-app-with-spring-security/speciale
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。