【问题标题】:How to authenticate users from database with an authentication provider?如何使用身份验证提供程序对数据库中的用户进行身份验证?
【发布时间】:2015-07-29 09:17:20
【问题描述】:

我是 Spring Security 的新手,我想使用数据库对用户进行身份验证。 我已经使用 jdbc 创建了一个登录页面和一个身份验证提供程序,用于检查用户是否存在于数据库中。 但是我的代码没有这样做的问题,它允许所有用户登录! 我的代码有什么问题? 感谢您的帮助。

@Component(value = "userService")
public class UserService implements AuthenticationProvider {

@Inject
@Named(value = "dataSource")
private DataSource dataSource;

ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;

name=auth.getName();    
    pwd=auth.getCredentials().toString();


public Authentication authenticate(Authentication auth)
        throws AuthenticationException {
    final String select_auth = "select username,password from users where username='"+name+"' and password='"+pwd+"'";
    try {
        connection = dataSource.getConnection();
        preparedStatement = connection.prepareStatement(select_auth);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {

                 //what to return here ?
            }

这是我的 security-confg.xml:

<http auto-config="true">
    <form-login login-page="/login" username-parameter="j_username"
        password-parameter="j_password" default-target-url="/accueil"

        authentication-failure-url="/403" />
    <logout logout-success-url="/login" />
</http>

<authentication-manager>
    <authentication-provider ref="userService">
    </authentication-provider>
</authentication-manager>

【问题讨论】:

  • 您最好使用UserDetailsService 而不是完整的AuthenticationProvider 实现。请参阅this post,了解为您的用例配置UserDetailsService 的最简单方法。
  • 感谢您的回复@manish,但找不到您发布的链接。
  • 发帖者删除了帖子并re-posted了它。检查新帖子。请参阅帖子顶部以及用于获取用户名和密码的 SQL 查询。

标签: java spring-mvc spring-security


【解决方案1】:

在代码中,我没有看到为查询设置用户名和密码的地方。您尝试获取名称和密码并检查您的查询是否返回任何结果

  @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String select_auth = "select username,password from users where username=? and password=?";
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    preparedStatement = connection.prepareStatement(select_auth);
    p.setString(1, username);
    p.setString(2, password);
    resultSet = preparedStatement.executeQuery();

    while (resultSet.next()) {

        List<SampleAuthority> authorities = new ArrayList<SampleAuthority>();

        SampleAuthority a = new SampleAuthority();
        authorities.add(a);
        Collection<? extends GrantedAuthority> authorities1 = authorities;
        return new UsernamePasswordAuthenticationToken(username, password, authorities1);

   }
}

@Override
public boolean supports(Class<?> arg0) {
    return true;
}

class SampleAuthority implements GrantedAuthority {

    @Override
    public String getAuthority() {
        return "ROLE_USER";
    }

}

在配置中,可以添加

<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
<logout logout-url="/logout" />

【讨论】:

  • 感谢您的回复,我真的不明白将用户名和密码放在查询中的问题出在哪里..但我正在寻找的是从中获取用户名和密码登录页面并通过此身份验证提供程序检查它们是否存在于数据库中..
  • 我的意思是你能检查 ResultSet 以确保它并不总是返回值吗?!还有一件事,你能分享一下 spring-security.xml 配置吗?!你在拦截中使用匿名角色吗?!
  • 我已经检查过了! resultSet 不会从数据库返回任何值!它有什么问题?我已经用 spring-security.xml 更新了帖子
  • 我已经更新了身份验证提供程序并添加了登录检查.. 但仍然对返回什么感到困惑?
  • 您必须检查两件事:来自数据库查询的结果集。请仔细检查我的代码。然后检查数据库是否返回结果集中的任何数据?!第二个问题是“应该返回什么?”,请看GrantedAuthority的示例代码。
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 2017-12-25
  • 1970-01-01
  • 2021-02-28
  • 2011-05-01
  • 2018-01-22
  • 2018-12-20
  • 1970-01-01
相关资源
最近更新 更多