Skip to content

Commit 39e2bb6

Browse files
committed
Create Authentication Only Once
Issue spring-projectsgh-17933
1 parent 0f4e1f2 commit 39e2bb6

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

core/src/main/java/org/springframework/security/authentication/dao/AbstractUserDetailsAuthenticationProvider.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.security.authentication.dao;
1818

19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
1922
import org.apache.commons.logging.Log;
2023
import org.apache.commons.logging.LogFactory;
2124

@@ -33,6 +36,7 @@
3336
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3437
import org.springframework.security.core.Authentication;
3538
import org.springframework.security.core.AuthenticationException;
39+
import org.springframework.security.core.GrantedAuthority;
3640
import org.springframework.security.core.SpringSecurityMessageSource;
3741
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3842
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
@@ -200,12 +204,11 @@ protected Authentication createSuccessAuthentication(Object principal, Authentic
200204
// so subsequent attempts are successful even with encoded passwords.
201205
// Also ensure we return the original getDetails(), so that future
202206
// authentication events after cache expiry contain the details
203-
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken
204-
.authenticated(principal, authentication.getCredentials(),
205-
this.authoritiesMapper.mapAuthorities(user.getAuthorities()))
206-
.toBuilder()
207-
.authorities((a) -> a.add(new SimpleGrantedAuthority(AUTHORITY)))
208-
.build();
207+
Collection<GrantedAuthority> authorities = new ArrayList<>(
208+
this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
209+
authorities.add(new SimpleGrantedAuthority(AUTHORITY));
210+
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(principal,
211+
authentication.getCredentials(), authorities);
209212
result.setDetails(authentication.getDetails());
210213
this.logger.debug("Authenticated user");
211214
return result;

ldap/src/main/java/org/springframework/security/ldap/authentication/AbstractLdapAuthenticationProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.ldap.authentication;
1818

19+
import java.util.ArrayList;
1920
import java.util.Collection;
2021

2122
import org.apache.commons.logging.Log;
@@ -103,11 +104,11 @@ protected Authentication createSuccessfulAuthentication(UsernamePasswordAuthenti
103104
UserDetails user) {
104105
Object password = this.useAuthenticationRequestCredentials ? authentication.getCredentials()
105106
: user.getPassword();
106-
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken
107-
.authenticated(user, password, this.authoritiesMapper.mapAuthorities(user.getAuthorities()))
108-
.toBuilder()
109-
.authorities((a) -> a.add(new SimpleGrantedAuthority(AUTHORITY)))
110-
.build();
107+
Collection<GrantedAuthority> authorities = new ArrayList<>(
108+
this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
109+
authorities.add(new SimpleGrantedAuthority(AUTHORITY));
110+
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(user, password,
111+
authorities);
111112
result.setDetails(authentication.getDetails());
112113
this.logger.debug("Authenticated user");
113114
return result;

0 commit comments

Comments
 (0)