Skip to content

Commit 743817f

Browse files
committed
Add AuthorizationProxyMixin
This commit adds Jackson configuration specific to authorization proxies created by Spring Security Closes gh-18077
1 parent fb701e4 commit 743817f

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.jackson;
18+
19+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20+
21+
import org.springframework.security.authorization.method.AuthorizationProxy;
22+
23+
/**
24+
* Jackson configurations for objects that extend {@link AuthorizationProxy}
25+
*
26+
* @author Josh Cummings
27+
* @since 7.0
28+
* @see org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory
29+
*/
30+
@JsonIgnoreProperties("callbacks")
31+
class AuthorizationProxyMixin {
32+
33+
}

core/src/main/java/org/springframework/security/jackson/CoreJacksonModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.security.authentication.RememberMeAuthenticationToken;
3030
import org.springframework.security.authentication.TestingAuthenticationToken;
3131
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
32+
import org.springframework.security.authorization.method.AuthorizationProxy;
3233
import org.springframework.security.core.authority.FactorGrantedAuthority;
3334
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3435
import org.springframework.security.core.context.SecurityContextImpl;
@@ -108,6 +109,7 @@ public void setupModule(SetupContext context) {
108109
context.setMixIn(UsernamePasswordAuthenticationToken.class, UsernamePasswordAuthenticationTokenMixin.class);
109110
context.setMixIn(TestingAuthenticationToken.class, TestingAuthenticationTokenMixin.class);
110111
context.setMixIn(BadCredentialsException.class, BadCredentialsExceptionMixin.class);
112+
context.setMixIn(AuthorizationProxy.class, AuthorizationProxyMixin.class);
111113
}
112114

113115
}

core/src/test/java/org/springframework/security/authorization/AuthorizationAdvisorProxyFactoryTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.function.Supplier;
3535
import java.util.stream.Stream;
3636

37-
import org.junit.jupiter.api.Disabled;
3837
import org.junit.jupiter.api.Test;
3938
import tools.jackson.databind.json.JsonMapper;
4039

@@ -50,6 +49,7 @@
5049
import org.springframework.security.authorization.method.AuthorizationProxy;
5150
import org.springframework.security.core.Authentication;
5251
import org.springframework.security.core.context.SecurityContextHolder;
52+
import org.springframework.security.jackson.CoreJacksonModule;
5353

5454
import static org.assertj.core.api.Assertions.assertThat;
5555
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -340,15 +340,13 @@ public void setTargetVisitorIgnoreValueTypesThenIgnores() {
340340
assertThat(factory.proxy(35)).isEqualTo(35);
341341
}
342342

343-
// TODO Find why callbacks property is serialized with Jackson 3, not with Jackson 2
344-
// FIXME: https://github.com/spring-projects/spring-security/issues/18077
345-
@Disabled("callbacks property is serialized with Jackson 3, not with Jackson 2")
346343
@Test
347344
public void serializeWhenAuthorizationProxyObjectThenOnlyIncludesProxiedProperties() {
348345
SecurityContextHolder.getContext().setAuthentication(this.admin);
349346
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
350347
User user = proxy(factory, this.alan);
351-
JsonMapper mapper = new JsonMapper();
348+
// gh-18077
349+
JsonMapper mapper = JsonMapper.builder().addModule(new CoreJacksonModule()).build();
352350
String serialized = mapper.writeValueAsString(user);
353351
Map<String, Object> properties = mapper.readValue(serialized, Map.class);
354352
assertThat(properties).hasSize(3).containsKeys("id", "firstName", "lastName");

0 commit comments

Comments
 (0)