【问题标题】:Problem trying to @Override the addViewControllers() method - Spring Security尝试@Override addViewControllers() 方法时出现问题 - Spring Security
【发布时间】:2018-12-14 20:43:50
【问题描述】:

我正在关注本教程 Part 5: Integrating Spring Security with Spring Boot Web 尝试将 Spring Security 功能添加到我的网页,但我遇到了很多配置问题。

所以我已经到了需要@Override这个方法的部分:

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

但我不太确定将代码放在哪里。我用谷歌搜索发现大多数人把它放在扩展 WebSecurityConfigurerAdapter 的类中,但这在我的情况下不起作用,我收到一个错误,说该方法没有覆盖其超类中的任何方法。

这是我的SecurityConfig,扩展了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");
    }
}

有什么想法吗??我已经尝试了几个小时!

【问题讨论】:

    标签: java spring spring-boot spring-mvc spring-security


    【解决方案1】:

    这种映射与安全本身没有任何共同之处。它只是一个返回视图的控制器定义。

    您肯定有一个实现WebMvcConfigurer 并使用@Configuration 注释进行注释的类。如果没有,请创建它。在上面包含这个方法覆盖。

    @Configuration
    public class WebMvcConfiguration implements WebMvcConfigurer {
    
       @Override
       public void addViewControllers(ViewControllerRegistry registry) {
           registry.addViewController("/login").setViewName("login");
       }
    }
    

    注意这个类有很多默认方法可以被覆盖。方法WebMvcConfigurer::addViewControllers肯定在里面。

    【讨论】:

    • 谢谢...我没有任何实现 WebMvcConfigurer 的类...您知道任何好的教程吗?我要疯了...找不到有用的东西在那里
    • 如果你没有这个类,就创建它。查看Baledung's webpage - Securing a Web Application 并使用官方 Spring 文档。
    【解决方案2】:

    您应该有一个实现WebMvcConfigurer@Configuration 类。在那里你可以覆盖addViewControllers

    这将对您有所帮助:

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/login").setViewName("login");
        }
    
    }
    

    【讨论】:

    • 谢谢...我没有任何实现 WebMvcConfigurer 的类...您知道任何好的教程吗?我要疯了...找不到有用的东西在那里
    • @NachoZveDeLaTorre 只是创建它。创建一个具有@Configuration 的类,然后实现WebMvcConfigurer。 Spring 会自动为你配置
    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2021-10-12
    • 2022-12-17
    • 2011-10-04
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    相关资源
    最近更新 更多