Jul*_*nen 5 java spring-security vaadin spring-boot vaadin-flow
我得到了一个简单的 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());
}}
Run Code Online (Sandbox Code Playgroud)
执行此代码时,我收到以下消息。
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
Run Code Online (Sandbox Code Playgroud)
但我发现解决问题的一种方法是在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());
}}
Run Code Online (Sandbox Code Playgroud)
最后我得到了这个问题。为什么在第一个示例中丢失了 Spring SecurityContext 有没有更好的方法来解决这个问题,还是我坚持使用第二个示例?
作为旁注,我意识到 Vaadin 的上传组件也有同样的问题。SecurityContext 在addSucceededListener回调方法中丢失。
我正在使用 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
}
Run Code Online (Sandbox Code Playgroud)
问题是通过 StreamResource 或 Upload Component 动态创建的文件被映射到具有以下前缀的 url /VAADIN/dynamic/resource/**。在上面的配置中,我们告诉Spring安全性/VAADIN/**,以忽略所有的请求开始/VAADIN/。这导致 Spring Security 忽略所有指向动态创建的资源的 HttpServletRequest,因为 Vaadin 使用/VAADIN/dynamic/resource/**url 前缀映射它们。当 Spring Security 忽略 HttpServletRequest 时,它的 SpringSecurityContext 将为空。请参阅WebSecurity.ignoring()文档。
该问题可以通过重命名/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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |