2013-01-18 51 views
0

My MySql數據庫區分大小寫,在休眠狀態下一切正常,數據庫中的表名與我的類相同。但是在Spring Security中,默認身份驗證不能正常工作,用小寫字母而不是大寫字母來構建表名第一個字母的SQL。有沒有什麼辦法讓Spring的安全性像Hibernate那樣理解大寫?或者我是否需要構建自定義身份驗證才能將表名更改爲大寫字母?春季安全區分大小寫

<!-- Session Factory Hibernate --> 
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 

    <property name="packagesToScan" value="br.com.empresa.domain" /> 
    <property name="dataSource"> 
     <ref local="mySQLdataSource" /> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
      <!-- Novos geradores de ids recomendados pela documentação do hibernate --> 
      <prop key="hibernate.id.new_generator_mappings">false</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 

</bean> 

<!-- Conexão com o Banco de Dados --> 
<bean id="mySQLdataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/db" /> --> 
    <property name="username" value="root" /> 
    <property name="password" value="asd123456" /> 

</bean> 

<!-- It is responsible for validating the user's credentials --> 
<security:authentication-manager> 

    <!-- It is responsible for providing credential validation to the AuthenticationManager --> 
    <security:authentication-provider> 
     <security:password-encoder ref="passwordEncoder" /> 
     <security:jdbc-user-service 
      data-source-ref="mySQLdataSource" /> 
    </security:authentication-provider> 

</security:authentication-manager> 

<bean 
    class="org.springframework.security.crypto.password.StandardPasswordEncoder" 
    id="passwordEncoder" /> 
+0

spring安全訪問數據庫如何?你是如何配置的? – Kent

+0

@Kent添加了我的配置 – raonirenosto

回答

2

我想你使用JdbcDaoImpl(通過JDBC用戶服務元素)。如果這是真的,那麼你可以爲默認查詢提供你自己的SQL。

<jdbc-user-service 
    users-by-username-query="select username, password, enabled from Users where username = ?" 
    authorities-by-username-query="select username, authority from Authorities where username = ?" 
/> 
+1

如果您需要用戶名不區分大小寫,那麼只需在'WHERE'子句中使用像'lower()'(或'upper()')這樣的本地函數,如'where lower(用戶名)=低(?)'。 – Lion

+0

@Maksym Demidas這就是我在互聯網上找到的方式,這很好,但爲什麼Spring安全性不像休眠?我把@Table(name =「Users」)註釋。無論如何,Spring應該與hibernate有更好的集成,那麼註釋就足夠了。 – raonirenosto

+1

因爲即使沒有Hibernate,Spring Security也必須工作。所以** JdbcDaoImpl **使用普通的JDBC調用而不是Hibernate。如果你願意,你可以提供你自己的實現UserDetailsS​​ervice並使用Hibernate的DAO。 Spring Security將能夠使用它。 –