Spring安全认证简单登录

时间:2013-05-12 12:48:32

标签: java spring spring-mvc spring-security

我在app-config.xml中配置了以下内容:

<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 [select login,&gt;来自帐户的密码,其中login =?和   密码=]; SQL状态[90012];错误代码[90012];参数“#2”是   没有设置; SQL语句:从帐户中选择登录,密码   登录=?和密码=? [90012-170];嵌套异常是   org.h2.jdbc.JdbcSQLException:未设置参数“#2”; SQL   声明:选择登录,登录帐户的密码=?和   密码=? [90012-170]

任何想法都错了吗?

我不完全确定安全性如何:jdbc-user-service有效吗?它如何在我的选择查询中填写=?

我的数据库定义为:

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)
);

由于

2 个答案:

答案 0 :(得分:3)

参数users-by-username-query的名称意味着查询将仅按用户名进行搜索,因此我建议将SQL查询修改为以下内容:

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

答案 1 :(得分:0)

Slava指出,如果用户启用了,您需要指示。以下是对Spring Security 3.2.6.RELEASE:UserDetailsService文档的引用。以下是它的说法:

  

返回的UserDetails是一个接口,提供保证非空提供身份验证信息的getter,例如用户名,密码,授予的权限以及用户帐户是启用还是禁用

如果您只阅读DaoAuthenticationProvider的文档,则会有一点误导,其中说明:

  

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

相关问题