我无法让 spring-security 显示我自己的登录页面。我已经尝试了几乎所有可用的解决方案。我不知道我的代码有什么问题。
下面是我已经尝试过的代码和代码中的注释部分。
SpringBoot 版本 - 2.1.5
安全配置类
@EnableWebSecurity
@ComponentScan
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService userDetailsService(){
return super.userDetailsService();
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception{
httpSecurity.
authorizeRequests()
.antMatchers("/resources/**","/login", "/home").permitAll()
.antMatchers("/user/**").hasRole("USER")
.and()
.formLogin().loginPage("/login").permitAll()
.defaultSuccessUrl("/user/dashboard")
.failureUrl("/login-error")
.and().logout().permitAll();
//////////// Tried this too /////////////////////////////////////
// httpSecurity.cors().disable()
// .csrf().disable()
// .httpBasic().disable()
// .authorizeRequests().antMatchers("/**").permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception{
authenticationManagerBuilder.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}
}
视图控制器类:
@Controller
@RequestMapping("/")
public class ViewController {
@Autowired
UserRepository userRepository;
@RequestMapping(value = {"/home"})
public String showHome(){ return "home.html";}
@GetMapping(value = "/login")
public String showLogin(){
return "login.html";
}
我希望 spring-security 禁用它自己的默认登录页面并显示我的登录页面。