Spring / Spring Boot 4 Basics Interview Questions
What is Spring Security in Spring Boot 4 and what are the key Boot 4 changes?
Spring Boot 4 ships with Spring Security 7, which introduces multi-factor authentication (MFA) and important changes to default security configurations -- particularly CSRF protection defaults that can silently break REST APIs.
// Spring Boot 4 / Spring Security 7: SecurityFilterChain @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // REST APIs: disable CSRF (Spring Security 7 default changed!) // Boot 3: CSRF enabled by default; REST APIs often disabled it // Boot 4 / Security 7: review CSRF defaults -- new SameSite-based protection .csrf(csrf -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) ) .authorizeHttpRequests(auth -> auth .requestMatchers("/actuator/health", "/api/public/**").permitAll() .requestMatchers("/api/admin/**").hasRole("ADMIN") .anyRequest().authenticated() ) .sessionManagement(session -> session .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // for JWT/OAuth2 ) .oauth2ResourceServer(oauth2 -> oauth2 .jwt(jwt -> jwt .decoder(JwtDecoders.fromIssuerLocation(issuerUri)) ) ); return http.build(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } // Spring Security 7: Multi-Factor Authentication // MFA is now a first-class concept in the security model // Configure in SecurityFilterChain with mfa() DSL // Previously required custom implementations
Key Spring Security 7 change for Boot 4: Spring Security 7 changes some CSRF and session management defaults. Teams migrating from Boot 3 should review their SecurityFilterChain configurations carefully -- previously-passing REST API tests may fail if CSRF defaults behave differently.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
