Skip to content

Commit

Permalink
Toggle serialization mode to json for compatibility with spring-secur…
Browse files Browse the repository at this point in the history
…ity 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 <nobodyiam@gmail.com>
  • Loading branch information
klboke and nobodyiam authored Aug 2, 2022
1 parent 1458a93 commit 17f5961
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion apollo-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> 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;
}
}

0 comments on commit 17f5961

Please sign in to comment.