-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
200 additions
and
38 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...g/src/main/java/org/springframework/security/config/web/server/HttpMessageConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2002-2024 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.config.web.server; | ||
|
||
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.JsonbHttpMessageConverter; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.util.ClassUtils; | ||
|
||
/** | ||
* Utility methods for {@link HttpMessageConverter}'s. | ||
* | ||
* @author Joe Grandja | ||
* @author luamas | ||
* @since 5.1 | ||
*/ | ||
final class HttpMessageConverters { | ||
|
||
private static final boolean jackson2Present; | ||
|
||
private static final boolean gsonPresent; | ||
|
||
private static final boolean jsonbPresent; | ||
|
||
static { | ||
ClassLoader classLoader = HttpMessageConverters.class.getClassLoader(); | ||
jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) | ||
&& ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader); | ||
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader); | ||
jsonbPresent = ClassUtils.isPresent("jakarta.json.bind.Jsonb", classLoader); | ||
} | ||
|
||
private HttpMessageConverters() { | ||
} | ||
|
||
static GenericHttpMessageConverter<Object> getJsonMessageConverter() { | ||
if (jackson2Present) { | ||
return new MappingJackson2HttpMessageConverter(); | ||
} | ||
if (gsonPresent) { | ||
return new GsonHttpMessageConverter(); | ||
} | ||
if (jsonbPresent) { | ||
return new JsonbHttpMessageConverter(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
config/src/main/java/org/springframework/security/config/web/server/OAuth2ErrorEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2002-2024 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.config.web.server; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.reactivestreams.Publisher; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.core.ResolvableType; | ||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.core.io.buffer.DataBufferFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.codec.HttpMessageEncoder; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.security.oauth2.core.OAuth2Error; | ||
import org.springframework.util.MimeType; | ||
|
||
class OAuth2ErrorEncoder implements HttpMessageEncoder<OAuth2Error> { | ||
|
||
private final HttpMessageConverter<Object> messageConverter = HttpMessageConverters.getJsonMessageConverter(); | ||
|
||
@NotNull | ||
@Override | ||
public List<MediaType> getStreamingMediaTypes() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public boolean canEncode(ResolvableType elementType, MimeType mimeType) { | ||
return getEncodableMimeTypes().contains(mimeType); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Flux<DataBuffer> encode(Publisher<? extends OAuth2Error> error, DataBufferFactory bufferFactory, | ||
ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) { | ||
return Mono.from(error).flatMap((data) -> { | ||
ByteArrayHttpOutputMessage bytes = new ByteArrayHttpOutputMessage(); | ||
try { | ||
this.messageConverter.write(data, MediaType.APPLICATION_JSON, bytes); | ||
return Mono.just(bytes.getBody().toByteArray()); | ||
} | ||
catch (IOException ex) { | ||
return Mono.error(ex); | ||
} | ||
}).map(bufferFactory::wrap).flux(); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public List<MimeType> getEncodableMimeTypes() { | ||
return List.of(MediaType.APPLICATION_JSON); | ||
} | ||
|
||
private static class ByteArrayHttpOutputMessage implements HttpOutputMessage { | ||
|
||
private final ByteArrayOutputStream body = new ByteArrayOutputStream(); | ||
|
||
@NotNull | ||
@Override | ||
public ByteArrayOutputStream getBody() { | ||
return this.body; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public HttpHeaders getHeaders() { | ||
return new HttpHeaders(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters