Skip to content

Commit b7b618f

Browse files
committed
Add webauthn Jackson 3 support and deprecate Jackson 2 one
Since this module was already using the jackson sub-package for Jackson 2 support, both Jackson 2 and Jackson 3 support lives in the same subpackage and the former package-private classes has been renamed with a Jackson2 qualifier. See spring-projectsgh-17832 Signed-off-by: Sébastien Deleuze <[email protected]>
1 parent c1c95d7 commit b7b618f

File tree

80 files changed

+2484
-190
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2484
-190
lines changed

webauthn/spring-security-webauthn.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515

1616
optional 'org.springframework:spring-jdbc'
1717
optional 'org.springframework:spring-tx'
18+
optional 'tools.jackson.core:jackson-databind'
1819

1920
provided 'jakarta.servlet:jakarta.servlet-api'
2021

webauthn/src/main/java/org/springframework/security/web/webauthn/authentication/PublicKeyCredentialRequestOptionsFilter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222
import jakarta.servlet.ServletException;
2323
import jakarta.servlet.http.HttpServletRequest;
2424
import jakarta.servlet.http.HttpServletResponse;
25+
import tools.jackson.databind.json.JsonMapper;
2526

2627
import org.springframework.http.HttpHeaders;
2728
import org.springframework.http.HttpMethod;
2829
import org.springframework.http.MediaType;
2930
import org.springframework.http.converter.HttpMessageConverter;
30-
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
31-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
31+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
3232
import org.springframework.http.server.ServletServerHttpResponse;
3333
import org.springframework.security.core.context.SecurityContext;
3434
import org.springframework.security.core.context.SecurityContextHolder;
3535
import org.springframework.security.core.context.SecurityContextHolderStrategy;
3636
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
3737
import org.springframework.security.web.util.matcher.RequestMatcher;
3838
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions;
39-
import org.springframework.security.web.webauthn.jackson.WebauthnJackson2Module;
39+
import org.springframework.security.web.webauthn.jackson.WebauthnJacksonModule;
4040
import org.springframework.security.web.webauthn.management.ImmutablePublicKeyCredentialRequestOptionsRequest;
4141
import org.springframework.security.web.webauthn.management.WebAuthnRelyingPartyOperations;
4242
import org.springframework.util.Assert;
@@ -63,8 +63,8 @@ public class PublicKeyCredentialRequestOptionsFilter extends OncePerRequestFilte
6363

6464
private PublicKeyCredentialRequestOptionsRepository requestOptionsRepository = new HttpSessionPublicKeyCredentialRequestOptionsRepository();
6565

66-
private HttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter(
67-
Jackson2ObjectMapperBuilder.json().modules(new WebauthnJackson2Module()).build());
66+
private HttpMessageConverter<Object> converter = new JacksonJsonHttpMessageConverter(
67+
JsonMapper.builder().addModule(new WebauthnJacksonModule()).build());
6868

6969
/**
7070
* Creates a new instance with the provided {@link WebAuthnRelyingPartyOperations}.

webauthn/src/main/java/org/springframework/security/web/webauthn/authentication/WebAuthnAuthenticationFilter.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import jakarta.servlet.ServletException;
2222
import jakarta.servlet.http.HttpServletRequest;
2323
import jakarta.servlet.http.HttpServletResponse;
24+
import tools.jackson.databind.json.JsonMapper;
2425

2526
import org.springframework.core.ResolvableType;
2627
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.HttpStatus;
2829
import org.springframework.http.converter.GenericHttpMessageConverter;
29-
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
30-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
30+
import org.springframework.http.converter.SmartHttpMessageConverter;
31+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
3132
import org.springframework.http.server.ServletServerHttpRequest;
3233
import org.springframework.security.authentication.BadCredentialsException;
3334
import org.springframework.security.core.Authentication;
@@ -40,7 +41,7 @@
4041
import org.springframework.security.web.webauthn.api.AuthenticatorAssertionResponse;
4142
import org.springframework.security.web.webauthn.api.PublicKeyCredential;
4243
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions;
43-
import org.springframework.security.web.webauthn.jackson.WebauthnJackson2Module;
44+
import org.springframework.security.web.webauthn.jackson.WebauthnJacksonModule;
4445
import org.springframework.security.web.webauthn.management.RelyingPartyAuthenticationRequest;
4546
import org.springframework.util.Assert;
4647

@@ -49,8 +50,7 @@
4950
/**
5051
* Authenticates {@code PublicKeyCredential<AuthenticatorAssertionResponse>} that is
5152
* parsed from the body of the {@link HttpServletRequest} using the
52-
* {@link #setConverter(GenericHttpMessageConverter)}. An example request is provided
53-
* below:
53+
* {@link #setConverter(SmartHttpMessageConverter)}. An example request is provided below:
5454
*
5555
* <pre>
5656
* {
@@ -72,8 +72,8 @@
7272
*/
7373
public class WebAuthnAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
7474

75-
private GenericHttpMessageConverter<Object> converter = new MappingJackson2HttpMessageConverter(
76-
Jackson2ObjectMapperBuilder.json().modules(new WebauthnJackson2Module()).build());
75+
private SmartHttpMessageConverter<Object> converter = new JacksonJsonHttpMessageConverter(
76+
JsonMapper.builder().addModule(new WebauthnJacksonModule()).build());
7777

7878
private PublicKeyCredentialRequestOptionsRepository requestOptionsRepository = new HttpSessionPublicKeyCredentialRequestOptionsRepository();
7979

@@ -94,7 +94,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
9494
PublicKeyCredential<AuthenticatorAssertionResponse> publicKeyCredential = null;
9595
try {
9696
publicKeyCredential = (PublicKeyCredential<AuthenticatorAssertionResponse>) this.converter
97-
.read(resolvableType.getType(), getClass(), httpRequest);
97+
.read(resolvableType, httpRequest, null);
9898
}
9999
catch (Exception ex) {
100100
throw new BadCredentialsException("Unable to authenticate the PublicKeyCredential", ex);
@@ -114,10 +114,11 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
114114
/**
115115
* Sets the {@link GenericHttpMessageConverter} to use for writing
116116
* {@code PublicKeyCredential<AuthenticatorAssertionResponse>} to the response. The
117-
* default is @{code MappingJackson2HttpMessageConverter}
117+
* default is @{code Jackson2HttpMessageConverter}
118118
* @param converter the {@link GenericHttpMessageConverter} to use. Cannot be null.
119119
*/
120-
public void setConverter(GenericHttpMessageConverter<Object> converter) {
120+
// TODO Accept HttpMessageConverter
121+
public void setConverter(SmartHttpMessageConverter<Object> converter) {
121122
Assert.notNull(converter, "converter cannot be null");
122123
this.converter = converter;
123124
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.webauthn.jackson;
18+
19+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
20+
21+
import org.springframework.security.web.webauthn.api.AttestationConveyancePreference;
22+
23+
/**
24+
* Jackson mixin for {@link AttestationConveyancePreference}
25+
*
26+
* @author Rob Winch
27+
* @since 6.4
28+
* @deprecated as of 7.0 in favor of
29+
* {@link org.springframework.security.web.webauthn.jackson.AttestationConveyancePreferenceMixin}
30+
* based on Jackson 3
31+
*/
32+
@Deprecated(forRemoval = true)
33+
@JsonSerialize(using = AttestationConveyancePreferenceJackson2Serializer.class)
34+
class AttestationConveyancePreferenceJackson2Mixin {
35+
36+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.webauthn.jackson;
18+
19+
import java.io.IOException;
20+
21+
import com.fasterxml.jackson.core.JsonGenerator;
22+
import com.fasterxml.jackson.databind.SerializerProvider;
23+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
24+
25+
import org.springframework.security.web.webauthn.api.AttestationConveyancePreference;
26+
27+
/**
28+
* Jackson serializer for {@link AttestationConveyancePreference}
29+
*
30+
* @author Rob Winch
31+
* @since 6.4
32+
* @deprecated as of 7.0 in favor of
33+
* {@link org.springframework.security.web.webauthn.jackson.AttestationConveyancePreferenceSerializer}
34+
* based on Jackson 3
35+
*/
36+
@Deprecated(forRemoval = true)
37+
@SuppressWarnings("serial")
38+
class AttestationConveyancePreferenceJackson2Serializer extends StdSerializer<AttestationConveyancePreference> {
39+
40+
AttestationConveyancePreferenceJackson2Serializer() {
41+
super(AttestationConveyancePreference.class);
42+
}
43+
44+
@Override
45+
public void serialize(AttestationConveyancePreference preference, JsonGenerator jgen, SerializerProvider provider)
46+
throws IOException {
47+
jgen.writeString(preference.getValue());
48+
}
49+
50+
}

webauthn/src/main/java/org/springframework/security/web/webauthn/jackson/AttestationConveyancePreferenceMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.security.web.webauthn.jackson;
1818

19-
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
19+
import tools.jackson.databind.annotation.JsonSerialize;
2020

2121
import org.springframework.security.web.webauthn.api.AttestationConveyancePreference;
2222

webauthn/src/main/java/org/springframework/security/web/webauthn/jackson/AttestationConveyancePreferenceSerializer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616

1717
package org.springframework.security.web.webauthn.jackson;
1818

19-
import java.io.IOException;
20-
21-
import com.fasterxml.jackson.core.JsonGenerator;
22-
import com.fasterxml.jackson.databind.SerializerProvider;
23-
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
19+
import tools.jackson.core.JacksonException;
20+
import tools.jackson.core.JsonGenerator;
21+
import tools.jackson.databind.SerializationContext;
22+
import tools.jackson.databind.ser.std.StdSerializer;
2423

2524
import org.springframework.security.web.webauthn.api.AttestationConveyancePreference;
2625

@@ -38,8 +37,8 @@ class AttestationConveyancePreferenceSerializer extends StdSerializer<Attestatio
3837
}
3938

4039
@Override
41-
public void serialize(AttestationConveyancePreference preference, JsonGenerator jgen, SerializerProvider provider)
42-
throws IOException {
40+
public void serialize(AttestationConveyancePreference preference, JsonGenerator jgen, SerializationContext ctxt)
41+
throws JacksonException {
4342
jgen.writeString(preference.getValue());
4443
}
4544

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.webauthn.jackson;
18+
19+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
20+
21+
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientInputs;
22+
23+
/**
24+
* Jackson mixin for {@link AuthenticationExtensionsClientInputs}
25+
*
26+
* @author Rob Winch
27+
* @since 6.4
28+
* @deprecated as of 7.0 in favor of
29+
* {@link org.springframework.security.web.webauthn.jackson.AuthenticationExtensionsClientInputMixin}
30+
* based on Jackson 3
31+
*/
32+
@Deprecated(forRemoval = true)
33+
@JsonSerialize(using = AuthenticationExtensionsClientInputJackson2Serializer.class)
34+
class AuthenticationExtensionsClientInputJackson2Mixin {
35+
36+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.webauthn.jackson;
18+
19+
import java.io.IOException;
20+
21+
import com.fasterxml.jackson.core.JsonGenerator;
22+
import com.fasterxml.jackson.databind.SerializerProvider;
23+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
24+
25+
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientInput;
26+
27+
/**
28+
* Provides Jackson serialization of {@link AuthenticationExtensionsClientInput}.
29+
*
30+
* @author Rob Winch
31+
* @since 6.4
32+
* @deprecated as of 7.0 in favor of
33+
* {@link org.springframework.security.web.webauthn.jackson.AuthenticationExtensionsClientInputSerializer}
34+
* based on Jackson 3
35+
*/
36+
@Deprecated(forRemoval = true)
37+
@SuppressWarnings("serial")
38+
class AuthenticationExtensionsClientInputJackson2Serializer extends StdSerializer<AuthenticationExtensionsClientInput> {
39+
40+
/**
41+
* Creates a new instance.
42+
*/
43+
AuthenticationExtensionsClientInputJackson2Serializer() {
44+
super(AuthenticationExtensionsClientInput.class);
45+
}
46+
47+
@Override
48+
public void serialize(AuthenticationExtensionsClientInput input, JsonGenerator jgen, SerializerProvider provider)
49+
throws IOException {
50+
jgen.writeObjectField(input.getExtensionId(), input.getInput());
51+
}
52+
53+
}

webauthn/src/main/java/org/springframework/security/web/webauthn/jackson/AuthenticationExtensionsClientInputMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.security.web.webauthn.jackson;
1818

19-
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
19+
import tools.jackson.databind.annotation.JsonSerialize;
2020

2121
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientInputs;
2222

0 commit comments

Comments
 (0)