Add UsernamePasswordAuthenticationToken factory methods
- unauthenticated factory method - authenticated factory method - test for unauthenticated factory method - test for authenticated factory method - make existing constructor protected - use newly factory methods in rest of the project - update copyright dates Closes gh-10790
This commit is contained in:
committed by
Josh Cummings
parent
d2f24ae5f5
commit
ac9c29b2a0
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -117,7 +117,7 @@ public abstract class AbstractUserDetailsReactiveAuthenticationManager
|
||||
}
|
||||
|
||||
private UsernamePasswordAuthenticationToken createUsernamePasswordAuthenticationToken(UserDetails userDetails) {
|
||||
return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(),
|
||||
return UsernamePasswordAuthenticationToken.authenticated(userDetails, userDetails.getPassword(),
|
||||
userDetails.getAuthorities());
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
||||
* <code>String</code>.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @author Norbert Nowak
|
||||
*/
|
||||
public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationToken {
|
||||
|
||||
@@ -71,6 +72,33 @@ public class UsernamePasswordAuthenticationToken extends AbstractAuthenticationT
|
||||
super.setAuthenticated(true); // must use super, as we override
|
||||
}
|
||||
|
||||
/**
|
||||
* This factory method can be safely used by any code that wishes to create a
|
||||
* unauthenticated <code>UsernamePasswordAuthenticationToken</code>.
|
||||
* @param principal
|
||||
* @param credentials
|
||||
* @return UsernamePasswordAuthenticationToken with false isAuthenticated() result
|
||||
*
|
||||
* @since 5.7
|
||||
*/
|
||||
public static UsernamePasswordAuthenticationToken unauthenticated(Object principal, Object credentials) {
|
||||
return new UsernamePasswordAuthenticationToken(principal, credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* This factory method can be safely used by any code that wishes to create a
|
||||
* authenticated <code>UsernamePasswordAuthenticationToken</code>.
|
||||
* @param principal
|
||||
* @param credentials
|
||||
* @return UsernamePasswordAuthenticationToken with true isAuthenticated() result
|
||||
*
|
||||
* @since 5.7
|
||||
*/
|
||||
public static UsernamePasswordAuthenticationToken authenticated(Object principal, Object credentials,
|
||||
Collection<? extends GrantedAuthority> authorities) {
|
||||
return new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getCredentials() {
|
||||
return this.credentials;
|
||||
|
||||
@@ -193,7 +193,7 @@ public abstract class AbstractUserDetailsAuthenticationProvider
|
||||
// so subsequent attempts are successful even with encoded passwords.
|
||||
// Also ensure we return the original getDetails(), so that future
|
||||
// authentication events after cache expiry contain the details
|
||||
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal,
|
||||
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(principal,
|
||||
authentication.getCredentials(), this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
|
||||
result.setDetails(authentication.getDetails());
|
||||
this.logger.debug("Authenticated user");
|
||||
|
||||
@@ -47,7 +47,8 @@ public class RemoteAuthenticationManagerImpl implements RemoteAuthenticationMana
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> attemptAuthentication(String username, String password)
|
||||
throws RemoteAuthenticationException {
|
||||
UsernamePasswordAuthenticationToken request = new UsernamePasswordAuthenticationToken(username, password);
|
||||
UsernamePasswordAuthenticationToken request = UsernamePasswordAuthenticationToken.unauthenticated(username,
|
||||
password);
|
||||
try {
|
||||
return this.authenticationManager.authenticate(request).getAuthorities();
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public class RemoteAuthenticationProvider implements AuthenticationProvider, Ini
|
||||
String password = (credentials != null) ? credentials.toString() : null;
|
||||
Collection<? extends GrantedAuthority> authorities = this.remoteAuthenticationManager
|
||||
.attemptAuthentication(username, password);
|
||||
return new UsernamePasswordAuthenticationToken(username, password, authorities);
|
||||
return UsernamePasswordAuthenticationToken.authenticated(username, password, authorities);
|
||||
}
|
||||
|
||||
public RemoteAuthenticationManager getRemoteAuthenticationManager() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -78,8 +78,8 @@ class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<U
|
||||
List<GrantedAuthority> authorities = mapper.readValue(readJsonNode(jsonNode, "authorities").traverse(mapper),
|
||||
GRANTED_AUTHORITY_LIST);
|
||||
UsernamePasswordAuthenticationToken token = (!authenticated)
|
||||
? new UsernamePasswordAuthenticationToken(principal, credentials)
|
||||
: new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
|
||||
? UsernamePasswordAuthenticationToken.unauthenticated(principal, credentials)
|
||||
: UsernamePasswordAuthenticationToken.authenticated(principal, credentials, authorities);
|
||||
JsonNode detailsNode = readJsonNode(jsonNode, "details");
|
||||
if (detailsNode.isNull() || detailsNode.isMissingNode()) {
|
||||
token.setDetails(null);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -125,7 +125,8 @@ public class InMemoryUserDetailsManager implements UserDetailsManager, UserDetai
|
||||
// supplied password.
|
||||
if (this.authenticationManager != null) {
|
||||
this.logger.debug(LogMessage.format("Reauthenticating user '%s' for password change request.", username));
|
||||
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
|
||||
this.authenticationManager
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(username, oldPassword));
|
||||
}
|
||||
else {
|
||||
this.logger.debug("No authentication manager set. Password won't be re-checked.");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -271,7 +271,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
|
||||
// supplied password.
|
||||
if (this.authenticationManager != null) {
|
||||
this.logger.debug(LogMessage.format("Reauthenticating user '%s' for password change request.", username));
|
||||
this.authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, oldPassword));
|
||||
this.authenticationManager
|
||||
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated(username, oldPassword));
|
||||
}
|
||||
else {
|
||||
this.logger.debug("No authentication manager set. Password won't be re-checked.");
|
||||
@@ -287,8 +288,8 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
|
||||
|
||||
protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
|
||||
UserDetails user = loadUserByUsername(currentAuth.getName());
|
||||
UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, null,
|
||||
user.getAuthorities());
|
||||
UsernamePasswordAuthenticationToken newAuthentication = UsernamePasswordAuthenticationToken.authenticated(user,
|
||||
null, user.getAuthorities());
|
||||
newAuthentication.setDetails(currentAuth.getDetails());
|
||||
return newAuthentication;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user