添加依赖
<!--Spring data redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--commons-pool2依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
配置redis信息
spring:
# redis配置
redis:
host: 116.62.180.44
port: 6379
#0库
database: 0
timeout: 10000ms
lettuce:
pool:
#最大连接数,默认8
max-active: 8
# 最大连接阻塞等待时间,默认-1
max-wait: 10000ms
# 最大空闲连接 默认8
max-idle: 200
# 最小空闲连接,默认0
min-idle: 5
当没有任何配置的时候存入redis的数据是二进制的
先对对象进行序列化
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//将信息序列化,在redis中看起来好看
@Configuration
public class RedisConfig {
//直接让SpringBoot管理
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//key的序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//value的序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//hash类型key的序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//hash类型value的序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//注入连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
将得到的用户信息存入到redis中
首先需要注入RedisTemplate
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.exception.GlobalException;
import com.xxxx.seckill.mapper.UserMapper;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IUserService;
import com.xxxx.seckill.utils.CookieUtil;
import com.xxxx.seckill.utils.MD5Util;
import com.xxxx.seckill.utils.UUIDUtil;
import com.xxxx.seckill.vo.LoginVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* <p>
* 服务实现类
* </p>
*
* @author van
* @since 2022-01-05
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
//注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate;
@Override
public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
// login那里已经对电话号码进行了判断,所以这里就不用管了
String mobile=loginVo.getMobile();
String password=loginVo.getPassword();
// 使用MybatisPlus,其默认提供了一系列的增删改查的基础方法
User user=userMapper.selectById(mobile);
//如果用户为空
if(null==user) {
// 弄这个是为了将报错信息放在前端
throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
}
//如果密码不正确
if(!MD5Util.formPassToDBPass(password,user.getSalt()).equals(user.getPassword())) {
throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
}
//uuid是统一的识别码,目的是让分布式系统中的所有元素都能有唯一的识别信息,这块是为了将uuid与cookie绑定起来
String ticket= UUIDUtil.uuid();
// 将用户信息存入到redis中
redisTemplate.opsForValue().set("user:"+ticket,user);
//将cookie的值存入到session中,这就需要request和response
// request.getSession().setAttribute(ticket,user);
//设置cookie的值
CookieUtil.setCookie(request,response,"userTicket",ticket);
return RespBean.success();
}
}
通过redis获取用户信息
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.exception.GlobalException;
import com.xxxx.seckill.mapper.UserMapper;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IUserService;
import com.xxxx.seckill.utils.CookieUtil;
import com.xxxx.seckill.utils.MD5Util;
import com.xxxx.seckill.utils.UUIDUtil;
import com.xxxx.seckill.vo.LoginVo;
import com.xxxx.seckill.vo.RespBean;
import com.xxxx.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* <p>
* 服务实现类
* </p>
*
* @author van
* @since 2022-01-05
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public User getUserByCookie(String userTicket,HttpServletRequest request,HttpServletResponse response) {
if(StringUtils.isEmpty(userTicket))
return null;
// 从redis中获取到user的值
User user = (User)redisTemplate.opsForValue().get("user:" + userTicket);
return user;
}
}
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private IUserService iUserService;
@RequestMapping("/toList")
public String toList(HttpServletRequest request,HttpServletResponse response,Model model,@CookieValue("userTicket") String ticket) {
if(StringUtils.isEmpty(ticket)) {
return "login";
}
User user=iUserService.getUserByCookie(ticket,request,response);
if(null==user) {
return "login";
}
model.addAttribute("user",user);
return "goodsList";
}
}