2013-05-12 117 views
2

,我有以下我的应用程序-config.xml中配置:Spring Security认证简单的登录

<security:http auto-config="true" /> 
    <security:global-method-security secured-annotations="enabled" /> 

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:jdbc-user-service data-source-ref="dataSource" 

       users-by-username-query=" 
        select login, password 
        from accounts where login=? and password=?" 

       authorities-by-username-query=" 
        select a.login, ar.authority from accounts a, account_roles ar 
        where a.account_id = ar.account_id and a.login =? " 

      /> 
     </security:authentication-provider> 
    </security:authentication-manager> 

但是当我开始我的应用程序,它显示我收到以下错误信息登录:

原因:PreparedStatementCallback;未分类SQL的SQLException [选择登录名,>登录名=>帐户的密码和 password =?]; SQL状态[90012];错误代码[90012];参数“#2”未设置为 ; SQL语句:选择登录名,密码从 login =?和密码=? [90012-170];嵌套的异常是 org.h2.jdbc.JdbcSQLException:参数“#2”未设置; SQL 声明:选择登录,密码从帐户登录=?和 password =? [90012-170]

任何想法什么是错的?

我不完全确定安全:jdbc-user-service是如何工作的?它在我的select查询中如何填入=?

我的数据库定义为:

CREATE TABLE accounts (
    account_id VARCHAR NOT NULL, 
    login VARCHAR NOT NULL, 
    password VARCHAR NOT NULL, 
    PRIMARY KEY (account_id) 
); 
CREATE TABLE account_roles (
    account_id VARCHAR NOT NULL, 
    authority VARCHAR NOT NULL, 
    PRIMARY KEY (account_id), 
    CONSTRAINT FK_account_roles FOREIGN KEY (account_id) REFERENCES accounts (account_id) 
); 

感谢

回答

3

参数users-by-username-query的名称意味着,查询将通过用户名只能做搜索,所以我建议修改你的SQL查询来是这样的:

users-by-username-query="select login, password, 'true' as enabled from accounts where login=? limit 1" 
+0

我也试过,并且得到这个消息: 原因:PreparedStatementCallback;未归类SQLException for SQL [选择登录名,密码从帐户登录=?限制1]; SQL状态[90008];错误代码[90008];参数“columnIndex”的值“3”无效[90008-170];嵌套异常是org.h2.jdbc.JdbcSQLException:参数“columnIndex”的值“3”无效[90008-170] P.S.我用我的数据库模式更新了原文。 – 2013-05-12 13:04:37

+0

这是通过应用程序配置,如果这有帮助:http://pastebin.com/NqptCiS4 – 2013-05-12 15:00:50

+0

@ john-sam我已经更新了我的答案。查询必须返回3个值:'login','password'和'enabled'属性。因为你的方案缺少'enabled'属性,所以我修改了SQL查询以始终返回'true'。 – 2013-05-12 16:13:13

0

正如Slava指出,如果用户启用或不启用,您需要指示。这里是对Spring Security 3.2.6.RELEASE:UserDetailsService文档的参考。以下是这样说的:

返回的UserDetails是提供保证的认证信息非空准备,比如用户名,密码干将接口,授予的权限和用户帐户是否启用或禁用

这是一个有点误导,如果你只读了DaoAuthenticationProvider的文档,其中规定:

它以查找用户名,密码和的GrantedAuthority UserDetailsS​​ervice的杠杆(作为DAO)。它仅通过将UsernamePasswordAuthenticationToken中提交的密码与UserDetailsS​​ervice加载的密码进行比较来验证用户身份。