Commit df3d1aee by 陶湘宇 Committed by 284718418@qq.com

redis存储session

parent 89abec5a
......@@ -10,6 +10,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.timer.api.utils.redis.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
......@@ -53,6 +54,8 @@ public class LogAspect
@Resource
private HttpSession session;
@Resource
private RedisUtil redisUtil;
// 配置织入点
@Pointcut("@annotation(cn.timer.api.aspect.lang.annotation.Log)")
......@@ -96,7 +99,7 @@ public class LogAspect
// 获取当前的用户
// LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
QyzxEmpLogin eld = (QyzxEmpLogin)session.getAttribute("ui");
QyzxEmpLogin eld = (QyzxEmpLogin)redisUtil.getAttribute(session.getId(),"ui");
// if (eld == null && controllerLog.businessType() == BusinessType.SEND_CODE) {
// Object[] objs = joinPoint.getArgs();
......
......@@ -6,6 +6,7 @@ import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import cn.timer.api.utils.redis.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
......@@ -48,6 +49,8 @@ public class RoleAspect {
@Resource
private HttpSession session;
@Resource
private RedisUtil redisUtil;
// 配置织入点
@Pointcut("@annotation(cn.timer.api.aspect.lang.annotation.Role)")
......@@ -67,7 +70,7 @@ public class RoleAspect {
sysRoleType.add(t.getType());
}
QyzxEmpLogin eld = BeanUtil.toBean(session.getAttribute("ui"), QyzxEmpLogin.class);
QyzxEmpLogin eld = BeanUtil.toBean(redisUtil.getAttribute(session.getId(),"ui"), QyzxEmpLogin.class);
Integer orgCode = eld.getOrgId();
Integer count = new LambdaQueryChainWrapper<QyzxEmpEntAsso>(qyzxEmpEntAssoMapper)
......
......@@ -51,17 +51,20 @@ public class RedisSessionInterceptor implements HandlerInterceptor {
*/
// 无论访问的地址是不是正确的,都进行登录验证,登录成功后的访问再进行分发,404的访问自然会进入到错误控制器中
HttpSession session = request.getSession();
Object ai = session.getAttribute("ai");
Object ui = session.getAttribute("ui");
if(session.getAttribute("ui")==null&&session.getAttribute("ai")==null) {
System.out.println("SID===="+session.getId());
Object ai = redisUtil.getAttribute(session.getId(),"ai");
Object ui = redisUtil.getAttribute(session.getId(),"ui");
if(ui==null&&ai==null) {
response401(response);
return false;
}
try {
QyzxEmpLogin eld = BeanUtil.toBean(session.getAttribute("ui"), QyzxEmpLogin.class);
QyzxEmpLogin eld = BeanUtil.toBean(ui, QyzxEmpLogin.class);
if(redisUtil.get("BlockUser"+eld.getId())!=null){
redisUtil.del("BlockUser"+eld.getId());
session.removeAttribute("ui");
redisUtil.del(session.getId());
response302(response);
return false;
}
......
......@@ -3,6 +3,7 @@ package cn.timer.api.config.interceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import cn.timer.api.utils.redis.RedisUtil;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
......@@ -23,6 +24,8 @@ public class UserMethodArgumentResolver implements HandlerMethodArgumentResolver
@Resource
private HttpSession session;
@Resource
private RedisUtil redisUtil;
public UserMethodArgumentResolver() {
}
......@@ -42,8 +45,9 @@ public class UserMethodArgumentResolver implements HandlerMethodArgumentResolver
// String webRequestid = webRequest.getSessionId();
// String sessionid = session.getId();
QyzxEmpLogin eld = BeanUtil.toBean(session.getAttribute("ui"), QyzxEmpLogin.class);
QyzxEmpLogin eld = BeanUtil.toBean(redisUtil.getAttribute(session.getId(),"ui"), QyzxEmpLogin.class);
String token = BeanUtil.toBean(session.getAttribute(CacheKeyConstant.TOKEN), String.class);
System.out.println("token+++++===="+token);
String refreshToken = BeanUtil.toBean(session.getAttribute(CacheKeyConstant.REFRESH_TOKEN), String.class);
// Object phone = webRequest.getAttribute(currentUserAnnotation.id(),
......
......@@ -152,7 +152,7 @@ public class LoginController {
@Value("${config-8timer.authentication-code}")
public String authentication_code;
//2020/0517 redis缓存taoxy
@Autowired
private HttpSession session;
......@@ -257,7 +257,7 @@ public class LoginController {
String message = j.getString("Message");
if ("OK".equals(message)) {
session.setAttribute(phone, code);
redisUtil.set(phone, code);
// redisTemplate.set(phone, code);
// redisTemplate.expire(phone, 60);
......@@ -302,7 +302,7 @@ public class LoginController {
String message = j.getString("Message");
if ("OK".equals(message)) {
session.setAttribute(phone, code);
//session.setAttribute(phone, code);
redisUtil.set(phone, code,60*5);
return ResultUtil.data("发送验证码成功");
} else {
......@@ -328,7 +328,7 @@ public class LoginController {
String code = entRegisterDto.getCode().toString();
String codeRedis = session.getAttribute(phone) != null ? session.getAttribute(phone).toString() : "";
String codeRedis = redisUtil.get(phone) != null ? redisUtil.get(phone).toString() : "";
if(authentication_code !=null && !("").equals(authentication_code)) {
if (authentication_code.equals(code)) {
......@@ -368,7 +368,7 @@ public class LoginController {
} else {
if(authentication_code ==null || ("").equals(authentication_code) || !authentication_code.equals(code)) {
String codeRedis = session.getAttribute(phone) != null ? session.getAttribute(phone).toString() : "";
String codeRedis = redisUtil.get(phone) != null ? redisUtil.get(phone).toString() : "";
if (!code.equals(codeRedis)) {
return ResultUtil.error("短信验证码错误");
}
......@@ -403,7 +403,7 @@ public class LoginController {
String code = entRegisterDto.getCode();
if (entRegisterDto.getPwUpdateType() != 2) {
String codeRedis = session.getAttribute(phone) != null ? session.getAttribute(phone).toString() : "";
String codeRedis = redisUtil.get(phone)!= null ? redisUtil.get(phone).toString() : "";
if (code == null) {
return ResultUtil.error("请填写验证码");
}
......@@ -469,7 +469,7 @@ public class LoginController {
String phone = entRegisterDto.getPhone();
String code = entRegisterDto.getCode();
// String codeRedis = redisTemplate.get(phone).toString();
String codeRedis = session.getAttribute(phone) != null ? session.getAttribute(phone).toString() : "";
String codeRedis = redisUtil.get(phone) != null ? redisUtil.get(phone).toString() : "";
if(authentication_code !=null && !("").equals(authentication_code)) {
if (authentication_code.equals(code)) {
......@@ -745,7 +745,7 @@ public class LoginController {
return ResultUtil.error("请输入验证码");
}
String phone = entRegisterDto.getPhone();
String codeRedis = session.getAttribute(phone) != null ? session.getAttribute(phone).toString() : "";
String codeRedis = redisUtil.get(phone)!= null ? redisUtil.get(phone).toString() : "";
if(authentication_code !=null && !("").equals(authentication_code)) {
if (authentication_code.equals(code)) {
......@@ -913,10 +913,10 @@ public class LoginController {
// 需要更新最后一次登陆信息
// 用户信息缓存
session.setAttribute("ui", qyzxEmpLogin1);
System.err.println(session.getId());
Object ui = session.getAttribute("ui");
//session.setAttribute("ui", qyzxEmpLogin1);
redisUtil.setAttribute(session.getId(),"ui",qyzxEmpLogin1);
System.err.println("redis=>>session"+session.getId());
Object ui = redisUtil.getAttribute(session.getId(),"ui");
return ResultUtil.data(qyzxEmpLogin1);
}
......
......@@ -23,6 +23,7 @@ import cn.timer.api.controller.spmk.service.SpmkServiceImpl;
import cn.timer.api.dao.kqmk.KqglAssoLeaveRulesMapper;
import cn.timer.api.dto.qyzx.*;
import cn.timer.api.utils.Md5;
import cn.timer.api.utils.redis.RedisUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -85,6 +86,9 @@ public class QyzxController {
private HttpSession session;
@Autowired
private RedisUtil redisUtil;
@Autowired
private QyzxEmpEntAssoMapper qyzxEmpEntAssoMapper;
@Autowired
......@@ -351,7 +355,8 @@ public class QyzxController {
}
}
emp.setOrgId(orgCode);
session.setAttribute("ui", emp);
//session.setAttribute("ui", emp);
redisUtil.setAttribute(session.getId(),"ui", emp);
qyzxEmpLoginMapper.updateById(emp);
return ResultUtil.data(menus, "切换企业成功");
} else {
......
......@@ -8,6 +8,7 @@ import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import cn.timer.api.utils.UserIp;
import cn.timer.api.utils.redis.RedisConfig;
import cn.timer.api.utils.redis.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -33,6 +34,8 @@ import javax.servlet.http.HttpSession;
public class SuperLoginController {
@Autowired
private HttpSession session;
@Autowired
private RedisUtil redisUtil;
@PostMapping(value = "/adminLogin")
@ApiOperation(value = "运营后台登录", httpMethod = "POST", notes = "接口发布说明")
public Result<Object> adminLogin(@RequestBody EntRegisterDto entRegisterDto, HttpServletRequest request){
......@@ -44,16 +47,17 @@ public class SuperLoginController {
if(adminAccount==null){
return ResultUtil.error("用户不存在或密码错误");
}
session=request.getSession();
session.setAttribute("ai", adminAccount);
// session=request.getSession();
redisUtil.setAttribute(session.getId(),"ai", adminAccount);
System.err.println(session.getId());
return ResultUtil.data(adminAccount);
};
@PostMapping(value = "/adminOutLogin")
@ApiOperation(value = "运营后台退出", httpMethod = "POST", notes = "接口发布说明")
public Result<String> adminOutLogin(HttpServletRequest request){
session= request.getSession();
session.removeAttribute("ai");
//session= request.getSession();
// session.removeAttribute("ai");
redisUtil.del(session.getId());
return ResultUtil.data("退出成功");
}
}
......@@ -26,6 +26,23 @@ public class RedisUtil {
}
/**
* 模拟session的getAttrubute方法
* taoxy
* 0520
*/
public Object getAttribute(String sinkey,String key){
return redisTemplate.opsForHash().get(sinkey,key);
}
/**
* 模拟session的setAttrubute方法
* taoxy
* 0520
*/
public void setAttribute(String sinkey,String key,Object value){
redisTemplate.opsForHash().put(sinkey, key, value);
}
/**
* 指定缓存失效时间
* @param key 键
* @param time 时间(秒)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment