mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-20 19:12:14 +00:00
SEC-1548: Added extra logging to Dao-authentication classes to clarify reasons for authentication failure (missing user vs wrong password etc.).
This commit is contained in:
parent
d6f408e8bf
commit
c458311d2d
@ -16,6 +16,8 @@
|
|||||||
package org.springframework.security.authentication.dao;
|
package org.springframework.security.authentication.dao;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.security.authentication.AccountExpiredException;
|
import org.springframework.security.authentication.AccountExpiredException;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
@ -71,6 +73,9 @@ import org.springframework.util.Assert;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractUserDetailsAuthenticationProvider implements AuthenticationProvider, InitializingBean,
|
public abstract class AbstractUserDetailsAuthenticationProvider implements AuthenticationProvider, InitializingBean,
|
||||||
MessageSourceAware {
|
MessageSourceAware {
|
||||||
|
|
||||||
|
protected final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
//~ Instance fields ================================================================================================
|
//~ Instance fields ================================================================================================
|
||||||
|
|
||||||
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
|
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
|
||||||
@ -123,6 +128,8 @@ public abstract class AbstractUserDetailsAuthenticationProvider implements Authe
|
|||||||
try {
|
try {
|
||||||
user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
|
user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
|
||||||
} catch (UsernameNotFoundException notFound) {
|
} catch (UsernameNotFoundException notFound) {
|
||||||
|
logger.debug("User '" + username + "' not found");
|
||||||
|
|
||||||
if (hideUserNotFoundExceptions) {
|
if (hideUserNotFoundExceptions) {
|
||||||
throw new BadCredentialsException(messages.getMessage(
|
throw new BadCredentialsException(messages.getMessage(
|
||||||
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
|
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
|
||||||
@ -291,16 +298,22 @@ public abstract class AbstractUserDetailsAuthenticationProvider implements Authe
|
|||||||
private class DefaultPreAuthenticationChecks implements UserDetailsChecker {
|
private class DefaultPreAuthenticationChecks implements UserDetailsChecker {
|
||||||
public void check(UserDetails user) {
|
public void check(UserDetails user) {
|
||||||
if (!user.isAccountNonLocked()) {
|
if (!user.isAccountNonLocked()) {
|
||||||
|
logger.debug("User account is locked");
|
||||||
|
|
||||||
throw new LockedException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.locked",
|
throw new LockedException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.locked",
|
||||||
"User account is locked"), user);
|
"User account is locked"), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user.isEnabled()) {
|
if (!user.isEnabled()) {
|
||||||
|
logger.debug("User account is disabled");
|
||||||
|
|
||||||
throw new DisabledException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
|
throw new DisabledException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
|
||||||
"User is disabled"), user);
|
"User is disabled"), user);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user.isAccountNonExpired()) {
|
if (!user.isAccountNonExpired()) {
|
||||||
|
logger.debug("User account is expired");
|
||||||
|
|
||||||
throw new AccountExpiredException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
|
throw new AccountExpiredException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
|
||||||
"User account has expired"), user);
|
"User account has expired"), user);
|
||||||
}
|
}
|
||||||
@ -310,6 +323,8 @@ public abstract class AbstractUserDetailsAuthenticationProvider implements Authe
|
|||||||
private class DefaultPostAuthenticationChecks implements UserDetailsChecker {
|
private class DefaultPostAuthenticationChecks implements UserDetailsChecker {
|
||||||
public void check(UserDetails user) {
|
public void check(UserDetails user) {
|
||||||
if (!user.isCredentialsNonExpired()) {
|
if (!user.isCredentialsNonExpired()) {
|
||||||
|
logger.debug("User account credentials have expired");
|
||||||
|
|
||||||
throw new CredentialsExpiredException(messages.getMessage(
|
throw new CredentialsExpiredException(messages.getMessage(
|
||||||
"AbstractUserDetailsAuthenticationProvider.credentialsExpired",
|
"AbstractUserDetailsAuthenticationProvider.credentialsExpired",
|
||||||
"User credentials have expired"), user);
|
"User credentials have expired"), user);
|
||||||
|
@ -56,6 +56,8 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (authentication.getCredentials() == null) {
|
if (authentication.getCredentials() == null) {
|
||||||
|
logger.debug("Authentication failed: no credentials provided");
|
||||||
|
|
||||||
throw new BadCredentialsException(messages.getMessage(
|
throw new BadCredentialsException(messages.getMessage(
|
||||||
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
|
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
|
||||||
includeDetailsObject ? userDetails : null);
|
includeDetailsObject ? userDetails : null);
|
||||||
@ -64,6 +66,8 @@ public class DaoAuthenticationProvider extends AbstractUserDetailsAuthentication
|
|||||||
String presentedPassword = authentication.getCredentials().toString();
|
String presentedPassword = authentication.getCredentials().toString();
|
||||||
|
|
||||||
if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) {
|
if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) {
|
||||||
|
logger.debug("Authentication failed: password does not match stored value");
|
||||||
|
|
||||||
throw new BadCredentialsException(messages.getMessage(
|
throw new BadCredentialsException(messages.getMessage(
|
||||||
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
|
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"),
|
||||||
includeDetailsObject ? userDetails : null);
|
includeDetailsObject ? userDetails : null);
|
||||||
|
@ -152,6 +152,8 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService {
|
|||||||
List<UserDetails> users = loadUsersByUsername(username);
|
List<UserDetails> users = loadUsersByUsername(username);
|
||||||
|
|
||||||
if (users.size() == 0) {
|
if (users.size() == 0) {
|
||||||
|
logger.debug("Query returned no results for user '" + username + "'");
|
||||||
|
|
||||||
throw new UsernameNotFoundException(
|
throw new UsernameNotFoundException(
|
||||||
messages.getMessage("JdbcDaoImpl.notFound", new Object[]{username}, "Username {0} not found"), username);
|
messages.getMessage("JdbcDaoImpl.notFound", new Object[]{username}, "Username {0} not found"), username);
|
||||||
}
|
}
|
||||||
@ -173,6 +175,8 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService {
|
|||||||
addCustomAuthorities(user.getUsername(), dbAuths);
|
addCustomAuthorities(user.getUsername(), dbAuths);
|
||||||
|
|
||||||
if (dbAuths.size() == 0) {
|
if (dbAuths.size() == 0) {
|
||||||
|
logger.debug("User '" + username + "' has no authorities and will be treated as 'not found'");
|
||||||
|
|
||||||
throw new UsernameNotFoundException(
|
throw new UsernameNotFoundException(
|
||||||
messages.getMessage("JdbcDaoImpl.noAuthority",
|
messages.getMessage("JdbcDaoImpl.noAuthority",
|
||||||
new Object[] {username}, "User {0} has no GrantedAuthority"), username);
|
new Object[] {username}, "User {0} has no GrantedAuthority"), username);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user