【问题标题】:Understanding Springboot MySql Authentication Config了解 Springboot MySql 身份验证配置
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2012-03-24
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2014-01-11
    • 1970-01-01
    相关资源
    最近更新 更多