【问题标题】:Spring Security and DB authorizationSpring Security 和数据库授权
【发布时间】:2016-11-21 22:10:51
【问题描述】:

我在登录时遇到问题。如果我的用户名和密码相同,登录成功但如果用户名和密码不同,我有错误Reason: Bad credentials

这是配置安全类中的配置方法:

protected void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    auth
    .jdbcAuthentication()
    .dataSource(dataSource)
    .usersByUsernameQuery("select username,password, enabled from users where username=? ")
    .authoritiesByUsernameQuery("select u.username, r.name from users u, security_role r, users_security_role uhr where u.id_user = uhr.id and r.id = uhr.security_role_id and u.username =?");
}

我在 DB 用户中有用户名和密码。 如果我在数据库中插入用户,如 "user","user""usernameandpasswordsame","usernameandpasswordsame" 一切正常,我可以登录并获得正确的 user_role,但如果在数据库中插入用户名和密码不同的用户,如 "user","userpass",我有错误。

这也是同一个配置安全类中的另一个配置方法:

        protected void configure(HttpSecurity http) throws Exception {
         http.csrf().disable()
         .authorizeRequests()
            .expressionHandler(webExpressionHandler())
            .anyRequest().authenticated()
            .and()
         .formLogin()
            .and()
         .logout()
         .deleteCookies("remove")
            .invalidateHttpSession(true)
            .logoutUrl("/logout")
         .permitAll();
    }

谁能告诉我有什么问题或问题?

【问题讨论】:

  • 我无法重现您的问题。请查看How to create a Minimal, Complete, and Verifiable example
  • 我猜你在某个地方犯了一个错误,并用用户名值填充了密码变量。你的查询看起来是正确的,所以我会检查前端(jsp,....)并确保变量没有混合。

标签: java mysql spring spring-security authorization


【解决方案1】:

发生这种情况的唯一情况是您将表单的用户名字段保存到密码列。尝试将您的用户名和密码直接插入您的表中。如果您使用的是 H2,请检查您的 SQL 文件。

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 2020-04-25
    • 1970-01-01
    • 2012-03-02
    • 2013-03-15
    • 2015-03-06
    • 2017-07-22
    • 2016-08-08
    • 2017-04-20
    相关资源
    最近更新 更多