Skip to content

Commit 50a009e

Browse files
committed
Add setter method for userDetailsChecker in CasAuthenticationProvider(#10277)
This commit introduces a setter method for the userDetailsChecker property in the CasAuthenticationProvider class. Previously, the userDetailsChecker was initialized with a default AccountStatusUserDetailsChecker instance, limiting customization options. Now, users can inject their own UserDetailsChecker implementation through the setter method, providing greater flexibility in handling user details validation.
1 parent 746ee27 commit 50a009e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

cas/src/main/java/org/springframework/security/cas/authentication/CasAuthenticationProvider.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@
5656
*
5757
* @author Ben Alex
5858
* @author Scott Battaglia
59+
* @author Kim Youngwoong
5960
*/
6061
public class CasAuthenticationProvider implements AuthenticationProvider, InitializingBean, MessageSourceAware {
6162

6263
private static final Log logger = LogFactory.getLog(CasAuthenticationProvider.class);
6364

6465
private AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService;
6566

66-
private final UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker();
67+
private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker();
6768

6869
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
6970

@@ -187,6 +188,17 @@ public void setAuthenticationUserDetailsService(
187188
this.authenticationUserDetailsService = authenticationUserDetailsService;
188189
}
189190

191+
/**
192+
* Sets the UserDetailsChecker to be used for checking the status of retrieved user
193+
* details. This allows customization of the UserDetailsChecker implementation.
194+
* @param userDetailsChecker the UserDetailsChecker to be set
195+
* @since 6.4
196+
*/
197+
public void setUserDetailsChecker(final UserDetailsChecker userDetailsChecker) {
198+
Assert.notNull(userDetailsChecker, "userDetailsChecker cannot be null");
199+
this.userDetailsChecker = userDetailsChecker;
200+
}
201+
190202
public void setServiceProperties(final ServiceProperties serviceProperties) {
191203
this.serviceProperties = serviceProperties;
192204
}

cas/src/test/java/org/springframework/security/cas/authentication/CasAuthenticationProviderTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.concurrent.atomic.AtomicInteger;
2122

2223
import org.apereo.cas.client.validation.Assertion;
2324
import org.apereo.cas.client.validation.AssertionImpl;
@@ -31,11 +32,13 @@
3132
import org.springframework.security.cas.ServiceProperties;
3233
import org.springframework.security.cas.web.authentication.ServiceAuthenticationDetails;
3334
import org.springframework.security.core.Authentication;
35+
import org.springframework.security.core.AuthenticationException;
3436
import org.springframework.security.core.authority.AuthorityUtils;
3537
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3638
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
3739
import org.springframework.security.core.userdetails.User;
3840
import org.springframework.security.core.userdetails.UserDetails;
41+
import org.springframework.security.core.userdetails.UserDetailsChecker;
3942
import org.springframework.security.core.userdetails.UsernameNotFoundException;
4043
import org.springframework.security.web.authentication.WebAuthenticationDetails;
4144

@@ -55,6 +58,7 @@
5558
*
5659
* @author Ben Alex
5760
* @author Scott Battaglia
61+
* @author Kim Youngwoong
5862
*/
5963
@SuppressWarnings("unchecked")
6064
public class CasAuthenticationProviderTests {
@@ -320,6 +324,28 @@ public void supportsRequiredTokens() {
320324
assertThat(cap.supports(CasAuthenticationToken.class)).isTrue();
321325
}
322326

327+
@Test
328+
public void testSetUserDetailsChecker() throws AuthenticationException {
329+
CasAuthenticationProvider cap = new CasAuthenticationProvider();
330+
cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
331+
cap.setKey("qwerty");
332+
cap.setTicketValidator(new MockTicketValidator(true));
333+
cap.setServiceProperties(makeServiceProperties());
334+
cap.afterPropertiesSet();
335+
CasServiceTicketAuthenticationToken token = CasServiceTicketAuthenticationToken.stateful("ST-123");
336+
337+
AtomicInteger checkCount = new AtomicInteger(0);
338+
UserDetailsChecker userDetailsChecker = new UserDetailsChecker() {
339+
@Override
340+
public void check(UserDetails user) {
341+
checkCount.incrementAndGet();
342+
}
343+
};
344+
cap.setUserDetailsChecker(userDetailsChecker);
345+
346+
assertThat(checkCount.get()).isEqualTo(1);
347+
}
348+
323349
private class MockAuthoritiesPopulator implements AuthenticationUserDetailsService {
324350

325351
@Override

0 commit comments

Comments
 (0)