springboot redis 密码_redis密码登录

(2) 2024-07-21 11:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
springboot redis 密码_redis密码登录,希望能够帮助你!!!。

创作背景

springboot2 集成redis集群网上的例子已经很多了,但涉及到密码几乎都是明文,这在实际生产环境中,是不允许的,特写此文章。

源码片段

第一步:pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> </parent> <groupId>com.wu</groupId> <artifactId>springbootKafka</artifactId> <version>0.0.1</version> <name>springbootKafka</name> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.6.7</version> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.3.RELEASE</version> </plugin> </plugins> </build> </project>

第二步:配置 application.yml

spring: main: web-application-type: none #去除web,以纯java的模式启动springboot redis: cluster: nodes: 192.168.1.101:8001,192.168.1.101:8002,192.168.1.102:8001,192.168.1.102:8002,192.168.1.103:8001,192.168.1.103:8002 max-redirects: 3 # 获取失败 最大重定向次数 pool: max-active: 1000 # 连接池最大连接数(使用负值表示没有限制) max-idle: 10 # 连接池中的最大空闲连接 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) min-idle: 5 # 连接池中的最小空闲连接 timeout: 6000 # 连接超时时长(毫秒) password: FC7EF9622A3D2B62 #redis加密密码 logging: level: root: info

以上重点是这一句:

password: FC7EF9622A3D2B62

redis密码不是明文的,而是通过des加密过的。

DesUtils.java
package com.wu.kafka.util; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.security.SecureRandom; public final class DesUtils { private static final String DES = "DES"; private static final String KEY = "comwukafka"; //默认密钥 private DesUtils() {} private static byte[] encrypt(byte[] src, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(DES); cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); return cipher.doFinal(src); } private static byte[] decrypt(byte[] src, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(DES); cipher.init(Cipher.DECRYPT_MODE, secretKey, sr); return cipher.doFinal(src); } private static String byte2hex(byte[] b) { String hs = ""; String temp = ""; for (int n = 0; n < b.length; n++) { temp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (temp.length() == 1) hs = hs + "0" + temp; else hs = hs + temp; } return hs.toUpperCase(); } private static byte[] hex2byte(byte[] b) { if ((b.length % 2) != 0) throw new IllegalArgumentException("length not even"); byte[] b2 = new byte[b.length / 2]; for (int n = 0; n < b.length; n += 2) { String item = new String(b, n, 2); b2[n / 2] = (byte) Integer.parseInt(item, 16); } return b2; } public static String decode(String src, String key) { String decryptStr = ""; try { byte[] decrypt = decrypt(hex2byte(src.getBytes()), key.getBytes()); decryptStr = new String(decrypt); } catch (Exception e) { e.printStackTrace(); } return decryptStr; } public static String encode(String src, String key){ byte[] bytes = null; String encryptStr = ""; try { bytes = encrypt(src.getBytes(), key.getBytes()); } catch (Exception ex) { ex.printStackTrace(); } if (bytes != null) encryptStr = byte2hex(bytes); return encryptStr; } /** * 解密 */ public static String decode(String src) { return decode(src, KEY); } /** * 加密 */ public static String encode(String src) { return encode(src, KEY); } }

第三步:自定义 RedisConnectionFactory,不采用默认逻辑

package com.wu.kafka.config; import com.wu.kafka.util.DesUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import javax.annotation.Resource; import java.io.Serializable; import java.util.HashMap; import java.util.Map; @Configuration public class RedisConfig { private final Environment environment; public RedisConfig(Environment environment) { this.environment = environment; } @Bean(name = "myredisConnectionFactory") public RedisConnectionFactory myLettuceConnectionFactory() { Map<String, Object> source = new HashMap<String, Object>(); source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes")); source.put("spring.redis.cluster.timeout", environment.getProperty("spring.redis.cluster.timeout")); source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects")); MapPropertySource mapPropertySource = new MapPropertySource("RedisClusterConfiguration", source); RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(mapPropertySource); //获取application.yml 中的密码(密文) String password = environment.getProperty("spring.redis.password"); //解密密码并停驾到配置中 redisClusterConfiguration.setPassword(DesUtils.decode(password)); return new LettuceConnectionFactory(redisClusterConfiguration); } @Bean public RedisTemplate<String, Serializable> redisTemplate(@Qualifier("myredisConnectionFactory") RedisConnectionFactory factory) { RedisTemplate<String, Serializable> template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(factory); return template; } @Bean public StringRedisTemplate createStringRedisTemplate(@Qualifier("myredisConnectionFactory") RedisConnectionFactory factory){ StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(factory); stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); stringRedisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return stringRedisTemplate; } }

第四步 使用

 @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate; public void test(){ stringRedisTemplate.boundValueOps("name").set("wuchao", 500, TimeUnit.SECONDS); String name = stringRedisTemplate.boundValueOps("name").get(); System.out.println(name); }

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复