Merge HttpMessageConverterAuthenticationSuccessHandler Supports Jackson 3

This commit is contained in:
Robert Winch 2026-03-02 11:32:59 -06:00
commit 0bb697c4a7
No known key found for this signature in database
3 changed files with 188 additions and 1 deletions

View File

@ -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 <T> the converted object type
* @author Sebastien Deleuze
* @since 7.0
*/
final class GenericHttpMessageConverterAdapter<T> implements GenericHttpMessageConverter<T> {
private final SmartHttpMessageConverter<T> smartConverter;
GenericHttpMessageConverterAdapter(SmartHttpMessageConverter<T> 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<MediaType> getSupportedMediaTypes() {
return this.smartConverter.getSupportedMediaTypes();
}
@Override
public T read(Class<? extends T> 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);
}
}

View File

@ -49,7 +49,7 @@ import org.springframework.util.Assert;
*/
public final class HttpMessageConverterAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private HttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter();
private HttpMessageConverter<Object> converter = HttpMessageConverters.getJsonMessageConverter();
private RequestCache requestCache = new HttpSessionRequestCache();

View File

@ -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<Object> 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));
}
}