I love Vaadin because of the intergrated login/logout-flow, but I’m always struggling to configure Spring Security for extended use-cases…
In the app I’m working on, I also need a few public REST controllers to handle POST requests. Whatever I try, they always block on 403, while GET requests work perfectly. Any idea what is wrong with my Spring Security configuration?
Spring Boot 3.2.5
Vaadin 24.3.12
@EnableWebSecurity
@Configuration
public class SecurityConfiguration extends VaadinWebSecurity {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(
new AntPathRequestMatcher("/api/**"),
new AntPathRequestMatcher("/images/**")
).permitAll());
// Icons from the line-awesome addon
http.authorizeHttpRequests(authorize -> authorize
.requestMatchers(new AntPathRequestMatcher("/line-awesome/**/*.svg")).permitAll());
super.configure(http);
setLoginView(http, LoginView.class);
}
}
I already tried with adding .csrf(AbstractHttpConfigurer::disable) and @PermitAll on the REST controller.
Did you try to set org.springframework.security logger category to DEBUG level? It might provide additional hints to understand who is blocking the request.
Thanks a lot, indeed this is the correct configuration I was looking for. You made my day :-) Because of deprecated warnings related to Spring Security 6.1 it was not clear exactly what I needed to use…
Vaadin does provide its own CSRF mechanism - so the one of Spring is literally useless in a Vaadin application. Only time it’s needed is with a native login form or additional JSP or other files served next to Vaadin (but nobody should do this anyway).
Hi @ecohen280,
I get your points about security, but in this particular case I partially agree:
in the initial post, the /api/ endpoints were already declared public
the posted logs clearly state that the issue was an invalid CSRF token for a POST request on /api/callback, not a missing authentication
the CSRF is disabled only for the APIs (and for Vaadin internal requests that, how @knoobie said, are already protected by Vaadin mechanism).
Whether API should be protected or not depends on the application, so only @frank.63 knows
Maybe they should, but the posted configuration was only a first POC of making Spring Controllers work together with Vaadin.
Indeed I know I shouldn’t disable csrf but that’s the only solution I found now… And my APIs don’t need authentication (yet), there are only which will provide public info only.
Anyhow, that being said… It’s not the first time I hit the “Vaadin+Spring security wall” and there are many like me. So I guess this topic needs some love, either with better examples and documentation or a different implementation…