2016-03-25 188 views
2

我一直在嘗試使用Postgres數據庫進行Spring安全認證。原因:PreparedStatementCallback;錯誤的SQL語法

這是我的SQL表定義:

CREATE TABLE "UTILISATEUR" (
"IdUtilisateur" serial NOT NULL, 
"Nom" character varying(50), 
"Prenom" character varying(50), 
"Profil" character varying(50), 
"Pseudo" character varying(20), 
"IdSite" integer DEFAULT 0, 
"Password" character varying(1024), 
role character varying(45), 
CONSTRAINT "UTILISATEUR_pkey" PRIMARY KEY ("IdUtilisateur"), 
CONSTRAINT fk1u FOREIGN KEY (role) 
    REFERENCES "Role" (role) MATCH SIMPLE 
    ON UPDATE NO ACTION ON DELETE NO ACTION 
); 

這是我的春天安全配置類:

package com.ardia.MDValidation; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import javax.sql.DataSource ; 

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    //Pour l'authentification des Utilisateur de Table Utilisateur 
@Autowired 
public void GlobalConfig(AuthenticationManagerBuilder auth,DataSource dataSource) throws Exception { 
    auth.jdbcAuthentication() 
    .dataSource(dataSource) 
     .usersByUsernameQuery("select Pseudo as principal , Password as credentials from UTILISATEUR where Pseudo = ? ") 
      .authoritiesByUsernameQuery("select Pseudo as principal , role as role from UTILISATEUR where Pseudo = ? ") 
       .rolePrefix("_ROLE"); 
} 
    //ne pas appliqué la securité sur les ressources 
@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring() 
    .antMatchers("/bootstrap/css/**","/css/**"); 
} 
@Override 
protected void configure(HttpSecurity http) throws Exception { 
http 

    .csrf().disable() 
    .authorizeRequests() 
    .anyRequest() 
     .authenticated()   
      .and() 
      .formLogin() 
      .permitAll(); 
} 

} 

它用於存儲用戶的工作。現在我無法讓它工作是因爲SQL語法。或者我應該更改表名,因爲在錯誤中,用戶名是小寫,我的表名是大寫?

這裏的錯誤:

org.postgresql.util.PSQLException: ERREUR: la relation « utilisateur » n'existe pas Position : 32 

我發現這個問題

,當你執行與jdbcAuthentication()它迫使列名和表名,以小寫查詢它似乎。有誰知道如何改變它?

+0

不要在UTILISATEUR使用大寫字母。 SQL **不區分大小寫,但是您的框架*可能引用標識符,這將強制sql分析器將它們視爲區分大小寫。總之:不要用首都。 – joop

+0

但我不能改變數據庫名稱它的項目,我不能觸摸數據庫只使用它就像它:( –

+0

@joop:法律,小寫,不帶引號的標識符有助於避免任何這樣的複雜化。您的建議是好的,只有解釋是向後的。[SQL中的標識符實際上是*區分大小寫的*。](http://stackoverflow.com/a/20880247/939860)在Postgres中名稱只是摺疊爲小寫(upper-除非雙引號 –

回答

1

無論誰創建你的表格,都選擇用雙引號保留CaMeL-case標識符。現在,您必須使用匹配的大小寫,並且每個地方都使用雙引號表和列名稱。

由於雙引號在您的客戶端中有特殊含義,所以您必須對特殊字符進行轉義或編碼。我不熟悉春天。也許用反斜槓逃脫?

"SELECT \"Pseudo\" AS principal, \"Password\"; AS credentials 
FROM \"UTILISATEUR\" WHERE \"Pseudo\" = ? " 

等等

相關:

+0

謝謝它的工作:D –

+0

@ KamelMili:你的意思是用反斜槓轉義,對嗎? –

+0

是的,有黑色的空白 –