From fb84e24893429dea81d3186cdb5a3f92201dd341 Mon Sep 17 00:00:00 2001 From: Robert Winch <362503+rwinch@users.noreply.github.com> Date: Fri, 27 Feb 2026 23:21:27 -0600 Subject: [PATCH] HttpMessageConverterAuthenticationSuccessHandler Supports Jackson 3 Closes gh-18804 --- .../GenericHttpMessageConverterAdapter.java | 99 +++++++++++++++++++ ...ConverterAuthenticationSuccessHandler.java | 2 +- .../authentication/HttpMessageConverters.java | 88 +++++++++++++++++ 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 web/src/main/java/org/springframework/security/web/authentication/GenericHttpMessageConverterAdapter.java create mode 100644 web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverters.java diff --git a/web/src/main/java/org/springframework/security/web/authentication/GenericHttpMessageConverterAdapter.java b/web/src/main/java/org/springframework/security/web/authentication/GenericHttpMessageConverterAdapter.java new file mode 100644 index 0000000000..693f51dbdb --- /dev/null +++ b/web/src/main/java/org/springframework/security/web/authentication/GenericHttpMessageConverterAdapter.java @@ -0,0 +1,99 @@ +/* + * Copyright 2004-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.security.web.authentication; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.List; + +import org.jspecify.annotations.Nullable; + +import org.springframework.core.ResolvableType; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.GenericHttpMessageConverter; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.http.converter.HttpMessageNotWritableException; +import org.springframework.http.converter.SmartHttpMessageConverter; + +/** + * {@link GenericHttpMessageConverter} implementation that delegates to a + * {@link SmartHttpMessageConverter}. + * + * @param the converted object type + * @author Sebastien Deleuze + * @since 7.0 + */ +final class GenericHttpMessageConverterAdapter implements GenericHttpMessageConverter { + + private final SmartHttpMessageConverter smartConverter; + + GenericHttpMessageConverterAdapter(SmartHttpMessageConverter smartConverter) { + this.smartConverter = smartConverter; + } + + @Override + public boolean canRead(Type type, @Nullable Class contextClass, @Nullable MediaType mediaType) { + return this.smartConverter.canRead(ResolvableType.forType(type), mediaType); + } + + @Override + public T read(Type type, @Nullable Class contextClass, HttpInputMessage inputMessage) + throws IOException, HttpMessageNotReadableException { + return this.smartConverter.read(ResolvableType.forType(type), inputMessage, null); + } + + @Override + public boolean canWrite(@Nullable Type type, Class clazz, @Nullable MediaType mediaType) { + return this.smartConverter.canWrite(ResolvableType.forType(type), clazz, mediaType); + } + + @Override + public void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage) + throws IOException, HttpMessageNotWritableException { + this.smartConverter.write(t, ResolvableType.forType(type), contentType, outputMessage, null); + } + + @Override + public boolean canRead(Class clazz, @Nullable MediaType mediaType) { + return this.smartConverter.canRead(ResolvableType.forClass(clazz), mediaType); + } + + @Override + public boolean canWrite(Class clazz, @Nullable MediaType mediaType) { + return this.smartConverter.canWrite(clazz, mediaType); + } + + @Override + public List getSupportedMediaTypes() { + return this.smartConverter.getSupportedMediaTypes(); + } + + @Override + public T read(Class clazz, HttpInputMessage inputMessage) + throws IOException, HttpMessageNotReadableException { + return this.smartConverter.read(clazz, inputMessage); + } + + @Override + public void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) + throws IOException, HttpMessageNotWritableException { + this.smartConverter.write(t, contentType, outputMessage); + } + +} diff --git a/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverterAuthenticationSuccessHandler.java b/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverterAuthenticationSuccessHandler.java index 060527f08d..2e8e0fe85c 100644 --- a/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverterAuthenticationSuccessHandler.java +++ b/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverterAuthenticationSuccessHandler.java @@ -49,7 +49,7 @@ import org.springframework.util.Assert; */ public final class HttpMessageConverterAuthenticationSuccessHandler implements AuthenticationSuccessHandler { - private HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); + private HttpMessageConverter converter = HttpMessageConverters.getJsonMessageConverter(); private RequestCache requestCache = new HttpSessionRequestCache(); diff --git a/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverters.java b/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverters.java new file mode 100644 index 0000000000..dd168d8eeb --- /dev/null +++ b/web/src/main/java/org/springframework/security/web/authentication/HttpMessageConverters.java @@ -0,0 +1,88 @@ +/* + * Copyright 2004-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.security.web.authentication; + +import org.springframework.http.converter.GenericHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.GsonHttpMessageConverter; +import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter; +import org.springframework.http.converter.json.JsonbHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.util.ClassUtils; + +/** + * Utility methods for {@link HttpMessageConverter}'s. + * + * @author Rob Winch + * @since 7.0.4 + */ +final class HttpMessageConverters { + + private static final boolean jacksonPresent; + + private static final boolean jackson2Present; + + private static final boolean gsonPresent; + + private static final boolean jsonbPresent; + + private static final String JSON_MAPPER = "tools.jackson.databind.json.JsonMapper"; + + private static final String OBJECT_MAPPER = "com.fasterxml.jackson.databind.ObjectMapper"; + + private static final String JSON_GENERATOR = "com.fasterxml.jackson.core.JsonGenerator"; + + private static final String GSON = "com.google.gson.Gson"; + + private static final String JSONB = "jakarta.json.bind.Jsonb"; + + static { + ClassLoader classLoader = HttpMessageConverters.class.getClassLoader(); + jacksonPresent = ClassUtils.isPresent(JSON_MAPPER, classLoader); + jackson2Present = ClassUtils.isPresent(OBJECT_MAPPER, classLoader) + && ClassUtils.isPresent(JSON_GENERATOR, classLoader); + gsonPresent = ClassUtils.isPresent(GSON, classLoader); + jsonbPresent = ClassUtils.isPresent(JSONB, classLoader); + } + + private HttpMessageConverters() { + } + + /** + * Gets the {@link GenericHttpMessageConverterAdapter} to use for JSON. + * @return the {@link GenericHttpMessageConverterAdapter} to use. + */ + @SuppressWarnings("removal") + static GenericHttpMessageConverter getJsonMessageConverter() { + if (jacksonPresent) { + return new GenericHttpMessageConverterAdapter<>(new JacksonJsonHttpMessageConverter()); + } + if (jackson2Present) { + return new MappingJackson2HttpMessageConverter(); + } + if (gsonPresent) { + return new GsonHttpMessageConverter(); + } + if (jsonbPresent) { + return new JsonbHttpMessageConverter(); + } + throw new IllegalStateException( + "Cannot find JSON Converter on the classpath. Add one following classes to the classpath " + + String.join(", ", JSON_MAPPER, OBJECT_MAPPER, JSON_MAPPER, GSON, JSONB)); + } + +}