redis短信验证功能

This commit is contained in:
fang 2025-03-06 09:44:19 +08:00
parent 6999b9fe4d
commit f6e644dbfe
12 changed files with 171 additions and 18 deletions

View File

@ -14708,3 +14708,38 @@
127.0.0.1 - - [05/Mar/2025:15:43:36 +0800] "GET /api/user/me HTTP/1.1" 200 85 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:15:43:36 +0800] "GET /api/user/info/1005 HTTP/1.1" 200 27 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:15:43:36 +0800] "GET /api/blog/of/me HTTP/1.1" 200 37 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:25 +0800] "GET /info.html HTTP/1.1" 304 0 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:25 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:26 +0800] "GET /api/user/me HTTP/1.1" 401 0 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:26 +0800] "GET /login.html HTTP/1.1" 304 0 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:27 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:14:42 +0800] "POST /api/user/code?phone=13619547889 HTTP/1.1" 499 0 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:19:15:14 +0800] "POST /api/user/login HTTP/1.1" 200 57 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:31 +0800] "GET /login.html HTTP/1.1" 304 0 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:31 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:39 +0800] "POST /api/user/code?phone=15226354789 HTTP/1.1" 499 0 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:51 +0800] "POST /api/user/login HTTP/1.1" 200 69 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:51 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:51 +0800] "GET /api/shop-type/list HTTP/1.1" 200 664 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:19:51 +0800] "GET /api/blog/hot?current=1 HTTP/1.1" 200 6065 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:47 +0800] "GET /index.html HTTP/1.1" 304 0 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:47 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:47 +0800] "GET /api/shop-type/list HTTP/1.1" 200 57 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:47 +0800] "GET /api/blog/hot?current=1 HTTP/1.1" 200 57 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:49 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:49 +0800] "GET /api/user/me HTTP/1.1" 200 57 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:34:49 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:43:01 +0800] "POST /api/user/code?phone=15226354789 HTTP/1.1" 200 57 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:48:06 +0800] "POST /api/user/code?phone=15226354789 HTTP/1.1" 499 0 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:48:20 +0800] "POST /api/user/login HTTP/1.1" 200 69 "http://127.0.0.1:8080/login.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:48:20 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:48:20 +0800] "GET /api/shop-type/list HTTP/1.1" 200 664 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:48:20 +0800] "GET /api/blog/hot?current=1 HTTP/1.1" 200 6065 "http://127.0.0.1:8080/index.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:21 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:21 +0800] "GET /api/user/me HTTP/1.1" 200 85 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:21 +0800] "GET /api/user/info/1012 HTTP/1.1" 200 27 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:21 +0800] "GET /api/blog/of/me HTTP/1.1" 200 37 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:38 +0800] "GET / HTTP/1.1" 200 5238 "http://127.0.0.1:8080/info.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:38 +0800] "GET /api/shop-type/list HTTP/1.1" 200 664 "http://127.0.0.1:8080/" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:38 +0800] "GET /js/axios.min.map HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
127.0.0.1 - - [05/Mar/2025:20:49:38 +0800] "GET /api/blog/hot?current=1 HTTP/1.1" 200 6065 "http://127.0.0.1:8080/" "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/133.0.0.0"

View File

@ -2787,3 +2787,13 @@
2025/03/05 15:43:10 [error] 12716#24776: *95 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 15:43:34 [error] 12716#24776: *101 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 15:43:36 [error] 12716#24776: *95 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 19:14:25 [error] 12716#24776: *106 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 19:14:27 [error] 12716#24776: *105 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:19:31 [error] 12716#24776: *110 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:19:51 [error] 12716#24776: *119 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:34:47 [error] 12716#24776: *120 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:34:49 [error] 12716#24776: *121 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:34:49 [error] 12716#24776: *125 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:48:20 [error] 12716#24776: *135 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:49:21 [error] 12716#24776: *136 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"
2025/03/05 20:49:38 [error] 12716#24776: *136 CreateFile() "D:\Program Files\java_program\hm-dianping\nginx-1.18.0/html/hmdp/js/axios.min.map" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: localhost, request: "GET /js/axios.min.map HTTP/1.1", host: "127.0.0.1:8080"

View File

@ -1 +1 @@
45532
14688

View File

@ -1,12 +1,18 @@
package com.hmdp.config;
import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
@ -18,6 +24,9 @@ public class MvcConfig implements WebMvcConfigurer {
"/blog/hot",
"/user/code",
"/user/login"
);
).order(1);
registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
.addPathPatterns("/**")
.order(0);
}
}

View File

@ -34,6 +34,7 @@ public class ShopController {
@GetMapping("/{id}")
public Result queryShopById(@PathVariable("id") Long id) {
return Result.ok(shopService.getById(id));
// return shopService.queryById(id);
}
/**

View File

@ -1,5 +1,6 @@
package com.hmdp.service;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.baomidou.mybatisplus.extension.service.IService;
@ -13,4 +14,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface IShopService extends IService<Shop> {
Result queryById(Long id);
}

View File

@ -1,11 +1,15 @@
package com.hmdp.service.impl;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.hmdp.mapper.ShopMapper;
import com.hmdp.service.IShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* <p>
* 服务实现类
@ -16,5 +20,13 @@ import org.springframework.stereotype.Service;
*/
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements IShopService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public Result queryById(Long id) {
// 使用redis缓存
// 查询缓存
stringRedisTemplate.opsForValue().get("cache:shop:" + id);
return null;
}
}

