From 17f59619c026ba9525fccda341ab8605c291deaf Mon Sep 17 00:00:00 2001 From: kl <632104866@QQ.com> Date: Tue, 2 Aug 2022 14:38:02 +0800 Subject: [PATCH] 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 --- CHANGES.md | 2 + apollo-portal/pom.xml | 1 - .../component/config/SpringSessionConfig.java | 95 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/SpringSessionConfig.java diff --git a/CHANGES.md b/CHANGES.md index b093b0a6f71..37769ee8deb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,8 @@ Apollo 2.1.0 * [Optimize performance of '/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces' interface queries](https://github.com/apolloconfig/apollo/pull/4473) * [Add a new API to load items with pagination](https://github.com/apolloconfig/apollo/pull/4468) * [fix(#4474):'openjdk:8-jre-alpine' potentially causing wrong number of cpu cores](https://github.com/apolloconfig/apollo/pull/4475) +* [Switching spring-session serialization mode to json for compatibility with spring-security version updates]() * [fix(#4483):Fixed overwrite JSON type configuration being empty](https://github.com/apolloconfig/apollo/pull/4486) + ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1) diff --git a/apollo-portal/pom.xml b/apollo-portal/pom.xml index 3b14538e804..69363e01057 100644 --- a/apollo-portal/pom.xml +++ b/apollo-portal/pom.xml @@ -58,7 +58,6 @@ org.springframework.session spring-session-data-redis - runtime org.springframework.session diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/SpringSessionConfig.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/SpringSessionConfig.java new file mode 100644 index 00000000000..f591600e249 --- /dev/null +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/SpringSessionConfig.java @@ -0,0 +1,95 @@ +/* + * Copyright 2022 Apollo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.ctrip.framework.apollo.portal.component.config; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.support.GenericConversionService; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.security.jackson2.SecurityJackson2Modules; + +/** + * Spring-session JSON serialization mode configuration + * + * @author kl (http://kailing.pub) + * @since 2022/7/26 + */ +@Configuration +public class SpringSessionConfig implements BeanClassLoaderAware { + + private ClassLoader loader; + + @Bean("springSessionConversionService") + @ConditionalOnProperty(prefix = "spring.session", name = "store-type", havingValue = "jdbc") + public ConversionService springSessionConversionService() { + GenericConversionService conversionService = new GenericConversionService(); + ObjectMapper objectMapper = this.objectMapper(); + conversionService.addConverter(Object.class, byte[].class, source -> { + try { + return objectMapper.writeValueAsBytes(source); + } catch (IOException e) { + throw new RuntimeException( + "Spring-session JSON serializing error, This is usually caused by the system upgrade, please clear the browser cookies and try again.", + e); + } + }); + + conversionService.addConverter(byte[].class, Object.class, source -> { + try { + return objectMapper.readValue(source, Object.class); + } catch (IOException e) { + throw new RuntimeException( + "Spring-session JSON deserializing error, This is usually caused by the system upgrade, please clear the browser cookies and try again.", + e); + } + }); + return conversionService; + } + + @Bean("springSessionDefaultRedisSerializer") + @ConditionalOnProperty(prefix = "spring.session", name = "store-type", havingValue = "redis") + public RedisSerializer springSessionDefaultRedisSerializer() { + return new GenericJackson2JsonRedisSerializer(objectMapper()); + } + + /** + * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default constructors + * + * @return the {@link ObjectMapper} to use + */ + private ObjectMapper objectMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModules(SecurityJackson2Modules.getModules(this.loader)); + return mapper; + } + + /* + * @see + * org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang + * .ClassLoader) + */ + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.loader = classLoader; + } +}