2016-08-12 73 views
-2

我在一个非常简单的Spring Boot应用程序中登录后得到了一个404。发生这种情况是因为我在我的configureAuth方法中添加了密码编码器。有人能帮我吗?为什么我在登录后会弹出默认登录页面?

这里北京时间我的安全配置代码:

@Configuration 
@EnableGlobalAuthentication 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Autowired 
private DataSource dataSource; 

@Autowired 
public void configureAuth(final AuthenticationManagerBuilder auth) throws Exception { 
    auth.jdbcAuthentication().passwordEncoder(passwordEncoder()).dataSource(dataSource).withDefaultSchema() 
      .withUser("admin").password(passwordEncoder().encode("admin123")).roles("USER", "ADMIN"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic().and().csrf().disable() 
      .headers().frameOptions().disable(); 
} 

@Bean 
public BCryptPasswordEncoder passwordEncoder() { 
    return new BCryptPasswordEncoder(); 
} 
} 

没有异常或其它错误。一个简单的whitelabel错误页面显示404。

编辑:登录表单即将到来,但我认为认证有问题。

谢谢 基督教

+0

确定。我的错。它一切正常。我仍然错过了成功登录后的页面... – Chrisposure

回答

1

您必须配置要求登录表单,我相信。 Reference

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin() 
      .loginPage("/login"); 
} 

从它看起来很重要的是指定.loginPage。我为我的项目使用了以下配置。

http. 
    .authorizeRequests().antMathcers("/login-page", "/login", "/successful-login", "/error-login").anonymous().and() 
    .formLogin() 
    .loginPage("/login-page") 
    .defaultSuccessUrl("/successful-login") 
    .loginProcessingUrl("/login") 
    .failureUrl("/error-login") 
     .permitAll() 

.loginProcessingUrl是我相信URL来处理登录POST请求。

我还使用我的SecurityConfig类@EnableWebSecurity注释,

+0

感谢您的帮助!我按照您的建议将代码更改为.loginPage(...),并在出现错误后将其重新更改回现在可以正常工作。大声笑......但是......等等......现在它不再工作了! – Chrisposure

+0

我不确定如果我是唯一一个面临此问题,但有时登录时,请求没有正确注册,我得到一个错误,如“请求方法POST不支持”,它每次都发生在一个虽然但大部分我的配置工作正常。 – px06

+0

是你发布的整个'SecurityConfiguration'类吗? – px06