FilterRegistrationBean is deprecated in Spring Boot 1.4.*

21,662

Solution 1

It has been moved to another package: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/FilterRegistrationBean.html

Solution 2

Check the javadoc. It was just moved to the package org.springframework.boot.web.servlet

Share:
21,662
pik4
Author by

pik4

I used to say that I am a Scala/Java/Python enthusiast working on fast-data project and bla bla bla. After some years, I realized that I am none of them. I am just a continuous learner with a strong background in Software Engineering. I ‘ve participated in a lot of projects and after 7-8 years I would say that my power is turning chaos into software. It had happened to me 3-4 times to hold a bomb ready to explode and try to disable it. If you ‘ve ever played the game “Keep talking and nobody explodes” then, yes, I am the guy who disables the bomb by collaborating and orchestrating my teams.

Updated on July 09, 2022

Comments

  • pik4
    pik4 almost 2 years

    I upgraded Spring Boot dependency in my project and I realized that some classes, such as FilterRegistrationBean, are deprecated.

    Do you know how can implement a Filter in Spring Boot 1.4.1?

    Bean of Filter

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        AuthenticationFilter f = new AuthenticationFilter();
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(f);
        registrationBean.addInitParameter("defaultToken", defaultToken);
        registrationBean.addInitParameter("secretKey", secretKey);
        ArrayList<String> match = new ArrayList<>();
        match.add("/users/*");
        registrationBean.setUrlPatterns(match);
        return registrationBean;
    }
    

    My Filter code:

    public class AuthenticationFilter implements Filter {
    
    private String defaultToken;
    private String secretKey;
    
    private UserSessionTokenRepository userSessionTokenRepository;
    private UserManager userManager;
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        defaultToken = filterConfig.getInitParameter("defaultToken");
        secretKey = filterConfig.getInitParameter("secretKey");
        userSessionTokenRepository = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()).getBean(UserSessionTokenRepository.class);
        userManager = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext()).getBean(UserManager.class);
    }
    
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        chain.doFilter(request, response);
    }
    
    public void addHeaders(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    }
    
    public void destroy() {
    }
    }