spring security 2 demo 自定义数据库结构时出错 报java.lang.ArrayIndexOutOfBoundsException: 2

vitou 发布于 2012/03/29 11:39
阅读 2K+
收藏 0

今天练习spring security 2时默认使用数据库表users 和authorities时,一切顺利。

当进行到自定义数据库表结构时出错了。

<jdbc-user-service data-source-ref="dataSource"
     users-by-username-query="select name,password
			 from management_user
			where name=?"
authorities-by-username-query="select u.name,r.name as authority
			     from management_user u
			     join management_user_role ur
			       on u.id=ur.management_user_id
			     join role r
			       on r.id=ur.role_id
			    where u.name=?"/>
 </authentication-provider>

表结构如下:

CREATE TABLE `management_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `password` varchar(100) NOT NULL,
  `configpassword` varchar(100) NOT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_management_user` (`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='后台管理员'

CREATE TABLE `management_user_role` (
  `role_id` int(11) NOT NULL,
  `management_user_id` int(11) NOT NULL,
  PRIMARY KEY (`role_id`,`management_user_id`),
  KEY `FK_Reference_7` (`management_user_id`),
  KEY `FK_Reference_6` (`role_id`),
  CONSTRAINT `FK_Reference_6` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`),
  CONSTRAINT `FK_Reference_7` FOREIGN KEY (`management_user_id`) REFERENCES `management_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='后台管理用户所属角色'

CREATE TABLE `role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` varchar(255) NOT NULL COMMENT '权限',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='后台管理角色'
当进入登入页面输入正确的名字和密码后报错,如下:
java.lang.ArrayIndexOutOfBoundsException: 2
 at com.mysql.jdbc.ResultSet.getNativeBoolean(ResultSet.java:5594)
 at com.mysql.jdbc.ResultSet.getBoolean(ResultSet.java:734)
 at org.springframework.security.userdetails.jdbc.JdbcDaoImpl$UsersByUsernameMapping.mapRow(JdbcDaoImpl.java:397)
 at org.springframework.jdbc.object.MappingSqlQuery.mapRow(MappingSqlQuery.java:64)
 at org.springframework.jdbc.object.MappingSqlQueryWithParameters$RowMapperImpl.mapRow(MappingSqlQueryWithParameters.java:115)
 at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
 at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:600)
 at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:538)
 at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:588)
 at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:613)
 at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:645)
 at org.springframework.jdbc.object.SqlQuery.execute(SqlQuery.java:112)
 at org.springframework.jdbc.object.SqlQuery.execute(SqlQuery.java:199)
 at org.springframework.jdbc.object.SqlQuery.execute(SqlQuery.java:207)
 at org.springframework.security.userdetails.jdbc.JdbcDaoImpl.loadUsersByUsername(JdbcDaoImpl.java:214)
 at org.springframework.security.userdetails.jdbc.JdbcDaoImpl.loadUserByUsername(JdbcDaoImpl.java:175)
 at org.springframework.security.providers.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:83)
 at org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:121)
 at org.springframework.security.providers.ProviderManager.doAuthentication(ProviderManager.java:188)
 at org.springframework.security.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:46)
 at org.springframework.security.ui.webapp.AuthenticationProcessingFilter.attemptAuthentication(AuthenticationProcessingFilter.java:82)
 at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:258)
 at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
 at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
 at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
 at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
 at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
 at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
 at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
 at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
 at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
 at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:183)
 at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:138)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
 at java.lang.Thread.run(Thread.java:619)
我虽然知道是说非法索引访问数组抛出的异常,还是结果集 转化出错 但是这个信息视乎很少,没见过着方面的问题,固请教下

以下是问题补充:

@vitou:原来用的是mysqldriver.jar 现在改用mysql-connection-java。5.14.jar包 报错 登陆失败 PreparedStatementCallback; uncategorized SQLException for SQL [select name,password from management_user where name=?]; 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 name,password from management_user where name=?]; 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. (2012/03/29 13:06)
加载中
0
vitou
vitou

后来 我在数据库中添加一个关于用户是否被禁的字段在

users-by-username-query="select name,password,enabled
                                         from management_user
                                        where name=?" 后,发现可以出现自己希望的了,

看来代码报错还是很给力的

希望我的这次错误可以给一些人帮助吧

返回顶部
顶部