我正在学习本教程 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/