Spring Security和DB授权

时间:2016-11-21 22:10:51

标签: java mysql spring spring-security authorization

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

这是Configuration Security类中的配置方法:

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用户中有tabe,用户名和密码。 如果我在数据库中插入用户,例如"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();
    }

有人可以告诉我出了什么问题或问题吗?

1 个答案:

答案 0 :(得分:0)

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

相关问题