以用户登录为例:
application-resources.yml:
#用户session在redis中保存的key
REDIS_STU_SESSION_KEY: REDIS_USER_SESSION
#session过期时间
STU_SESSION_EXPIRE: 7200
StuService.java:
//验证用户账号密码 CustomResult checkLogin(User user, HttpSession session); //获取token CustomResult getStuByToken(String token);
StuServiceImpl.java:
@Autowired private RedisClient redisClient; @Autowired private UserMapper userMapper; @Autowired private TaskMapper taskMapper; @Value("${REDIS_STU_SESSION_KEY}") private String REDIS_STU_SESSION_KEY; @Value("${STU_SESSION_EXPIRE}") private Integer STU_SESSION_EXPIRE; /** * @return boolean * @Description //验证用户是否存在 * @Date 2019-03-31 21:59 * @Param [user] **/ @Override public CustomResult checkLogin(User user,HttpSession session) { CustomResult result = new CustomResult(); UserExample userExample = new UserExample(); UserExample.Criteria criteria = userExample.createCriteria(); criteria.andUsernameEqualTo(user.getUsername()); criteria.andPasswordEqualTo(user.getPassword()); List<User> userList1 = userMapper.selectByExample(userExample); //HttpServletRequest request = null; //HttpSession session = request.getSession(); if (userList1.isEmpty()) { result.setMsg("账号或密码错误,请重试!"); //return 0;//用户不存在 return result; } else { criteria.andRoleEqualTo(2); List<User> userList2 = userMapper.selectByExample(userExample); if (userList2.isEmpty()){ result.setMsg("您已成功注册,请等待管理员审核!"); return result; //return 1;//等待分配权限,管理员审核 }else{ System.out.println("===================="+userList2.get(0)); //登陆成功,设置session session.setAttribute("stuInfo",userList2.get(0)); User userInfo = userList2.get(0); //生成token String token = UUID.randomUUID().toString(); userInfo.setPassword(null); //用户信息写入redis(:使redis目录分层,更为简洁) redisClient.set(REDIS_STU_SESSION_KEY + ":" + token, JsonUtil.objectToJson(userInfo)); //session过期时间 redisClient.expire(REDIS_STU_SESSION_KEY + ":" + token,STU_SESSION_EXPIRE); //Object stu = redisClient.get("stu"); //System.out.println("========123"+stu); //return 2;//用户存在,通过管理员审核 return CustomResult.ok(token); } } } /** * @Description //获取token * @Date 2019-04-10 16:06 * @Param [token] * @return com.sun123.springboot.CustomResult **/ @Override public CustomResult getStuByToken(String token) { String json = (String)redisClient.get(REDIS_STU_SESSION_KEY + ":" + token); //判断是否为空 if(StringUtils.isBlank(json)){ return CustomResult.build(400,"此session已经过期,请重新登录"); } //若不为空,更新过期时间 redisClient.expire(REDIS_STU_SESSION_KEY + ":" + token,STU_SESSION_EXPIRE); //返回用户信息 return CustomResult.ok(JsonUtil.jsonToPojo(json,User.class)); }
LoginController.java:
import com.sun123.springboot.CustomResult; import com.sun123.springboot.entity.User; import com.sun123.springboot.service.StuService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; /** * @ClassName LoginController * @Description TODO * @Date 2019/3/31 20:27 * @Version 1.0 */ @Controller public class LoginController { @Autowired private StuService stuService; @GetMapping("/login") public String toLogin(){ return "students/login/stu-login"; } @PostMapping("/checkLogin") @ResponseBody public CustomResult checkLogin(User user, HttpSession session){ //int flag = stuService.checkLogin(user,session); CustomResult result = stuService.checkLogin(user, session); return result; } @GetMapping("/token/{token}") @ResponseBody public CustomResult getUserByToken(@PathVariable String token){ CustomResult stuByToken = stuService.getStuByToken(token); return stuByToken; } }
代码测试:
查看响应结果:
浏览器访问:http://localhost:8002/token/d6b4f83c-76df-4f99-b6f9-56e6ff9d6acb