|
| 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