Migrate to latest Spring Security - Update matcher methods. Part 2

Migrate to latest Spring Security - Update matcher methods. Part 2

Sergey Tech

1 год назад

656 Просмотров

We're going to update HttpSecurity matcher methods as well as some other parts that were deprecated or/and remove in the previous Spring Versions

👋🏻Connect with me:
Website: https://skryvets.com
Twitter: https://twitter.com/skryvets
Github: https://github.com/skryvets
LinkedIn: https://www.linkedin.com/in/skryvets

SUBSCRIBE TO MY CHANNEL: https://www.youtube.com/@kryvets-media?sub_confirmation=1 ❤️

Chapters:

0:00 Introduction
0:25 Sample Project Overview
1:20 @Configuration
1:29 Update build file
2:03 HttpSecurity antMatchers update
2:26 authorizeHttpRequests update
2:38 authorizeHttpRequests antMatcher update
2:55 requestMatcher uses ant/mvc matcher
3:44 Unexpected behaviour
4:54 Fallback to old antMatcher style
5:39 HttpSecurity Lambda DSL
6:59 End
Ссылки и html тэги не поддерживаются


Комментарии:

@alexandersmirnov4274
@alexandersmirnov4274 - 17.06.2023 19:35

helpful!!

Ответить
@tomaszvinys2389
@tomaszvinys2389 - 20.06.2023 23:35

Hey, that's almost exactly what I had been looking for ;D
Maybe you could explain the details on why do we need that AntMatcher for the second path we want to open (in this case - "h2-console/**"). Why can't we just use the .requestMathers? Or ... also the .requestMathers(/*accept multiple parameters here*/); which I tried and it also didn't work, so what's the point of multiple parameters?...
Thanks for valuable info!

Ответить
@zimsbert
@zimsbert - 30.09.2023 01:26

Found out migration from Spring Boot 2.* to 3.* the hard way just yesterday. I migrated a multi-project gradle project yesterday. It took me just a few minutes to migrate the EE stuff like javax.* to jakarta.* and leveling up spring boot 2.7.15 to 3.1.4. Everything was fine except Spring Security :( I was lucky because I already migrated the "old" extending WebSecurityConfigurerAdapter to the new @Bean version beforehand. The real hard point was authentication. We have an SPA with Spring as an API backend with persistent sessions (yes, we need it that way). I had to figure out how to migrate SecurityContextHolder.getContext().setAuthentication(authentication); to Spring Boot 3.* It turned out, that I had to provide a Bean for SecurityContextRepository and access it where we had the setAuthentication() part. And there, additionally, I had to inject the SecurityContextRepository and save the SecurityContext explicitly.

The second (and third) part was, that the SPA (Angular) provided the CSRF token (out-of-the-box with a pre-defined module) in raw format and not in Base64 format, as it is expected in Spring Security now. So I had to further specify the XorCsrfTokenRequestAttributeHandler in WebSecurityConfig (filterChain) csrfTokenRequestHandler(requestHandler::handle). We also required that for WebSocket connections, which is another part of Spring (WebSocket) Security.

That was quite hard but straight-through at the end, when I think of it in the past.

The "secret" behind Spring is having the know-how of how to do complex stuff. Then, the Framework will assist you with not requiring any boilerplate code or at least a minimum amount of it. And the real secret is how to not write code that could brake Spring stuff and read the docs really really carefully. But: Who really has the time to figure out _all_ the docs in a daily dev life during workdays?

So I talked about "the hard way" in the beginning. It was not as hard as it may be read here. I invested 1 full work day yesterday and I will have 1 work day more to invest for dev testing on Monday. ;) I had more complex migrations in the past.

But, and that's the point, Sergey, you point out things as straight and effective as human could do! Perfect! You should be standing in front of the camera as official Spring Boot migration, Spring Security and attacks-like-csrf-mitigating guy, directly sponsored by the Pivotal team! Perfect videos!!

Ответить
@VictorPalmero-v7e
@VictorPalmero-v7e - 11.10.2023 21:10

Good video man!!!

Ответить
@4khdfreestockfootagenocopy135
@4khdfreestockfootagenocopy135 - 16.07.2024 18:20

I hope you will make more videos. waiting for it.

Ответить