View File

@ -1,18 +1,29 @@
package com.hmdp.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import com.hmdp.mapper.UserMapper;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.hmdp.utils.RedisConstants.*;
import static com.hmdp.utils.SystemConstants.USER_NICK_NAME_PREFIX;
/**
@ -26,7 +37,8 @@ import static com.hmdp.utils.SystemConstants.USER_NICK_NAME_PREFIX;
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Override
public Result sendCode(String phone, HttpSession session) {
// 校验手机号是否合格
@ -35,8 +47,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
}
// 合格生成验证码
String code = RandomUtil.randomNumbers(6);
// 保存到session
session.setAttribute("code",code);
// // 保存到session
// session.setAttribute("code",code);
// 保存到redis并且两分钟失效
stringRedisTemplate.opsForValue().set(LOGIN_CODE_KEY+phone,code,LOGIN_CODE_TTL, TimeUnit.MINUTES);
// 发送验证码
log.debug("发送验证码成功,验证码为:{}",code);
return Result.ok();
@ -51,7 +65,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
}
// 获取loginForm中的code对比session中的
String code = loginForm.getCode();
if(code == null || !code.equals(session.getAttribute("code"))){
// if(code == null || !code.equals(session.getAttribute("code"))){
// return Result.fail("验证码错误");
// }
String cacheCode = stringRedisTemplate.opsForValue().get(LOGIN_CODE_KEY+phone);
if(code == null || !code.equals(cacheCode)){
return Result.fail("验证码错误");
}
// 判断用户是否存在存在存到session不存在创建用户
@ -60,8 +78,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
user = saveUserByPhone(phone);
save(user);
}
session.setAttribute("user",user);
return Result.ok();
// session.setAttribute("user",user);
// 保存用户信息到 redis中
// 随机生成token作为登录令牌
String token = UUID.randomUUID().toString(true);
// 将user对象转成hashMap
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
Map<String,Object> userMap = BeanUtil.beanToMap(userDTO, new HashMap<>(),
CopyOptions.create()
.setIgnoreNullValue(true)
.setFieldValueEditor((fieldName, fieldValue) -> fieldValue.toString()));
stringRedisTemplate.opsForHash().putAll(LOGIN_USER_KEY+token,userMap);
stringRedisTemplate.expire(LOGIN_USER_KEY+token,LOGIN_USER_TTL,TimeUnit.MINUTES);
return Result.ok(token);
}
private User saveUserByPhone(String phone) {

View File

@ -1,27 +1,30 @@
package com.hmdp.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.hmdp.utils.RedisConstants.LOGIN_USER_KEY;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
if(UserHolder.getUser() == null){
response.setStatus(401);
return false;
}
// 将user信息保存到线程中
UserDTO userDTO = new UserDTO();
BeanUtil.copyProperties(user, userDTO);
UserHolder.saveUser(userDTO);
return true;
}
}

View File

@ -4,7 +4,7 @@ public class RedisConstants {
public static final String LOGIN_CODE_KEY = "login:code:";
public static final Long LOGIN_CODE_TTL = 2L;
public static final String LOGIN_USER_KEY = "login:token:";
public static final Long LOGIN_USER_TTL = 36000L;
public static final Long LOGIN_USER_TTL = 30L;
public static final Long CACHE_NULL_TTL = 2L;

View File

@ -0,0 +1,52 @@
package com.hmdp.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.hmdp.dto.UserDTO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.hmdp.utils.RedisConstants.LOGIN_USER_KEY;
public class RefreshTokenInterceptor implements HandlerInterceptor {
private final StringRedisTemplate stringRedisTemplate;
public RefreshTokenInterceptor(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 获取token
String token = request.getHeader("authorization");
if (StrUtil.isBlank(token)) {
return true;
}
// HttpSession session = request.getSession();
// User user = (User) session.getAttribute("user");
System.out.println(stringRedisTemplate);
Map<Object, Object> userMap = stringRedisTemplate.opsForHash().entries(LOGIN_USER_KEY + token);
if (userMap.isEmpty()) {
return true;
}
// 将userMap转成userDto
UserDTO userdto = BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false);
// 将user信息保存到线程中
UserHolder.saveUser(userdto);
// 更新token的时间service层的那种写法是直接三十分钟后就会失效
// 但是实际应该是三十分钟没访问的话才失效
stringRedisTemplate.expire(LOGIN_USER_KEY + token, RedisConstants.LOGIN_USER_TTL, TimeUnit.MINUTES);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 移除用户
UserHolder.removeUser();
}
}

View File

@ -5,7 +5,7 @@ spring:
name: hmdp
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3307/hmdp?useSSL=false&serverTimezone=UTC
url: jdbc:mysql://127.0.0.1:3307/hmdp?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username: root
password: 123456
redis: