Skip to content

Commit b69a4ef

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 8095c2b commit b69a4ef

1 file changed

Lines changed: 47 additions & 48 deletions

File tree

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

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,9 @@ final class SerializationSamples {
367367
generatorByClassName.put(ClientAuthorizationRequiredException.class,
368368
(r) -> new ClientAuthorizationRequiredException("id"));
369369
generatorByClassName
370-
.put(OAuth2AuthorizedClientRefreshedEvent.class, (r) -> new OAuth2AuthorizedClientRefreshedEvent(
371-
TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
372-
new OAuth2AuthorizedClient(clientRegistration, "principal",
373-
TestOAuth2AccessTokens.noScopes())));
370+
.put(OAuth2AuthorizedClientRefreshedEvent.class, (r) -> new OAuth2AuthorizedClientRefreshedEvent(
371+
TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
372+
new OAuth2AuthorizedClient(clientRegistration, "principal", TestOAuth2AccessTokens.noScopes())));
374373
generatorByClassName.put(OidcUserRefreshedEvent.class,
375374
(r) -> new OidcUserRefreshedEvent(TestOAuth2AccessTokenResponses.accessTokenResponse().build(),
376375
TestOidcUsers.create(), TestOidcUsers.create(), authentication));
@@ -418,28 +417,28 @@ final class SerializationSamples {
418417
(r) -> applyDetails(new DPoPAuthenticationToken("token", "proof", "method", "uri")));
419418
generatorByClassName.put(OAuth2ProtectedResourceMetadata.class,
420419
(r) -> OAuth2ProtectedResourceMetadata.builder()
421-
.resource("https://localhost/resource")
422-
.authorizationServer("https://localhost/authorizationServer")
423-
.scope("scope")
424-
.bearerMethod("bearerMethod")
425-
.resourceName("resourceName")
426-
.tlsClientCertificateBoundAccessTokens(true)
427-
.build());
420+
.resource("https://localhost/resource")
421+
.authorizationServer("https://localhost/authorizationServer")
422+
.scope("scope")
423+
.bearerMethod("bearerMethod")
424+
.resourceName("resourceName")
425+
.tlsClientCertificateBoundAccessTokens(true)
426+
.build());
428427

