原因:PreparedStatementCallback;糟糕的SQL语法

时间:2016-03-25 10:25:46

标签: mysql postgresql spring-security spring-boot identifier

我一直在尝试使用Postgres数据库进行Spring安全性身份验证。

这是我的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()执行查询时,它会强制列和表名称为小写。有谁知道如何改变它?

1 个答案:

答案 0 :(得分:1)

创建表的人选择使用双引号保留CaMeL案例标识符。现在,您必须在任何地方使用匹配的大小写和双引号表和列名称。

由于双引号在您的客户端中具有特殊含义,因此您必须转义或编码特殊字符。我对Spring不熟悉。也许用反斜杠逃脱?

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

相关: