2017-03-03 127 views
0

我在使用MySql存儲用戶和user_roles的Springboot項目中混淆了Spring身份驗證。我的數據庫沒有任何問題,但我很難理解「魔術」彈簧認證應該如何在幕後工作。下面是我迄今爲止...瞭解Springboot MySql身份驗證配置

...... Web配置...

@Configuration 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

@Autowired 
private DataSource dataSource; 

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

    auth.jdbcAuthentication().dataSource(dataSource) 
    .usersByUsernameQuery("select * from mydatabase.users where username=?") 
    .authoritiesByUsernameQuery("select * from mydatabase.user_roles where username=?"); 

} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests().anyRequest().authenticated() 
    .and().formLogin().loginPage("/login").defaultSuccessUrl("/cardPage").permitAll() 
    .and().logout().logoutSuccessUrl("/login"); 

} 
} 

...... HTML表單...

<form class="col s12" action="/login" method="post"> 
    <div class="row"> 
     <div class="input-field col s12"> 
      <input id="username" name="username" type="text" class="validate" /> <label for="username">Username</label> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="input-field col s12"> 
      <input id="password" name="password" type="password" class="validate" /> <label for="password">Password</label> 
     </div> 
    </div> 
    <div class="row center"> 
     <button class="btn waves-effect waves-light" type="submit" name="action"> 
      Submit <i class="mdi-content-send right"></i> 
     </button> 
    </div> 
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /> 
</form> 

我能夠登錄到我的表單並提交用戶名和密碼,我可以定義Spring看到我的安全配置中的「configureAuth」方法中定義的第一個查詢,但它似乎只是失敗後,並返回一個param.error給我login.j SP。以下是控制檯輸出。可以看出,jdbcAuthentication似乎部分工作,因爲它會觸發第一個查詢,但它絕不會在.authoritiesByUserNameQuery中觸發第二個查詢。我使用thymeleaf作爲模板引擎,並且可以肯定地看到在客戶端事物上有錯誤param.error,但我不確定如何從html頁面中的param.error獲取更多信息,或者至少在服務器端啓用更有用的日誌記錄。有什麼樣的日誌配置可以用來暴露幕後的東西,至少有一些線索,哪裏出了問題?我是否需要以特定方式爲此安全模型設置數據庫?如果需要,我可以粘貼我的表格的圖片。

2017-03-02 20:01:28.522 DEBUG 12460 --- [nio-8081-exec-4] o.s.jdbc.core.JdbcTemplate    : Executing prepared SQL query 
2017-03-02 20:01:28.522 DEBUG 12460 --- [nio-8081-exec-4] o.s.jdbc.core.JdbcTemplate    : Executing prepared SQL statement [select * from mydatabase.users where username=?] 
2017-03-02 20:01:28.528 DEBUG 12460 --- [nio-8081-exec-4] o.s.jdbc.datasource.DataSourceUtils  : Fetching JDBC Connection from DataSource 
2017-03-02 20:01:33.898 TRACE 12460 --- [nio-8081-exec-4] o.s.jdbc.core.StatementCreatorUtils  : Setting SQL statement parameter value: column index 1, parameter value [user], value class [java.lang.String], SQL type unknown 
2017-03-02 20:01:34.049 DEBUG 12460 --- [nio-8081-exec-4] o.s.jdbc.datasource.DataSourceUtils  : Returning JDBC Connection to DataSource 
2017-03-02 20:01:34.056 DEBUG 12460 --- [nio-8081-exec-4] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: [email protected] 
2017-03-02 20:01:34.066 DEBUG 12460 --- [nio-8081-exec-5] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: [email protected] 
2017-03-02 20:01:34.086 DEBUG 12460 --- [nio-8081-exec-5] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: [email protected] 
+0

更新:我添加了logging.level.org.springframework.security = DEBUG,它大大增加了控制檯輸出。仍然在努力...如果我能夠解決它,將增加更多。 – Jason

回答

0

添加調試日誌記錄org.springframework.security = DEBUG後,我發現我的數據庫是罪魁禍首。我的「用戶」表中沒有「啓用」列。顯然彈簧框架需要這個值(在這種情況下,TINYINT爲1 =啓用,0 =禁用)。在使用這個字段更新我的數據庫後,我能夠通過身份驗證成功登錄,還有一些非常簡單的日誌記錄,彈出來幫助我們理解事物在幕後的工作情況,如果從Spring安全性開始,我強烈建議添加此日誌記錄級別。