Problem trying to @Override the addViewControllers() method – Spring Security Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Problem trying to @Override the addViewControllers() method – Spring Security without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m following this tutorial Part 5: Integrating Spring Security with Spring Boot Web trying to add the Spring Security functionality to my web page but I’m having heaps of config problems.

So I’ve got to this part where I need to @Override this method:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/login").setViewName("login");
}

But I’m not quite sure where to put the code. I’ve googled and found that most people put it in the class that extends the WebSecurityConfigurerAdapter but this is not working in my case, I’m getting an error saying that the method doesn’t override any method from its superclass.

This is my SecurityConfig that extends the WebSecurityConfigurerAdapter:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().anyRequest().authenticated()
            .antMatchers("/resources/**").permitAll()
            .anyRequest().permitAll();

        http
            .formLogin().failureUrl("/ingresar?error")
                        .defaultSuccessUrl("/")
            .loginPage("/ingresar").permitAll()
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                     .logoutSuccessUrl("/logout")
                     .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
}

Any idea ?? I’ve been trying for hours !

Answer

This mapping has nothing common with the security itself. It is just a controller definition to return a view.

You surely have a class implementing WebMvcConfigurer and annotated with @Configuration annotation. If not, create it. Include this method override up there.

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

   @Override
   public void addViewControllers(ViewControllerRegistry registry) {
       registry.addViewController("/login").setViewName("login");
   }
}

Note the class has a lot of default methods available to be overridden. The method WebMvcConfigurer::addViewControllers in surely there.

We are here to answer your question about Problem trying to @Override the addViewControllers() method – Spring Security - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji