了解Springboot MySql身份验证配置
我在使用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]
添加调试日志记录org.springframework.security = DEBUG后,我发现我的数据库是罪魁祸首。我的“用户”表中没有“启用”列。显然弹簧框架需要这个值(在这种情况下,TINYINT为1 =启用,0 =禁用)。在使用这个字段更新我的数据库后,我能够通过身份验证成功登录,还有一些非常简单的日志记录,弹出来帮助我们理解事物在幕后的工作情况,如果从Spring安全性开始,我强烈建议添加此日志记录级别。
更新:我添加了logging.level.org.springframework.security = DEBUG,它大大增加了控制台输出。仍然在努力...如果我能够解决它,将增加更多。 – Jason