2015-12-31 20 views
1

你好,我有一個Spring Boot應用程序,它只有1個Admin和沒有用戶。 我已經創建了一個名爲「administrator」的表,其中包含用戶名和帶有散列密碼的字符串。 (我把密碼轉換器這樣做。)Spring Boot,MySQL:如何用散列密碼進行數據庫驗證?

現在的問題是我應該在WebSecurityConfiguration.java改變,這樣的代碼跟我已經寫在登錄字符串檢查哈希在我的數據庫形成能夠登錄

我的代碼:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
      .withUser("admin").password("admin").roles("ADMIN"); 

} 

我發現下面的,但我還是真的不知道該怎麼做:

+0

你閱讀的文檔? http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#jc-authentication-jdbc –

+0

我看着它,但它並沒有真正顯示如何進行身份驗證。我讀到我需要編碼結束編碼密碼。但是我不知道我必須做什麼更改,它會編碼我在WebSecurity中定義的密碼,將其保存在數據庫中,並且在嘗試進行身份驗證時可以從數據庫進行編碼。 – PrestigeDev

+0

請參閱http://www.mkyong.com/spring-security/spring-security-form-login-using-database/例如和http://stackoverflow.com/questions/8521251/spring-securitypassword-encoding-in -db-and-in-applicationcontext –

回答

0

它應該是這樣的:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

auth.jdbcAuthentication().dataSource(this.dataSource) 
.usersByUsernameQuery("select email, password, active from user where email=?") 
.authoritiesByUsernameQuery("select u.email, r.role from user u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?").passwordEncoder(passwordencoder()); 
} 

@Bean(name="passwordEncoder") 
    public PasswordEncoder passwordencoder(){ 
     return new BCryptPasswordEncoder(); 
    } 
相關問題