Spring session unable save session to Redis

穿精又带淫゛_ 提交于 2019-12-13 16:04:27

问题


I have an application that is integrated with spring security. I separated this application to two app for this I want to config spring session with Redis for centralization authentication between this two app. I have configured Redis but when I want to log in the below exception is raised:

Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: org.springframework.dao.support.PersistenceExceptionTranslationInterceptor at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:68) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:35) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:90) ~[spring-data-redis-1.7.2.RELEASE.jar:na] ... 35 common frames omitted Caused by: java.io.NotSerializableException: org.springframework.dao.support.PersistenceExceptionTranslationInterceptor at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) ~[na:1.8.0_111] at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) ~[na:1.8.0_111]

The RedisConfig:

@Configuration
@EnableRedisHttpSession
public class RedisConfig implements BeanClassLoaderAware {

    private ClassLoader loader;

    @Bean
    @Primary
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
        redisTemplate.setConnectionFactory(cf);
        redisTemplate.setKeySerializer(redisTemplate.getStringSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
        return redisTemplate;
    }

    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID"); // <1>
        serializer.setCookiePath("/"); // <2>
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$"); // <3>
        return serializer;
    }

    @Bean
    public HttpSessionStrategy httpSessionStrategy() {
        return new HeaderHttpSessionStrategy();
    }

    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }

    ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
        return mapper;
    }

    public void setBeanClassLoader(ClassLoader classLoader) {
        this.loader = classLoader;
    }
}

and this config:

public class SafeDeserializationRepository<S extends ExpiringSession> implements SessionRepository<S> {

    private final SessionRepository<S> delegate;
    private final RedisTemplate<Object, Object> redisTemplate;

    private static final String BOUNDED_HASH_KEY_PREFIX = "spring:session:sessions:";
    // private static final Logger logger =
    // getLogger(SafeDeserializationRepository.class);

    public SafeDeserializationRepository(SessionRepository<S> delegate, RedisTemplate<Object, Object> redisTemplate) {
        this.delegate = delegate;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public S createSession() {
        return delegate.createSession();
    }

    @Override
    public void save(S session) {
        delegate.save(session);
    }

    @Override
    public S getSession(String id) {
        try {
            return delegate.getSession(id);
        } catch (SerializationException e) {
            // logger.info("Deleting non-deserializable session with key {}",
            // id);
            redisTemplate.delete(BOUNDED_HASH_KEY_PREFIX + id);
            return null;
        }
    }

    @Override
    public void delete(String id) {
        delegate.delete(id);
    }
}

and RedisSessionConfig:

@Configuration
public class RedisSessionConfig extends RedisHttpSessionConfiguration {
  @Autowired
  RedisTemplate<Object, Object> redisTemplate;

  @Bean
  @Override
  public <S extends ExpiringSession> SessionRepositoryFilter<? extends ExpiringSession> springSessionRepositoryFilter(SessionRepository<S> sessionRepository) {
    return super.springSessionRepositoryFilter(new SafeDeserializationRepository<>(sessionRepository, redisTemplate));
  }
}

What is wrong?

I am confused

Thanks for your help.


回答1:


I ran in to a similar issue few weeks ago.Please check the classes. You can use the java's implements Serializable for those classes. The issue is encountered when the class is not Serializable. Also look in the Jackson annotations so that you can choose how object will be serialized.



来源:https://stackoverflow.com/questions/44303504/spring-session-unable-save-session-to-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!