Skip to content

Commit 17f5961

Browse files
klbokenobodyiam
andauthored
Toggle serialization mode to json for compatibility with spring-security version updates (#4484)
* add tech-support-qq-4.png * Update README.md * Enhance the user experience in the scenario of submitting duplicate keys * Modify the key-value conflict exception prompt, adjust the code style * refactor(apollo-portal/Spring-session): Toggle serialization mode to json for compatibility with spring-security version updates * doc(CHANGES.md): update CHANGES.md Co-authored-by: Jason Song <[email protected]>
1 parent 1458a93 commit 17f5961

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Apollo 2.1.0
2121
* [Optimize performance of '/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces' interface queries](https://github.com/apolloconfig/apollo/pull/4473)
2222
* [Add a new API to load items with pagination](https://github.com/apolloconfig/apollo/pull/4468)
2323
* [fix(#4474):'openjdk:8-jre-alpine' potentially causing wrong number of cpu cores](https://github.com/apolloconfig/apollo/pull/4475)
24+
* [Switching spring-session serialization mode to json for compatibility with spring-security version updates]()
2425
* [fix(#4483):Fixed overwrite JSON type configuration being empty](https://github.com/apolloconfig/apollo/pull/4486)
26+
2527
------------------
2628
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)

apollo-portal/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
<dependency>
5959
<groupId>org.springframework.session</groupId>
6060
<artifactId>spring-session-data-redis</artifactId>
61-
<scope>runtime</scope>
6261
</dependency>
6362
<dependency>
6463
<groupId>org.springframework.session</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2022 Apollo 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+
* http://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 com.ctrip.framework.apollo.portal.component.config;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import java.io.IOException;
21+
import org.springframework.beans.factory.BeanClassLoaderAware;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.core.convert.ConversionService;
26+
import org.springframework.core.convert.support.GenericConversionService;
27+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
28+
import org.springframework.data.redis.serializer.RedisSerializer;
29+
import org.springframework.security.jackson2.SecurityJackson2Modules;
30+
31+
/**
32+
* Spring-session JSON serialization mode configuration
33+
*
34+
* @author kl (http://kailing.pub)
35+
* @since 2022/7/26
36+
*/
37+
@Configuration
38+
public class SpringSessionConfig implements BeanClassLoaderAware {
39+
40+
private ClassLoader loader;
41+
42+
@Bean("springSessionConversionService")
43+
@ConditionalOnProperty(prefix = "spring.session", name = "store-type", havingValue = "jdbc")
44+
public ConversionService springSessionConversionService() {
45+
GenericConversionService conversionService = new GenericConversionService();
46+
ObjectMapper objectMapper = this.objectMapper();
47+
conversionService.addConverter(Object.class, byte[].class, source -> {
48+
try {
49+
return objectMapper.writeValueAsBytes(source);
50+
} catch (IOException e) {
51+
throw new RuntimeException(
52+
"Spring-session JSON serializing error, This is usually caused by the system upgrade, please clear the browser cookies and try again.",
53+
e);
54+
}
55+
});
56+
57+
conversionService.addConverter(byte[].class, Object.class, source -> {
58+
try {
59+
return objectMapper.readValue(source, Object.class);
60+
} catch (IOException e) {
61+
throw new RuntimeException(
62+
"Spring-session JSON deserializing error, This is usually caused by the system upgrade, please clear the browser cookies and try again.",
63+
e);
64+
}
65+
});
66+
return conversionService;
67+
}
68+
69+
@Bean("springSessionDefaultRedisSerializer")
70+
@ConditionalOnProperty(prefix = "spring.session", name = "store-type", havingValue = "redis")
71+
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
72+
return new GenericJackson2JsonRedisSerializer(objectMapper());
73+
}
74+
75+
/**
76+
* Customized {@link ObjectMapper} to add mix-in for class that doesn't have default constructors
77+
*
78+
* @return the {@link ObjectMapper} to use
79+
*/
80+
private ObjectMapper objectMapper() {
81+
ObjectMapper mapper = new ObjectMapper();
82+
mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
83+
return mapper;
84+
}
85+
86+
/*
87+
* @see
88+
* org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang
89+
* .ClassLoader)
90+
*/
91+
@Override
92+
public void setBeanClassLoader(ClassLoader classLoader) {
93+
this.loader = classLoader;
94+
}
95+
}

0 commit comments

Comments
 (0)