ERR unknown command 'CONFIG' when using Secured Redis

当springboot项目中用AWS的ElasticCache进行session共享的时候,用到@EnableRedisHttpSession这一注解时,重启服务的时候会出现“ERR unknown command 'CONFIG' when using Secured Redis”的错误,查看注解可以看到@Import(RedisHttpSessionConfiguration.class)这个实现类,可以看到
static class EnableRedisKeyspaceNotificationsInitializer implements InitializingBean {
private final RedisConnectionFactory connectionFactory;

    private ConfigureRedisAction configure;

    EnableRedisKeyspaceNotificationsInitializer(
            RedisConnectionFactory connectionFactory,
            ConfigureRedisAction configure) {
        this.connectionFactory = connectionFactory;
        this.configure = configure;
    }

    public void afterPropertiesSet() throws Exception {
        if (this.configure == ConfigureRedisAction.NO_OP) {
            return;
        }
        RedisConnection connection = this.connectionFactory.getConnection();
        try {
            this.configure.configure(connection);
        }
        finally {
            try {
                connection.close();
            }
            catch (Exception e) {
                LogFactory.getLog(getClass()).error("Error closing RedisConnection", e);
            }
        }
    }
}

这样一段代码,其中this.configure.configure(connection);这一配置实现类ConfigureNotifyKeyspaceEventsAction中有如下操作;

public void configure(RedisConnection connection) {
    String notifyOptions = getNotifyOptions(connection);
    String customizedNotifyOptions = notifyOptions;
    if (!customizedNotifyOptions.contains("E")) {
        customizedNotifyOptions += "E";
    }
    boolean A = customizedNotifyOptions.contains("A");
    if (!(A || customizedNotifyOptions.contains("g"))) {
        customizedNotifyOptions += "g";
    }
    if (!(A || customizedNotifyOptions.contains("x"))) {
        customizedNotifyOptions += "x";
    }
    if (!notifyOptions.equals(customizedNotifyOptions)) {
        connection.setConfig(CONFIG_NOTIFY_KEYSPACE_EVENTS, customizedNotifyOptions);
    }
}

其中这一句connection.setConfig(CONFIG_NOTIFY_KEYSPACE_EVENTS, customizedNotifyOptions);进行了config的配置,在ElasticCache中是禁用了config命令的,因此汇报上述错误,如果想代码中禁止这一配置,
可以添加下面这段配置代码:

@Configuration  
@EnableRedisHttpSession
public class RedisConfig{
    @Bean
    public ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }
}

通过注入ConfigureRedisAction.NO_OP可以使EnableRedisKeyspaceNotificationsInitializer类中的

    public void afterPropertiesSet() throws Exception {
        if (this.configure == ConfigureRedisAction.NO_OP) {
            return;
        }
        RedisConnection connection = this.connectionFactory.getConnection();
        try {
            this.configure.configure(connection);
        }
        finally {
            try {
                connection.close();
            }
            catch (Exception e) {
                LogFactory.getLog(getClass()).error("Error closing RedisConnection", e);
            }
        }
    }

这段代码在执行到第一个判断就跳出,不在进行连接配置。解决此问题。
pom中添加依赖:

    
        org.springframework.boot
        spring-boot-starter-data-redis
    
    
        org.springframework.session  
        spring-session-data-redis  
    

配置文件中添加
spring.session.store-type=Redis #使用redis进行session存储
spring.session.redis.flushMode=IMMEDIATE #立即刷新redis缓存

同时在注解中加入下面参数:
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 60)
可以设置session过期时间。

两项配置即可。
具体细节可以参考:
https://github.com/spring-projects/spring-session/issues/124#issuecomment-71490616

你可能感兴趣的