springboot集成redis 并且配置文件加密访问

加入maven依赖

  <!-- 缓存依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <scope>compile</scope>
        </dependency>

配置文件配置

# redis数据源
spring:
  redis:
    host: test01
    database: 1
    password: eGlud2VpMjAyMA==
    servlet:
      multipart:
        max-file-size: 100MB
        max-request-size: 100MB
    port: 6380     

代码实现(利用spring面向切面实现加密密码连接)

step1: 创建一个切面获取redis 的登录密码

public class CommonPointCut {

    @Pointcut("execution(* org.springframework.boot.autoconfigure.data.redis.RedisProperties.getPassword())")
    public void getRedisConnectionPwd(){}
}    

step2:加入config配置文件

@Data
@Aspect
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class RedisConfig {

    private static final Logger log = LoggerFactory.getLogger(RedisConfig.class);

    @Autowired
    private RedisProperties redisProperties;

    private String password;

    @Before("com.xinwei.shop.pointcut.CommonPointCut.getRedisConnectionPwd()")
    public void setRedisProperties(){
        log.info("login redis to decode password of project");
        String pwdStr =  Base64.decodeStr(password);
        log.info("redis login password = {}", pwdStr);
        redisProperties.setPassword(pwdStr);
    }

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        //设置自定义的序列化方式(fastjson) ,不然key会出现 \xac\xed\x00\x05t\x00\tb
        template.setDefaultSerializer(new FastJsonRedisSerializer(Object.class));
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }

}

完成

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值