验证Spring Security登录凭据

时间:2011-03-15 08:16:03

标签: spring spring-security

我的Web应用程序正在使用Spring Security,但在处理基于表单的登录时返回BadCredentialsException。用户输入的凭据与数据库中的确切匹配,我没有看到将j_password值与db中的Users.getPassword的值进行比较的Spring代码,有关如何解决此问题的任何想法?我想看看它与BadCredentialsException的比较。当我使用硬编码时,下面是我的实现大纲,希望有人可以发现我的错误。谢谢你的帮助!

public static Users getLoggedInUser() {
    Users user = null;
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null && auth.isAuthenticated()) {
        Object principal = auth.getPrincipal();
        if (principal instanceof Users) {
            user = (Users) principal;
        }
    }
    return user;
}

安全上下文文件(删除了xml和模式定义):

<global-method-security secured-annotations="enabled">
</global-method-security>
<http security="none" pattern="/services/rest-api/1.0/**" />
<http security="none" pattern="/preregistered/**" />
<http access-denied-page="/auth/denied.html">
    <intercept-url
        pattern="/**/*.xhtml"
        access="ROLE_NONE_GETS_ACCESS" />
    <intercept-url
        pattern="/auth/**"
        access="ROLE_ANONYMOUS,ROLE_USER" />
    <intercept-url
        pattern="/auth/*"
        access="ROLE_ANONYMOUS" />
     <intercept-url
        pattern="/**"
        access="ROLE_USER" />
    <form-login
        login-processing-url="/j_spring_security_check.html"
        login-page="/auth/login.html"
        default-target-url="/registered/home.html"
        authentication-failure-url="/auth/login.html?_dc=45" />
    <logout logout-url="/auth/logout.html"
            logout-success-url="/" />
    <anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/>
    <remember-me user-service-ref="userManager" key="valid key here"/>
</http>
<!-- Configure the authentication provider -->
<authentication-manager>
    <authentication-provider user-service-ref="userManager">
            <password-encoder ref="passwordEncoder" />
    </authentication-provider>
</authentication-manager>

UserDetails实现(Users.java):

public class Users implements Serializable, UserDetails {
    public Collection<GrantedAuthority> getAuthorities() {
     List<GrantedAuthority> auth = new ArrayList<GrantedAuthority>();
    auth.add(new GrantedAuthorityImpl("ROLE_USER"));
    return auth;
}

}

user-service-ref =“userManager”(UserManagerImpl.java):

 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    Users user = null;
    try {
        user = userDAO.findByUsername(username);
    } catch (DataAccessException ex) {
        throw new UsernameNotFoundException("Invalid login", ex);
    }
    if (user == null) {
        throw new UsernameNotFoundException("User not found.");
    }
    return user;
}

2 个答案:

答案 0 :(得分:1)

看起来您正在将明文密码存储在数据库中,但在spring-security配置中使用passwordEncoder,该配置会在与存储的密码进行比较之前对接收到的密码进行编码。

答案 1 :(得分:0)

对我来说,你刚刚开始使用Spring Security。由于您的问题非常通用,我无法确切地说出您的问题在哪里。这可能是错误的配置。也许数据库有问题。或其他任何事情。

我建议您按以下方式解决问题:

  1. 尽可能简化你的配置。
  2. 尝试使用最简单的Spring Security配置检查应用程序的工作方式。
  3. 如果有效,请逐步添加功能,并始终检查是否存在问题。
  4. 简单的Spring安全配置可能是这样的:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/security
             http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    
    <http auto-config="true">
        <session-management session-fixation-protection="none"/>
        <intercept-url pattern="/login.jsp" filters="none"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/login.jsp" always-use-default-target="true"/>
    </http>
    
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="admin" password="pass1" authorities="ROLE_ADMIN, ROLE_USER"/>
                <user name="user" password="pass2" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>
    
    </beans:beans>
    
相关问题