-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Toggle serialization mode to json for compatibility with spring-secur…
…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
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...src/main/java/com/ctrip/framework/apollo/portal/component/config/SpringSessionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |