了解Springboot MySql身份验证配置

时间:2017-03-03 02:09:23

标签: mysql authentication spring-boot

我在使用MySql存储用户和user_roles的Springboot项目中使用Spring身份验证。我的数据库连接没有任何问题,但我很难理解“魔术”弹簧认证应该如何在幕后工作。以下是我到目前为止......

...网络配置...

@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 ...

<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.jsp。下面是控制台输出。可以看出,jdbcAuthentication似乎部分工作,因为它触发了第一个查询,但它似乎永远不会触发.authoritiesByUserNameQuery中的第二个查询。我使用thymeleaf作为模板引擎,并且肯定可以看到客户端的 param.error 中存在错误,但我不确定如何从param.error中获取更多信息在html页面中或者至少在服务器端启用更有用的日志记录。是否有某种日志配置,我可以用来暴露幕后的东西,至少有一个线索,哪里出了问题?我是否需要以特定方式为此安全模型设置数据库?如果需要,我可以粘贴我的表格的图片。

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: org.apache.catalina.connector.RequestFacade@467d2a7f
2017-03-02 20:01:34.066 DEBUG 12460 --- [nio-8081-exec-5] o.s.b.w.f.OrderedRequestContextFilter    : Bound request context to thread: org.apache.catalina.connector.RequestFacade@467d2a7f
2017-03-02 20:01:34.086 DEBUG 12460 --- [nio-8081-exec-5] o.s.b.w.f.OrderedRequestContextFilter    : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@467d2a7f

1 个答案:

答案 0 :(得分:0)

在为org.springframework.security = DEBUG添加DEBUG日志记录后,我发现我的数据库是罪魁祸首。我的“用户”表中没有“启用”列。显然,spring框架需要这个值(在这种情况下,TINYINT为1 =启用,0 =禁用)。使用此字段更新我的数据库后,我能够通过身份验证成功登录,还有一些非常直接的日志记录,可以帮助了解幕后的工作情况,如果您从Spring安全性开始,我强烈建议添加此日志记录级别。