java - 尝试@Override addViewControllers() 方法时出现问题 - Spring Security

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

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

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

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

但我不太确定将代码放在哪里。我用谷歌搜索发现大多数人将它放在扩展 WebSecurityConfigurerAdapter 的类中,但这在我的情况下不起作用,我收到一条错误消息说该方法没有覆盖任何方法来自它的父类(super class)。

这是我的 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");
    }
}

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

最佳答案

此映射与安全性本身没有任何共同之处。它只是一个返回 View 的 Controller 定义。

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

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

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

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

关于java - 尝试@Override addViewControllers() 方法时出现问题 - Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53786649/

相关文章:

java - 比较路径长度

java - 2 war 共享代码

spring - 我可以在 Spring EL 中使用属性占位符吗?

java - 在 weblogic 10.3.6 上访问 spring boot application.properties

java - 拥有多个字段的类的缺点

java - gRpc,客户端收到 io.grpc.StatusRuntimeException : UNIMPLEMENTED: Method even when the message is received and deserialized by the server

java - 从类路径资源解析 XML 时解析器配置异常

java - org.elasticsearch.transport.NodeDisconnectedException : [][inet[localhost/127. 0.0.1:9300]][cluster/nodes/info] 断开连接

google-app-engine - Jetty 无法在 appengine flexible 中启动 spring boot 应用程序

java - 即使设置了 spring.expression.compiler.mode,Spring SpEL 也不会编译