簡體   English   中英

Spring Security和數據庫授權

[英]Spring Security and DB authorization

我登錄有問題。 如果我的用戶名和密碼相同,則登錄成功,但如果用戶名和密碼不同,則出現錯誤Reason: Bad credentials錯誤

這是“配置安全性”類中的configure方法:

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 =?");
}

我在數據庫用戶中有用戶名和密碼的Tabe。 如果我在數據庫中插入用戶,例如"user","user""usernameandpasswordsame","usernameandpasswordsame"一切正常,我可以登錄並獲取正確的user_role,但是如果在用戶名和密碼不同的數據庫中插入user,例如"user","userpass" ,我有錯誤。

這也是同一配置安全性類中的另一種configure方法:

        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();
    }

有人可以告訴我什么是錯或問題嗎?

發生這種情況的唯一情況是將表單的用戶名字段保存到密碼列。 嘗試將用戶名和密碼直接插入表中。 如果您使用的是H2,請檢查您的SQL文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM