Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Spring Boot 3.3.1 Migration: HandlerInterceptor and WebMvcConfigurer Replacements

Tech 1

Interceptor Changes in Spring MVC

The abstract class HandlerInterceptorAdapter was removed in Spring Boot 3.3.1. Earlier, you could extend this class; now you must implement the HandlerInterceptor interface directly.

Before (Spring Boot 2.x):

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import example.core.RequestContext;
import example.core.ContextHelper;
import example.core.ServiceValidator;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RequestTokenInterceptor extends HandlerInterceptorAdapter {
    private final ServiceValidator validator = new ServiceValidator();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        RequestContext ctx = ContextHelper.extractContext(request);
        String token = request.getHeader("X-Auth-Token");
        try {
            if (!validator.isValid(ctx, token)) {
                log.warn("Token validation failed for [{}]", ctx.getPath());
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return false;
            }
            return true;
        } catch (Exception ex) {
            log.error("Interceptor error", ex);
            return false;
        }
    }
}

After (Spring Boot 3.x):

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import example.core.RequestContext;
import example.core.ContextHelper;
import example.core.ServiceValidator;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RequestTokenInterceptor implements HandlerInterceptor {
    private final ServiceValidator validator = new ServiceValidator();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        RequestContext ctx = ContextHelper.extractContext(request);
        String token = request.getHeader("X-Auth-Token");
        try {
            if (!validator.isValid(ctx, token)) {
                log.warn("Token validation failed for [{}]", ctx.getPath());
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                return false;
            }
            return true;
        } catch (Exception ex) {
            log.error("Interceptor error", ex);
            return false;
        }
    }
}

Custom MVC Configuration

The WebMvcConfigurerAdapter class has also been removed. To customize Spring MVC configuration, implement the WebMvcConfigurer interface instead. Unlike the old adapter, this itnerface provides default methods, so you only need to oevrride what you need, and Spring Boot’s auto‑configuration remains active. Alternatively, you can extend WebMvcConfigurationSupport, but that disables auto‑configuration and requires manual setup of many defaults.

Before (Spring Boot 2.x):

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import example.formatting.TrimmingFormatter;
import example.formatting.DateFormatter;
import java.time.LocalDate;

@Configuration
public class FormatterConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldType(String.class, new TrimmingFormatter());
        registry.addFormatterForFieldType(LocalDate.class, new DateFormatter());
    }
}

After (Spring Boot 3.x):

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import example.formatting.TrimmingFormatter;
import example.formatting.DateFormatter;
import java.time.LocalDate;

@Configuration
public class FormatterConfig implements WebMvcConfigurer {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldType(String.class, new TrimmingFormatter());
        registry.addFormatterForFieldType(LocalDate.class, new DateFormatter());
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.