2016-08-12 59 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

相關問題