Problem trying to @Override the addViewControllers() method - Spring Security

Nacho Zve De La Torre :

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 !

Nikolas :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=129871&siteId=1