SpringBoot2.1.4快速整合session-redis实现分布式session共享,极少配置

集成redis

1.引入jar包

   <!-- redis -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
	</dependency>

2.配置属性
在application.properties增加Redis服务的相关信息

#################redis单服务基础配置#################
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.password=
spring.redis.port=6379

3. RedisTemplate配置

   /**
	 * RedisTemplate配置
     */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
		// 设置序列化
		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
				Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		// 配置redisTemplate
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(lettuceConnectionFactory);
		RedisSerializer<?> stringSerializer = new StringRedisSerializer();
		redisTemplate.setKeySerializer(stringSerializer);// key序列化
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
		redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
		redisTemplate.setHashValueSerializer(jackson2
@RequestMapping("/testjson") @ResponseBody public Map<String,Object> testjson(String data) throws JSONException { Map<String,Object> map = new HashMap<>(); // String objectStr="{\\\\"username\":\"老李\",\"nickname\":\"李刚\",\"remark\":\"肚痛\"}"; // String basestr = Base64.getBase64(objectStr); String javabean = Base64.getFromBase64(data);//解密 // System.out.println(javabean+"解密后"); JSONObject JSON = JSONObject.parseObject(javabean);//转json String j = JSON.get("data").toString();//根据key获取value // System.out.println(j+"解密后"); JSONObject JSONs = JSONObject.parseObject(j);//value转json SysUser sysUser = JSONs.toJavaObject(SysUser.class);//json转实体类 System.out.println(sysUser.getUsername()+"名字"); return map; } 测试类 package com.example.hello; import com.alibaba.fastjson.JSONObject; import com.example.hello.util.Base64; import com.example.hello.util.HttpClientUtil; import org.junit.Test; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class HelloApplicationTests { public static final String baseUrl = "http://localhost:8080/"; @Test public void contextLoads() throws UnsupportedEncodingException { String requestUrl = baseUrl + "testjson"; Map<String, Object> argsMap = new HashMap<String, Object>(); argsMap.put("data", "{'username':'老李','nickname':'李刚','remark':'肚痛'}"); String argsStr = JSONObject.toJSONString(argsMap); System.out.println("加密前"+argsStr); String base = Base64.getBase64(argsStr); argsStr = URLEncoder.encode(base,"utf8"); HttpClientUtil.httpRequest(requestUrl + "?data=" + argsStr, ""); System.out.println("10000000000"); } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值