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

redis存储session

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