Spring Security - 列索引异常

时间:2017-10-19 16:44:20

标签: java spring spring-mvc spring-security spring-data-jpa

我想使用spring security.But我收到了SQLException.I仍然没有找到任何解决方案。

我的表格结构:

APP_USER(ID_APP_USER,NAME,PASSWORD)

APP_ROLE(ID_ROLE,NAME)

APP_USER_ROLE(ID_USER_ROLE,ID_USER,ID_ROLE)

我的Spring配置文件:

 <!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">

    <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />

    <!-- access denied page -->
    <access-denied-handler error-page="/403" />

    <form-login
        login-page="/login"
        default-target-url="/index"
        authentication-failure-url="/login?error"
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout"  />
    <!-- enable csrf protection -->
    <csrf/>
</http>

<!-- Select users and user_roles from database -->
<authentication-manager>
  <authentication-provider>
    <jdbc-user-service data-source-ref="dataSource"
      users-by-username-query=
        "select name as username,password from app_user where name=?"
      authorities-by-username-query=
        "SELECT app_user.name as username, app_role.name as role 
            FROM app_user 
            INNER JOIN app_user_role ON app_user.id_app_user = app_user_role.id_user 
            INNER JOIN app_role ON app_user_role.id_role = app_role.id_role
            WHERE app_user.name = ? " />
  </authentication-provider>
</authentication-manager>

我收到了以下错误:

  

20:36:55.281 [http-nio-8089-exec-10] DEBUG o.s.j.s.SQLErrorCodeSQLExceptionTranslator - 用SQL状态'99999'翻译SQLException,错误代码'17003',消息[无效列索引]; SQL是[select name as as username,password from app_user where name =?] for task [PreparedStatementCallback]   20:36:55.282 [http-nio-8089-exec-10] DEBUG o.s.s.w.a.UsernamePasswordAuthenticationFilter - 身份验证请求失败:org.springframework.security.authentication.AuthenticationServiceException:PreparedStatementCallback; SQL的SQLSet访问权限无效[选择名称作为用户名,密码来自app_user,其中name =?];嵌套异常是java.sql.SQLException:列索引无效   20:36:55.282 [http-nio-8089-exec-10] DEBUG o.s.s.w.a.UsernamePasswordAuthenticationFilter - 更新了SecurityContextHolder以包含空认证   20:36:55.282 [http-nio-8089-exec-10] DEBUG o.s.s.w.a.UsernamePasswordAuthenticationFilter - 委托身份验证失败处理程序org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler@2f483b1e   20:36:55.282 [http-nio-8089-exec-10] DEBUG o.s.s.w.a.SimpleUrlAuthenticationFailureHandler - 重定向到/ login?错误   20:36:55.282 [http-nio-8089-exec-10] DEBUG o.s.s.web.DefaultRedirectStrategy - 重定向到'/ FNDWEB /登录?错误'

怎么了?请帮帮我:/提前谢谢。

1 个答案:

答案 0 :(得分:2)

最后我找到了解决方案。我们需要在查询中添加“,1启用”。

<authentication-provider>
    <jdbc-user-service data-source-ref="dataSource"
      users-by-username-query=
        "select name as username,password,1 as enabled from app_user where name=?"
      authorities-by-username-query=
        "SELECT app_user.name as username, app_role.name as role 
            FROM app_user 
            INNER JOIN app_user_role ON app_user.id_app_user = app_user_role.id_user 
            INNER JOIN app_role ON app_user_role.id_role = app_role.id_role
            WHERE app_user.name = ? " />
  </authentication-provider>