java - Vaadin 在 StreamResource 回调方法中缺少 SpringSecurityContext
问题描述
我有一个简单的 StreamResource 示例,当单击下载锚点时,SpringSecurityContext 神秘地消失了。基本上,当单击下载锚点时,createInputStream
会调用方法来创建下载文件,但是当执行此方法时,SecurityContext 为空。下面是重现该问题的简化示例。
public class HomeView extends VerticalLayout {
public HomeView() {
Anchor anchor = new Anchor();
anchor.add("DOWNLOAD");
anchor.setHref(new StreamResource("file", () -> createInputStream()));
add(anchor);
// SecurityContext returns correct value and is not null.
System.err.println(SecurityContextHolder.getContext());
System.err.println("Thread name in constructor : " + Thread.currentThread().getName());
}
private InputStream createInputStream() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write("text".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// SecurityContextHolder.getContext() returns null here. Why?
System.err.println(SecurityContextHolder.getContext());
System.err.println("Thread name in createInputStream() : " + Thread.currentThread().getName());
return new ByteArrayInputStream(outputStream.toByteArray());
}}
执行此代码时,我收到以下消息。
org.springframework.security.core.context.SecurityContextImpl@db426455: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@db426455: Principal: org.springframework.security.core.userdetails.User@983d0d8b...Rest omitted
Thread name in constructor : http-nio-8080-exec-4
org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication
Thread name in createInputStream() : http-nio-8080-exec-9
但我发现解决问题的一种方法是在createInputStream
方法中手动设置 SecurityContext。下面举个例子。
public class HomeView extends VerticalLayout {
SecurityContext context;
public HomeView() {
Anchor anchor = new Anchor();
anchor.add("DOWNLOAD");
anchor.setHref(new StreamResource("file", () -> createInputStream()));
add(anchor);
// Save Context to a variable
context = SecurityContextHolder.getContext();
System.err.println(SecurityContextHolder.getContext());
}
private InputStream createInputStream() {
// Set SecurityContext before accessing it.
SecurityContextHolder.setContext(context);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write("text".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// SecurityContextHolder.getContext() no longer returns null.
System.err.println(SecurityContextHolder.getContext());
return new ByteArrayInputStream(outputStream.toByteArray());
}}
最后我得到了这个问题。为什么在第一个示例中丢失了 Spring SecurityContext 是否有更好的方法来解决这个问题或者我是否坚持第二个示例?
作为旁注,我意识到 Vaadin 的上传组件也有同样的问题。addSucceededListener
SecurityContext 在回调方法中丢失。
我正在使用 Vaadin 13.0.1 和 Spring Boot 2.1.3。
解决方案
问题出在下面的示例中 Spring Security 的 WebSecurity 配置,该示例是 Vaadin 的 Bakery 应用示例的直接副本。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(
// Vaadin Flow static resources
"/VAADIN/**", // This was the problematic spot
//Rest of configuration omitted for simplicity
}
问题是通过 StreamResource 或 Upload Component 动态创建的文件被映射到具有以下前缀的 url /VAADIN/dynamic/resource/**
。在上面的配置中,我们告诉 Spring Security with忽略所有/VAADIN/**
以. 这导致 Spring Security 忽略所有指向动态创建的资源的 HttpServletRequest,因为 Vaadin 使用url 前缀映射它们。当 Spring Security 忽略 HttpServletRequest 时,它的 SpringSecurityContext 将为空。请参阅WebSecurity.ignoring()文档。/VAADIN/
/VAADIN/dynamic/resource/**
这个问题可以通过重命名/VAADIN/**
来解决/VAADIN/static/**
。这将防止 Spring Security 忽略对动态资源的请求,因此 SpringSecurityContext 将在 StreamResource 和 Upload 回调方法中可用。下面是一个工作示例。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(
// Vaadin Flow static resources
"/VAADIN/static/**",
//Rest of configuration omitted for simplicity
}
推荐阅读
- html - 如何摆脱 SVG 占用的空间以及为什么无法更改其大小?
- javascript - Mongoose Pagination - 在第一次查询时限制为 1,然后在后续查询中限制为 10
- javascript - 如何存储json下拉列表中的项目
- objective-c - 屏幕锁定时如何将数据发送到隐藏设备
- javascript - 使用参数时Nodejs中的类不返回预期值
- flutter - 如何在飞镖中制作超级构造函数
- c++ - 一起使用 new 和 & 符号
- java - PDF 垂直剪切并合并
- swiftui - SwiftUI - 开始/结束外观转换的不平衡调用
- reactjs - React Native Deck Swiper