Skip to content

Commit 8095c2b

Browse files
committed
Fix NotSerializableException in SerializationSamples
Explicitly configure Instancio for OneTimeTokenAuthenticationToken in SerializationSamples.java. This ensures that the generated test instances use a valid, serializable principal (null) instead of the default Object() which causes serialization failures.
1 parent debfd34 commit 8095c2b

1 file changed

Lines changed: 56 additions & 49 deletions

File tree

config/src/test/java/org/springframework/security/SerializationSamples.java

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ final class SerializationSamples {
284284
Authentication authentication = TestAuthentication.authenticated(user);
285285
SecurityContext securityContext = new SecurityContextImpl(authentication);
286286

287+
instancioByClassName.put(OneTimeTokenAuthenticationToken.class, () -> {
288+
InstancioOfClassApi<?> instancio = Instancio.of(OneTimeTokenAuthenticationToken.class);
289+
instancio.supply(Select.all(OneTimeTokenAuthenticationToken.class),
290+
(r) -> applyDetails(new OneTimeTokenAuthenticationToken("token")));
291+
return instancio;
292+
});
293+
287294
// oauth2-core
288295
generatorByClassName.put(DefaultOAuth2User.class, (r) -> TestOAuth2Users.create());
289296
generatorByClassName.put(OAuth2AuthorizationRequest.class,
@@ -360,9 +367,10 @@ final class SerializationSamples {
360367
generatorByClassName.put(ClientAuthorizationRequiredException.class,
361368
(r) -> new ClientAuthorizationRequiredException("id"));
362369
generatorByClassName
363-
.put(OAuth2AuthorizedClientRefreshedEvent.class, (r) -> new OAuth2AuthorizedClientRefreshedEvent(
364-
TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
365-
new OAuth2AuthorizedClient(clientRegistration, "principal", TestOAuth2AccessTokens.noScopes())));
370+
.put(OAuth2AuthorizedClientRefreshedEvent.class, (r) -> new OAuth2AuthorizedClientRefreshedEvent(
371+
TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
372+
new OAuth2AuthorizedClient(clientRegistration, "principal",
373+
TestOAuth2AccessTokens.noScopes())));
366374
generatorByClassName.put(OidcUserRefreshedEvent.class,
367375
(r) -> new OidcUserRefreshedEvent(TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
368376
TestOidcUsers.create(), TestOidcUsers.create(), authentication));
@@ -410,28 +418,28 @@ final class SerializationSamples {
410418
(r) -> applyDetails(new DPoPAuthenticationToken("token", "proof", "method", "uri")));
411419
generatorByClassName.put(OAuth2ProtectedResourceMetadata.class,
412420
(r) -> OAuth2ProtectedResourceMetadata.builder()
413-
.resource("https://localhost/resource")
414-
.authorizationServer("https://localhost/authorizationServer")
415-
.scope("scope")
416-
.bearerMethod("bearerMethod")
417-
.resourceName("resourceName")
418-
.tlsClientCertificateBoundAccessTokens(true)
419-
.build());
421+
.resource("https://localhost/resource")
422+
.authorizationServer("https://localhost/authorizationServer")
423+
.scope("scope")
424+
.bearerMethod("bearerMethod")
425+
.resourceName("resourceName")
426+
.tlsClientCertificateBoundAccessTokens(true)
427+
.build());
420428

421429
// oauth2-authorization-server
422430
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
423431
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
424432
OAuth2AuthorizationRequest authorizationRequest = authorization
425-
.getAttribute(OAuth2AuthorizationRequest.class.getName());
433+
.getAttribute(OAuth2AuthorizationRequest.class.getName());
426434
Authentication principal = authorization.getAttribute(Principal.class.getName());
427435
generatorByClassName.put(RegisteredClient.class, (r) -> registeredClient);
428436
generatorByClassName.put(OAuth2Authorization.class, (r) -> authorization);
429437
generatorByClassName.put(OAuth2Authorization.Token.class, (r) -> authorization.getAccessToken());
430438
generatorByClassName.put(OAuth2AuthorizationConsent.class,
431439
(r) -> OAuth2AuthorizationConsent.withId("registeredClientId", "principalName")
432-
.scope("scope1")
433-
.scope("scope2")
434-
.build());
440+
.scope("scope1")
441+
.scope("scope2")
442+
.build());
435443
generatorByClassName.put(OAuth2AuthorizationCodeRequestAuthenticationToken.class, (r) -> {
436444
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationToken = new OAuth2AuthorizationCodeRequestAuthenticationToken(
437445
"authorizationUri", "clientId", principal, "redirectUri", "state", authorizationRequest.getScopes(),
@@ -492,10 +500,10 @@ final class SerializationSamples {
492500
return authenticationToken;
493501
});
494502
OAuth2ClientRegistration oauth2ClientRegistration = OAuth2ClientRegistration.builder()
495-
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
496-
.scope("scope1")
497-
.redirectUri("https://localhost/oauth2/callback")
498-
.build();
503+
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
504+
.scope("scope1")
505+
.redirectUri("https://localhost/oauth2/callback")
506+
.build();
499507
generatorByClassName.put(OAuth2ClientRegistration.class, (r) -> oauth2ClientRegistration);
500508
generatorByClassName.put(OAuth2ClientRegistrationAuthenticationToken.class, (r) -> {
501509
OAuth2ClientRegistrationAuthenticationToken authenticationToken = new OAuth2ClientRegistrationAuthenticationToken(
@@ -504,10 +512,10 @@ final class SerializationSamples {
504512
return authenticationToken;
505513
});
506514
OidcClientRegistration oidcClientRegistration = OidcClientRegistration.builder()
507-
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
508-
.scope("scope1")
509-
.redirectUri("https://localhost/oauth2/callback")
510-
.build();
515+
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
516+
.scope("scope1")
517+
.redirectUri("https://localhost/oauth2/callback")
518+
.build();
511519
generatorByClassName.put(OidcClientRegistration.class, (r) -> oidcClientRegistration);
512520
generatorByClassName.put(OidcClientRegistrationAuthenticationToken.class, (r) -> {
513521
OidcClientRegistrationAuthenticationToken authenticationToken = new OidcClientRegistrationAuthenticationToken(
@@ -524,9 +532,9 @@ final class SerializationSamples {
524532
});
525533
generatorByClassName.put(OidcLogoutAuthenticationToken.class, (r) -> {
526534
OidcIdToken idToken = OidcIdToken.withTokenValue("tokenValue")
527-
.issuedAt(Instant.now())
528-
.expiresAt(Instant.now().plusSeconds(60))
529-
.build();
535+
.issuedAt(Instant.now())
536+
.expiresAt(Instant.now().plusSeconds(60))
537+
.build();
530538
OidcLogoutAuthenticationToken authenticationToken = new OidcLogoutAuthenticationToken(idToken, principal,
531539
"sessionId", "clientId", "postLogoutRedirectUri", "state");
532540
authenticationToken.setDetails(details);
@@ -548,21 +556,21 @@ final class SerializationSamples {
548556
});
549557
generatorByClassName.put(OAuth2AuthorizationServerMetadata.class,
550558
(r) -> OAuth2AuthorizationServerMetadata.builder()
551-
.issuer("https://localhost")
552-
.authorizationEndpoint("https://localhost/oauth2/authorize")
553-
.tokenEndpoint("https://localhost/oauth2/token")
554-
.responseType("code")
555-
.build());
559+
.issuer("https://localhost")
560+
.authorizationEndpoint("https://localhost/oauth2/authorize")
561+
.tokenEndpoint("https://localhost/oauth2/token")
562+
.responseType("code")
563+
.build());
556564
generatorByClassName.put(OidcProviderConfiguration.class,
557565
(r) -> OidcProviderConfiguration.builder()
558-
.issuer("https://localhost")
559-
.authorizationEndpoint("https://localhost/oauth2/authorize")
560-
.tokenEndpoint("https://localhost/oauth2/token")
561-
.jwkSetUrl("https://localhost/oauth2/jwks")
562-
.responseType("code")
563-
.subjectType("subjectType")
564-
.idTokenSigningAlgorithm("RS256")
565-
.build());
566+
.issuer("https://localhost")
567+
.authorizationEndpoint("https://localhost/oauth2/authorize")
568+
.tokenEndpoint("https://localhost/oauth2/token")
569+
.jwkSetUrl("https://localhost/oauth2/jwks")
570+
.responseType("code")
571+
.subjectType("subjectType")
572+
.idTokenSigningAlgorithm("RS256")
573+
.build());
566574
generatorByClassName.put(OAuth2TokenType.class, (r) -> OAuth2TokenType.ACCESS_TOKEN);
567575
generatorByClassName.put(OAuth2TokenFormat.class, (r) -> OAuth2TokenFormat.SELF_CONTAINED);
568576
generatorByClassName.put(AuthorizationServerSettings.class,
@@ -597,8 +605,7 @@ final class SerializationSamples {
597605
token.setDetails(details);
598606
return token;
599607
});
600-
generatorByClassName.put(OneTimeTokenAuthenticationToken.class,
601-
(r) -> applyDetails(new OneTimeTokenAuthenticationToken("token")));
608+
602609
generatorByClassName.put(OneTimeTokenAuthentication.class,
603610
(r) -> applyDetails(new OneTimeTokenAuthentication("username", authentication.getAuthorities())));
604611
generatorByClassName.put(AccessDeniedException.class,
@@ -722,10 +729,10 @@ final class SerializationSamples {
722729
Saml2Authentication saml2 = TestSaml2Authentications.authentication();
723730
generatorByClassName.put(Saml2Authentication.class, (r) -> applyDetails(saml2));
724731
Saml2ResponseAssertionAccessor assertion = Saml2ResponseAssertion.withResponseValue("response")
725-
.nameId("name")
726-
.sessionIndexes(List.of("id"))
727-
.attributes(Map.of("key", List.of("value")))
728-
.build();
732+
.nameId("name")
733+
.sessionIndexes(List.of("id"))
734+
.attributes(Map.of("key", List.of("value")))
735+
.build();
729736
generatorByClassName.put(Saml2ResponseAssertion.class, (r) -> assertion);
730737
generatorByClassName.put(Saml2AssertionAuthentication.class, (r) -> applyDetails(
731738
new Saml2AssertionAuthentication(assertion, authentication.getAuthorities(), "id")));
@@ -746,9 +753,9 @@ final class SerializationSamples {
746753
generatorByClassName.put(Saml2LogoutRequest.class, (r) -> TestSaml2LogoutRequests.create());
747754
generatorByClassName.put(OpenSamlAssertingPartyDetails.class,
748755
(r) -> OpenSamlAssertingPartyDetails
749-
.withEntityDescriptor(
750-
TestOpenSamlObjects.entityDescriptor(TestRelyingPartyRegistrations.full().build()))
751-
.build());
756+
.withEntityDescriptor(
757+
TestOpenSamlObjects.entityDescriptor(TestRelyingPartyRegistrations.full().build()))
758+
.build());
752759

753760
// web
754761
generatorByClassName.put(AnonymousAuthenticationToken.class, (r) -> {
@@ -867,8 +874,8 @@ final class SerializationSamples {
867874
(r) -> TestPublicKeyCredentialUserEntities.userEntity().id(TestBytes.get()).build());
868875
generatorByClassName.put(WebAuthnAuthentication.class, (r) -> {
869876
PublicKeyCredentialUserEntity userEntity = TestPublicKeyCredentialUserEntities.userEntity()
870-
.id(TestBytes.get())
871-
.build();
877+
.id(TestBytes.get())
878+
.build();
872879
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
873880
WebAuthnAuthentication webAuthnAuthentication = new WebAuthnAuthentication(userEntity, authorities);
874881
webAuthnAuthentication.setDetails(details);

0 commit comments

Comments
 (0)