【发布时间】:2017-03-03 02:09:23
【问题描述】:
我在使用 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 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
【问题讨论】:
-
更新:我添加了 logging.level.org.springframework.security = DEBUG,它大大增加了控制台输出。仍在努力...如果我能够解决它会添加更多。
标签: mysql authentication spring-boot