Skip to content

Commit 65a14d6

Browse files
sdeleuzerwinch
authored andcommitted
Add Jackson 3 support
This commit adds support for Jackson 3 which has the following major differences with the Jackson 2 one: - jackson subpackage instead of jackson2 - Jackson type prefix instead of Jackson2 - JsonMapper instead of ObjectMapper - For configuration, JsonMapper.Builder instead of ObjectMapper since the latter is now immutable - Remove custom support for unmodifiable collections - Use safe default typing via a PolymorphicTypeValidator Jackson 3 changes compared to Jackson 2 are documented in https://cowtowncoder.medium.com/jackson-3-0-0-ga-released-1f669cda529a and https://github.com/FasterXML/jackson/blob/main/jackson3/MIGRATING_TO_JACKSON_3.md. This commit does not cover webauthn which is a special case (uses jackson sub-package for Jackson 2 support) which will be handled in a distinct commit. See gh-17832 Signed-off-by: Sébastien Deleuze <[email protected]>
1 parent 916a687 commit 65a14d6

File tree

156 files changed

+9052
-146
lines changed

Some content is hidden

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

156 files changed

+9052
-146
lines changed

cas/spring-security-cas.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies {
1515
api 'org.springframework:spring-web'
1616

1717
optional 'com.fasterxml.jackson.core:jackson-databind'
18+
optional 'tools.jackson.core:jackson-databind'
1819

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.cas.jackson;
18+
19+
import java.util.Date;
20+
import java.util.Map;
21+
22+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
23+
import com.fasterxml.jackson.annotation.JsonCreator;
24+
import com.fasterxml.jackson.annotation.JsonProperty;
25+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
26+
import org.apereo.cas.client.authentication.AttributePrincipal;
27+
28+
/**
29+
* Helps in jackson deserialization of class
30+
* {@link org.apereo.cas.client.validation.AssertionImpl}, which is used with
31+
* {@link org.springframework.security.cas.authentication.CasAuthenticationToken}.
32+
*
33+
* @author Sebastien Deleuze
34+
* @author Jitendra Singh
35+
* @since 7.0
36+
* @see CasJacksonModule
37+
* @see org.springframework.security.jackson.SecurityJacksonModules
38+
*/
39+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
40+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
41+
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
42+
class AssertionImplMixin {
43+
44+
/**
45+
* Mixin Constructor helps in deserialize
46+
* {@link org.apereo.cas.client.validation.AssertionImpl}
47+
* @param principal the Principal to associate with the Assertion.
48+
* @param validFromDate when the assertion is valid from.
49+
* @param validUntilDate when the assertion is valid to.
50+
* @param authenticationDate when the assertion is authenticated.
51+
* @param attributes the key/value pairs for this attribute.
52+
*/
53+
@JsonCreator
54+
AssertionImplMixin(@JsonProperty("principal") AttributePrincipal principal,
55+
@JsonProperty("validFromDate") Date validFromDate, @JsonProperty("validUntilDate") Date validUntilDate,
56+
@JsonProperty("authenticationDate") Date authenticationDate,
57+
@JsonProperty("attributes") Map<String, Object> attributes) {
58+
}
59+
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.cas.jackson;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
22+
import com.fasterxml.jackson.annotation.JsonCreator;
23+
import com.fasterxml.jackson.annotation.JsonProperty;
24+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
25+
import org.apereo.cas.client.proxy.ProxyRetriever;
26+
27+
/**
28+
* Helps in deserialize
29+
* {@link org.apereo.cas.client.authentication.AttributePrincipalImpl} which is used with
30+
* {@link org.springframework.security.cas.authentication.CasAuthenticationToken}.
31+
*
32+
* @author Sebastien Deleuze
33+
* @author Jitendra Singh
34+
* @since 7.0
35+
* @see CasJacksonModule
36+
* @see org.springframework.security.jackson.SecurityJacksonModules
37+
*/
38+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
39+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
40+
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
41+
class AttributePrincipalImplMixin {
42+
43+
/**
44+
* Mixin Constructor helps in deserialize
45+
* {@link org.apereo.cas.client.authentication.AttributePrincipalImpl}
46+
* @param name the unique identifier for the principal.
47+
* @param attributes the key/value pairs for this principal.
48+
* @param proxyGrantingTicket the ticket associated with this principal.
49+
* @param proxyRetriever the ProxyRetriever implementation to call back to the CAS
50+
* server.
51+
*/
52+
@JsonCreator
53+
AttributePrincipalImplMixin(@JsonProperty("name") String name,
54+
@JsonProperty("attributes") Map<String, Object> attributes,
55+
@JsonProperty("proxyGrantingTicket") String proxyGrantingTicket,
56+
@JsonProperty("proxyRetriever") ProxyRetriever proxyRetriever) {
57+
}
58+
59+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.cas.jackson;
18+
19+
import java.util.Collection;
20+
21+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
22+
import com.fasterxml.jackson.annotation.JsonCreator;
23+
import com.fasterxml.jackson.annotation.JsonProperty;
24+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
25+
import org.apereo.cas.client.validation.Assertion;
26+
27+
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
28+
import org.springframework.security.cas.authentication.CasAuthenticationToken;
29+
import org.springframework.security.core.GrantedAuthority;
30+
import org.springframework.security.core.userdetails.UserDetails;
31+
32+
/**
33+
* Mixin class which helps in deserialize {@link CasAuthenticationToken} using jackson.
34+
*
35+
* @author Sebastien Deleuze
36+
* @author Jitendra Singh
37+
* @since 7.0
38+
* @see CasJacksonModule
39+
* @see org.springframework.security.jackson.SecurityJacksonModules
40+
*/
41+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
42+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, isGetterVisibility = JsonAutoDetect.Visibility.NONE,
43+
getterVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.ANY)
44+
class CasAuthenticationTokenMixin {
45+
46+
/**
47+
* Mixin Constructor helps in deserialize {@link CasAuthenticationToken}
48+
* @param keyHash hashCode of provided key to identify if this object made by a given
49+
* {@link CasAuthenticationProvider}
50+
* @param principal typically the UserDetails object (cannot be <code>null</code>)
51+
* @param credentials the service/proxy ticket ID from CAS (cannot be
52+
* <code>null</code>)
53+
* @param authorities the authorities granted to the user (from the
54+
* {@link org.springframework.security.core.userdetails.UserDetailsService}) (cannot
55+
* be <code>null</code>)
56+
* @param userDetails the user details (from the
57+
* {@link org.springframework.security.core.userdetails.UserDetailsService}) (cannot
58+
* be <code>null</code>)
59+
* @param assertion the assertion returned from the CAS servers. It contains the
60+
* principal and how to obtain a proxy ticket for the user.
61+
*/
62+
@JsonCreator
63+
CasAuthenticationTokenMixin(@JsonProperty("keyHash") Integer keyHash, @JsonProperty("principal") Object principal,
64+
@JsonProperty("credentials") Object credentials,
65+
@JsonProperty("authorities") Collection<? extends GrantedAuthority> authorities,
66+
@JsonProperty("userDetails") UserDetails userDetails, @JsonProperty("assertion") Assertion assertion) {
67+
}
68+
69+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.cas.jackson;
18+
19+
import org.apereo.cas.client.authentication.AttributePrincipalImpl;
20+
import org.apereo.cas.client.validation.AssertionImpl;
21+
import tools.jackson.core.Version;
22+
import tools.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
23+
24+
import org.springframework.security.cas.authentication.CasAuthenticationToken;
25+
import org.springframework.security.jackson.SecurityJacksonModule;
26+
import org.springframework.security.jackson.SecurityJacksonModules;
27+
28+
/**
29+
* Jackson module for spring-security-cas. This module register
30+
* {@link AssertionImplMixin}, {@link AttributePrincipalImplMixin} and
31+
* {@link CasAuthenticationTokenMixin}. If no default typing enabled by default then it'll
32+
* enable it because typing info is needed to properly serialize/deserialize objects. In
33+
* order to use this module just add this module into your JsonMapper configuration.
34+
*
35+
* <p>
36+
* The recommended way to configure it is to use {@link SecurityJacksonModules} in order
37+
* to enable properly automatic inclusion of type information with related validation.
38+
*
39+
* <pre>
40+
* ClassLoader loader = getClass().getClassLoader();
41+
* JsonMapper mapper = JsonMapper.builder()
42+
* .addModules(SecurityJacksonModules.getModules(loader))
43+
* .build();
44+
* </pre>
45+
*
46+
* @author Sebastien Deleuze
47+
* @author Jitendra Singh
48+
* @since 7.0
49+
* @see SecurityJacksonModules
50+
*/
51+
public class CasJacksonModule extends SecurityJacksonModule {
52+
53+
public CasJacksonModule() {
54+
super(CasJacksonModule.class.getName(), new Version(1, 0, 0, null, null, null));
55+
}
56+
57+
@Override
58+
public void configurePolymorphicTypeValidator(BasicPolymorphicTypeValidator.Builder builder) {
59+
builder.allowIfSubType(AssertionImpl.class)
60+
.allowIfSubType(AttributePrincipalImpl.class)
61+
.allowIfSubType(CasAuthenticationToken.class);
62+
}
63+
64+
@Override
65+
public void setupModule(SetupContext context) {
66+
context.setMixIn(AssertionImpl.class, AssertionImplMixin.class);
67+
context.setMixIn(AttributePrincipalImpl.class, AttributePrincipalImplMixin.class);
68+
context.setMixIn(CasAuthenticationToken.class, CasAuthenticationTokenMixin.class);
69+
}
70+
71+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
/**
18+
* Jackson 3+ serialization support for CAS.
19+
*/
20+
package org.springframework.security.cas.jackson;

cas/src/main/java/org/springframework/security/cas/jackson2/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
/**
18-
* Jackson support for CAS.
18+
* Jackson 2 support for CAS.
1919
*/
2020
@NullMarked
2121
package org.springframework.security.cas.jackson2;

0 commit comments

Comments
 (0)