mirror of
https://github.com/qiurunze123/miaosha.git
synced 2023-11-19 22:41:03 +08:00
分布式session单点爆破
This commit is contained in:
parent
e94690a2b0
commit
984f922b81
|
@ -31,9 +31,10 @@
|
|||
3.只返回和业务有关的<br>
|
||||
|
||||
#### [解决分布式session]()
|
||||
--生成一个随机的uuid一类的写回到cookie中
|
||||
--redis 内存写入
|
||||
--下一个页面拿到uuid 内存取对象
|
||||
--生成随机的uuid作为cookie返回并redis内存写入
|
||||
--拦截器每次拦截方法,来重新获根据cookie获取对象
|
||||
--下一个页面拿到key重新获取对象
|
||||
--HandlerMethodArgumentResolver 方法 supportsParameter 如果为true 执行 resolveArgument 方法获取miaoshauser对象
|
||||
--如果有缓存的话 这个功能实现起来就和简单,在一个用户访问接口的时候我们把访问次数写到缓存中,在加上一个有效期。
|
||||
通过拦截器. 做一个注解 @AccessLimit 然后封装这个注解,可以有效的设置每次访问多少次,有效时间是否需要登录!
|
||||
#### [通用缓存key的封装采用什么设计模式]()
|
||||
|
|
|
@ -3,10 +3,13 @@ package com.geekq.miaosha.access;
|
|||
import com.alibaba.fastjson.JSON;
|
||||
import com.geekq.miaosha.common.enums.ResultStatus;
|
||||
import com.geekq.miaosha.common.resultbean.ResultGeekQ;
|
||||
import com.geekq.miaosha.controller.LoginController;
|
||||
import com.geekq.miaosha.domain.MiaoshaUser;
|
||||
import com.geekq.miaosha.redis.RedisService;
|
||||
import com.geekq.miaosha.service.MiaoShaUserService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
|
@ -23,6 +26,8 @@ import static com.geekq.miaosha.common.enums.ResultStatus.SESSION_ERROR;
|
|||
@Service
|
||||
public class AccessInterceptor extends HandlerInterceptorAdapter{
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccessInterceptor.class);
|
||||
|
||||
@Autowired
|
||||
MiaoShaUserService userService;
|
||||
|
||||
|
@ -32,7 +37,11 @@ public class AccessInterceptor extends HandlerInterceptorAdapter{
|
|||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
/**
|
||||
* 获取调用 获取主要方法
|
||||
*/
|
||||
if(handler instanceof HandlerMethod) {
|
||||
logger.info("打印拦截方法handler :{} ",handler);
|
||||
MiaoshaUser user = getUser(request, response);
|
||||
UserContext.setUser(user);
|
||||
HandlerMethod hm = (HandlerMethod)handler;
|
||||
|
|
|
@ -93,7 +93,7 @@ public class MiaoShaUserService {
|
|||
if(!calcPass.equals(dbPass)){
|
||||
throw new GlobleException(PASSWORD_ERROR);
|
||||
}
|
||||
//生成cookie
|
||||
//生成cookie 将session返回游览器 分布式session
|
||||
String token= UUIDUtil.uuid();
|
||||
addCookie(response, token, user);
|
||||
return true ;
|
||||
|
@ -102,6 +102,7 @@ public class MiaoShaUserService {
|
|||
private void addCookie(HttpServletResponse response, String token, MiaoshaUser user) {
|
||||
redisService.set(MiaoShaUserKey.token, token, user);
|
||||
Cookie cookie = new Cookie(COOKIE_NAME_TOKEN, token);
|
||||
//设置有效期
|
||||
cookie.setMaxAge(MiaoShaUserKey.token.expireSeconds());
|
||||
cookie.setPath("/");
|
||||
response.addCookie(cookie);
|
||||
|
|
Loading…
Reference in New Issue
Block a user