Skip to content

Commit 828d7ed

Browse files
committed
Improve cglib object serialize
Issue gh-15395
1 parent 561c786 commit 828d7ed

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2024 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.jackson2;
18+
19+
import com.fasterxml.jackson.databind.module.SimpleModule;
20+
21+
/**
22+
* Modify serializer so that to serialize Cglib object
23+
*
24+
* @author DingHao
25+
* @since 6.4
26+
* @see org.springframework.security.authorization.method.AuthorizeReturnObject
27+
*/
28+
public final class AuthorizeReturnObjectJackson2Module extends SimpleModule {
29+
30+
@Override
31+
public void setupModule(SetupContext context) {
32+
context.addBeanSerializerModifier(new CglibBeanSerializerModifier());
33+
}
34+
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024 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.jackson2;
18+
19+
import java.io.IOException;
20+
21+
import com.fasterxml.jackson.core.JsonGenerator;
22+
import com.fasterxml.jackson.databind.BeanDescription;
23+
import com.fasterxml.jackson.databind.JsonSerializer;
24+
import com.fasterxml.jackson.databind.SerializationConfig;
25+
import com.fasterxml.jackson.databind.SerializerProvider;
26+
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
27+
28+
import org.springframework.aop.support.AopUtils;
29+
import org.springframework.util.ClassUtils;
30+
31+
/**
32+
* Serialize cglib generated objects
33+
*
34+
* @author DingHao
35+
* @since 6.4
36+
*/
37+
class CglibBeanSerializerModifier extends BeanSerializerModifier {
38+
39+
@Override
40+
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc,
41+
JsonSerializer<?> serializer) {
42+
if (beanDesc.getBeanClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR)) {
43+
return new CglibObjectSerializer();
44+
}
45+
return serializer;
46+
}
47+
48+
static class CglibObjectSerializer extends JsonSerializer<Object> {
49+
50+
@Override
51+
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
52+
Class<?> targetClass = AopUtils.getTargetClass(value);
53+
JsonSerializer<Object> serializer = provider.findValueSerializer(targetClass);
54+
serializer.serialize(value, gen, provider);
55+
}
56+
57+
}
58+
59+
}

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

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

37+
import com.fasterxml.jackson.core.JsonProcessingException;
38+
import com.fasterxml.jackson.databind.ObjectMapper;
39+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
3740
import org.jetbrains.annotations.NotNull;
3841
import org.junit.jupiter.api.Test;
3942

@@ -46,6 +49,7 @@
4649
import org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory.TargetVisitor;
4750
import org.springframework.security.core.Authentication;
4851
import org.springframework.security.core.context.SecurityContextHolder;
52+
import org.springframework.security.jackson2.AuthorizeReturnObjectJackson2Module;
4953

5054
import static org.assertj.core.api.Assertions.assertThat;
5155
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -336,6 +340,25 @@ public void setTargetVisitorIgnoreValueTypesThenIgnores() {
336340
assertThat(factory.proxy(35)).isEqualTo(35);
337341
}
338342

343+
@Test
344+
public void serializeCglibObjectThrowException() {
345+
SecurityContextHolder.getContext().setAuthentication(this.admin);
346+
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
347+
User user = proxy(factory, this.alan);
348+
ObjectMapper mapper = new ObjectMapper();
349+
assertThatExceptionOfType(InvalidDefinitionException.class).isThrownBy(() -> mapper.writeValueAsString(user));
350+
}
351+
352+
@Test
353+
public void serializeCglibObjectSuccess() throws JsonProcessingException {
354+
SecurityContextHolder.getContext().setAuthentication(this.admin);
355+
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
356+
User user = proxy(factory, this.alan);
357+
ObjectMapper mapper = new ObjectMapper();
358+
mapper.registerModule(new AuthorizeReturnObjectJackson2Module());
359+
assertThat(mapper.writeValueAsString(user)).isInstanceOf(String.class);
360+
}
361+
339362
private Authentication authenticated(String user, String... authorities) {
340363
return TestAuthentication.authenticated(TestAuthentication.withUsername(user).authorities(authorities).build());
341364
}

docs/modules/ROOT/pages/servlet/authorization/method-security.adoc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,39 @@ class User
22582258
----
22592259
======
22602260

2261+
Or register `AuthorizeReturnObjectJackson2Module` to `ObjectMapper`:
2262+
2263+
[source,java]
2264+
----
2265+
ObjectMapper mapper = new ObjectMapper();
2266+
mapper.registerModule(new AuthorizeReturnObjectJackson2Module());
2267+
----
2268+
2269+
If you are using Spring Boot, you can also publish `AuthorizeReturnObjectJackson2Module` as a bean:
2270+
2271+
[tabs]
2272+
======
2273+
Java::
2274+
+
2275+
[source,java,role="primary"]
2276+
----
2277+
@Bean
2278+
public AuthorizeReturnObjectJackson2Module authorizeReturnObjectJackson2Module() {
2279+
return new AuthorizeReturnObjectJackson2Module();
2280+
}
2281+
----
2282+
2283+
Kotlin::
2284+
+
2285+
[source,kotlin,role="secondary"]
2286+
----
2287+
@Bean
2288+
fun authorizeReturnObjectJackson2Module(): AuthorizeReturnObjectJackson2Module {
2289+
return AuthorizeReturnObjectJackson2Module()
2290+
}
2291+
----
2292+
======
2293+
22612294
Finally, you will need to publish a <<custom_advice, custom interceptor>> to catch the `AccessDeniedException` thrown for each field, which you can do like so:
22622295

22632296
[tabs]

0 commit comments

Comments
 (0)