429428
// oauth2-authorization-server
430429
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
431430
OAuth2Authorization authorization = TestOAuth2Authorizations.authorization(registeredClient).build();
432431
OAuth2AuthorizationRequest authorizationRequest = authorization
433-
.getAttribute(OAuth2AuthorizationRequest.class.getName());
432+
.getAttribute(OAuth2AuthorizationRequest.class.getName());
434433
Authentication principal = authorization.getAttribute(Principal.class.getName());
435434
generatorByClassName.put(RegisteredClient.class, (r) -> registeredClient);
436435
generatorByClassName.put(OAuth2Authorization.class, (r) -> authorization);
437436
generatorByClassName.put(OAuth2Authorization.Token.class, (r) -> authorization.getAccessToken());
438437
generatorByClassName.put(OAuth2AuthorizationConsent.class,
439438
(r) -> OAuth2AuthorizationConsent.withId("registeredClientId", "principalName")
440-
.scope("scope1")
441-
.scope("scope2")
442-
.build());
439+
.scope("scope1")
440+
.scope("scope2")
441+
.build());
443442
generatorByClassName.put(OAuth2AuthorizationCodeRequestAuthenticationToken.class, (r) -> {
444443
OAuth2AuthorizationCodeRequestAuthenticationToken authenticationToken = new OAuth2AuthorizationCodeRequestAuthenticationToken(
445444
"authorizationUri", "clientId", principal, "redirectUri", "state", authorizationRequest.getScopes(),
@@ -500,10 +499,10 @@ final class SerializationSamples {
500499
return authenticationToken;
501500
});
502501
OAuth2ClientRegistration oauth2ClientRegistration = OAuth2ClientRegistration.builder()
503-
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
504-
.scope("scope1")
505-
.redirectUri("https://localhost/oauth2/callback")
506-
.build();
502+
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
503+
.scope("scope1")
504+
.redirectUri("https://localhost/oauth2/callback")
505+
.build();
507506
generatorByClassName.put(OAuth2ClientRegistration.class, (r) -> oauth2ClientRegistration);
508507
generatorByClassName.put(OAuth2ClientRegistrationAuthenticationToken.class, (r) -> {
509508
OAuth2ClientRegistrationAuthenticationToken authenticationToken = new OAuth2ClientRegistrationAuthenticationToken(
@@ -512,10 +511,10 @@ final class SerializationSamples {
512511
return authenticationToken;
513512
});
514513
OidcClientRegistration oidcClientRegistration = OidcClientRegistration.builder()
515-
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
516-
.scope("scope1")
517-
.redirectUri("https://localhost/oauth2/callback")
518-
.build();
514+
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
515+
.scope("scope1")
516+
.redirectUri("https://localhost/oauth2/callback")
517+
.build();
519518
generatorByClassName.put(OidcClientRegistration.class, (r) -> oidcClientRegistration);
520519
generatorByClassName.put(OidcClientRegistrationAuthenticationToken.class, (r) -> {
521520
OidcClientRegistrationAuthenticationToken authenticationToken = new OidcClientRegistrationAuthenticationToken(
@@ -532,9 +531,9 @@ final class SerializationSamples {
532531
});
533532
generatorByClassName.put(OidcLogoutAuthenticationToken.class, (r) -> {
534533
OidcIdToken idToken = OidcIdToken.withTokenValue("tokenValue")
535-
.issuedAt(Instant.now())
536-
.expiresAt(Instant.now().plusSeconds(60))
537-
.build();
534+
.issuedAt(Instant.now())
535+
.expiresAt(Instant.now().plusSeconds(60))
536+
.build();
538537
OidcLogoutAuthenticationToken authenticationToken = new OidcLogoutAuthenticationToken(idToken, principal,
539538
"sessionId", "clientId", "postLogoutRedirectUri", "state");
540539
authenticationToken.setDetails(details);
@@ -556,21 +555,21 @@ final class SerializationSamples {
556555
});
557556
generatorByClassName.put(OAuth2AuthorizationServerMetadata.class,
558557
(r) -> OAuth2AuthorizationServerMetadata.builder()
559-
.issuer("https://localhost")
560-
.authorizationEndpoint("https://localhost/oauth2/authorize")
561-
.tokenEndpoint("https://localhost/oauth2/token")
562-
.responseType("code")
563-
.build());
558+
.issuer("https://localhost")
559+
.authorizationEndpoint("https://localhost/oauth2/authorize")
560+
.tokenEndpoint("https://localhost/oauth2/token")
561+
.responseType("code")
562+
.build());
564563
generatorByClassName.put(OidcProviderConfiguration.class,
565564
(r) -> OidcProviderConfiguration.builder()
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());
565+
.issuer("https://localhost")
566+
.authorizationEndpoint("https://localhost/oauth2/authorize")
567+
.tokenEndpoint("https://localhost/oauth2/token")
568+
.jwkSetUrl("https://localhost/oauth2/jwks")
569+
.responseType("code")
570+
.subjectType("subjectType")
571+
.idTokenSigningAlgorithm("RS256")
572+
.build());
574573
generatorByClassName.put(OAuth2TokenType.class, (r) -> OAuth2TokenType.ACCESS_TOKEN);
575574
generatorByClassName.put(OAuth2TokenFormat.class, (r) -> OAuth2TokenFormat.SELF_CONTAINED);
576575
generatorByClassName.put(AuthorizationServerSettings.class,
@@ -729,10 +728,10 @@ final class SerializationSamples {
729728
Saml2Authentication saml2 = TestSaml2Authentications.authentication();
730729
generatorByClassName.put(Saml2Authentication.class, (r) -> applyDetails(saml2));
731730
Saml2ResponseAssertionAccessor assertion = Saml2ResponseAssertion.withResponseValue("response")
732-
.nameId("name")
733-
.sessionIndexes(List.of("id"))
734-
.attributes(Map.of("key", List.of("value")))
735-
.build();
731+
.nameId("name")
732+
.sessionIndexes(List.of("id"))
733+
.attributes(Map.of("key", List.of("value")))
734+
.build();
736735
generatorByClassName.put(Saml2ResponseAssertion.class, (r) -> assertion);
737736
generatorByClassName.put(Saml2AssertionAuthentication.class, (r) -> applyDetails(
738737
new Saml2AssertionAuthentication(assertion, authentication.getAuthorities(), "id")));
@@ -753,9 +752,9 @@ final class SerializationSamples {
753752
generatorByClassName.put(Saml2LogoutRequest.class, (r) -> TestSaml2LogoutRequests.create());
754753
generatorByClassName.put(OpenSamlAssertingPartyDetails.class,
755754
(r) -> OpenSamlAssertingPartyDetails
756-
.withEntityDescriptor(
757-
TestOpenSamlObjects.entityDescriptor(TestRelyingPartyRegistrations.full().build()))
758-
.build());
755+
.withEntityDescriptor(
756+
TestOpenSamlObjects.entityDescriptor(TestRelyingPartyRegistrations.full().build()))
757+
.build());
759758

760759
// web
761760
generatorByClassName.put(AnonymousAuthenticationToken.class, (r) -> {
@@ -874,8 +873,8 @@ final class SerializationSamples {
874873
(r) -> TestPublicKeyCredentialUserEntities.userEntity().id(TestBytes.get()).build());
875874
generatorByClassName.put(WebAuthnAuthentication.class, (r) -> {
876875
PublicKeyCredentialUserEntity userEntity = TestPublicKeyCredentialUserEntities.userEntity()
877-
.id(TestBytes.get())
878-
.build();
876+
.id(TestBytes.get())
877+
.build();
879878
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
880879
WebAuthnAuthentication webAuthnAuthentication = new WebAuthnAuthentication(userEntity, authorities);
881880
webAuthnAuthentication.setDetails(details);

0 commit comments

Comments
 (0)