在 Vaadin 的请求中包含 CSRF 令牌

问题描述 投票:0回答:1

在 Vaadin 的网站上,他们声明:

客户端和服务器之间的所有请求都包含在用户会话特定的 CSRF 令牌中

但是,我无法以编程方式获取此令牌。我试过

System.out.println(VaadinRequest.getCurrent().getAttribute("_csrf"));

System.out.println(VaadinRequest.getCurrent().getHeader("X-CSRF-TOKEN"));

为了以某种方式查看请求是否确实包含此令牌。在这两种情况下,返回值都是

null

在我的

SecurityConfig.java
中,我已禁用 CSRF 令牌,因为如果启用它,Vaadin 就会崩溃。我认为可能存在一些重叠。

我也有

SecurityVaadinConfig.java
,这是非常默认的,因为它看起来像这样:

@Configuration
@EnableWebSecurity
public class SecurityVaadinConfig extends VaadinWebSecurity {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((auth) -> auth
                .requestMatchers(new AntPathRequestMatcher("/**")).permitAll());
        super.configure(http);
        setLoginView(http, LoginView.class);
    }

    @Override
    protected void configure(WebSecurity web) throws Exception {
        super.configure(web);
    }
}

如何确保“客户端与服务器之间的所有请求”中确实传递了这个token?

java vaadin
1个回答
0
投票

好的。我在两年前就发现了这个问题(https://github.com/vaadin/web-components/issues/201)。 CSRF 令牌作为页面的元标记传递。我能够通过

UI.getCurrent().getCsrfToken()
检索并确保 CSRF 已启用、CSRF 令牌。

© www.soinside.com 2019 - 2024. All rights reserved.