很想吐槽的一個東西java
嘗試了spring的authentication,使用org.acegisecurity.userdetails.jdbc.JdbcDaoImpl的authenticationDaospring
代碼以下:sql
1 package org.hawklithm.acegi; 2 3 import org.acegisecurity.Authentication; 4 import org.acegisecurity.providers.ProviderManager; 5 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class AuthenticateMain { 9 public void go() { 10 UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("hawky", "root"); 11 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-acegi.xml"); 12 ProviderManager manager=(ProviderManager) context.getBean("authenticationManager"); 13 Authentication auth=manager.authenticate(authRequest); 14 System.out.println("name: "+auth.getName()); 15 System.out.println("String: "+auth.toString()); 16 System.out.println("authenticated: "+auth.isAuthenticated()); 17 } 18 19 public static void main(String args[]) { 20 AuthenticateMain auth = new AuthenticateMain(); 21 auth.go(); 22 } 23 }
配置文件:ide
<bean id="authenticationDao" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"> <property name="dataSource" ref="dataSource" /> <property name="usersByUsernameQuery"> <value> select username,password from user where username= ? </value> </property> <property name="authoritiesByUsernameQuery"> <value> select username,authority from user_privileges where username= ? </value> </property> </bean> <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="authenticationDao" /> </bean> <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"> <property name="providers"> <list> <ref bean="daoAuthenticationProvider" /> </list> </property> </bean>
剛開始一直報錯:spa
Exception in thread "main" org.acegisecurity.AuthenticationServiceException: PreparedStatementCallback; uncategorized SQLException for SQL [
select username,password from user where username= ?
]; SQL state [S1009]; error code [0]; Column Index out of range, 3 > 2. ; nested exception is java.sql.SQLException: Column Index out of range, 3 > 2. ; nested exception is org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [
select username,password from user where username= ?
]; SQL state [S1009]; error code [0]; Column Index out of range, 3 > 2. ; nested exception is java.sql.SQLException: Column Index out of range, 3 > 2.
at org.acegisecurity.providers.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:102)
at org.acegisecurity.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:122)
at org.acegisecurity.providers.ProviderManager.doAuthentication(ProviderManager.java:200)
at org.acegisecurity.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:47)
at org.hawklithm.acegi.AuthenticateMain.go(AuthenticateMain.java:13)
at org.hawklithm.acegi.AuthenticateMain.main(AuthenticateMain.java:21)
爲何呢?爲何會越界呢?code
疑惑了好久,網上各類解答,可是各類嘗試無果xml
後來想起書上的例子裏面是查詢username,password和enable,而後添加上了enable,結果就搞定了,太坑了,竟然必須加入enable參數blog
即,將sql語句select username,password from user where username= ?改成select username,password,enable from user where username= ?