This commit is contained in:
xiangwbs
2018-04-16 21:16:24 +08:00
parent 9806101ee2
commit 597d5da36a
6 changed files with 52 additions and 46 deletions
@@ -95,7 +95,9 @@ public class SysUserService {
if (old == null) {
throw new BusinessException("该用户不存在");
}
if (id.equals(CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID))) {
String token = ThreadLocalUtil.getToken();
String userName = (String) CommonDataUtil.getToken(token);
if (old.getUserName().equals(userName)) {
throw new BusinessException("不能删除当前登录用户");
}
if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) {
@@ -111,7 +113,6 @@ public class SysUserService {
result.setMessage("删除成功");
result.setSuccess(true);
return result;
}
/**
@@ -127,7 +128,9 @@ public class SysUserService {
if (old == null) {
throw new BusinessException("该用户不存在");
}
if (old.getId().equals(CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID))) {
String token = ThreadLocalUtil.getToken();
String userName = (String) CommonDataUtil.getToken(token);
if (old.getUserName().equals(userName)) {
throw new BusinessException("不能修改当前登录用户");
}
if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) {
@@ -160,6 +163,16 @@ public class SysUserService {
return sysUserRepository.findOne(id);
}
/**
* 根据用户名查找用户
*
* @param userName
* @return
*/
public SysUser getByUserName(String userName) {
return sysUserRepository.getByUserName(userName);
}
/**
* 列表查询
*
@@ -193,7 +206,9 @@ public class SysUserService {
if (old == null) {
throw new BusinessException("未查询到用户信息");
}
if (CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID).equals(id)) {
String token = ThreadLocalUtil.getToken();
String userName = (String) CommonDataUtil.getToken(token);
if (old.getUserName().equals(userName)) {
throw new BusinessException("不能重置当前登录用户");
}
if (CommonEnum.YesOrNoEnum.YES.getCode().equals(old.getAdmin())) {
@@ -259,7 +274,9 @@ public class SysUserService {
*/
public RestMessage login(HttpServletRequest request, String userName, String passWord, String checkCode) {
RestMessage restMessage = new RestMessage();
String imgCode = (String) CommonDataUtil.getToken(CommonConstant.KEY_CAPTCHA);
// String imgCode = (String) CommonDataUtil.getToken(CommonConstant.KEY_CAPTCHA);
HttpSession session = request.getSession();
String imgCode = (String) session.getAttribute(CommonConstant.KEY_CAPTCHA);
//验证验证码
if (!checkCode.equalsIgnoreCase(imgCode)) {
throw new BusinessException("验证码错误");
@@ -273,22 +290,21 @@ public class SysUserService {
if (!flag) {
throw new BusinessException("密码错误");
}
//保存登录数据
CommonDataUtil.setToken(CommonConstant.CURRENT_USER, userName);
CommonDataUtil.setToken(CommonConstant.CURRENT_USER_ID, user.getId());
//生成session
HttpSession session = request.getSession();
session.setAttribute(userName, userName);
//保存登录信息
SysUserLoginInOut loginInOut = new SysUserLoginInOut();
loginInOut.setCreateTime(new Date());
loginInOut.setUserId(user.getId());
loginInOut.setInoutType(CommonEnum.LoginInOutEnum.IN.getValue());
loginInOut.setIp(IpUtil.getIpAddr(request));
String ip = IpUtil.getIpAddr(request);
loginInOut.setIp(ip);
RestMessage save = loginInOutService.save(loginInOut);
if (!save.isSuccess()) {
throw new BusinessException("保存用户登录日志失败");
}
//保存登录数据
String token = RSAUtil.decrypt(userName + ip);
CommonDataUtil.setToken(token, userName);
restMessage.setData(token);
restMessage.setSuccess(true);
restMessage.setMessage("登录成功");
return restMessage;
@@ -302,14 +318,12 @@ public class SysUserService {
*/
public RestMessage logout(HttpServletRequest request) {
RestMessage restMessage = new RestMessage();
String userId = (String) CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID);
SysUser user = getById(userId);
String token = ThreadLocalUtil.getToken();
String userName = (String) CommonDataUtil.getToken(token);
SysUser user = getByUserName(userName);
if (user != null) {
//清空公共数据
CommonDataUtil.clearMap();
//清除session参数
HttpSession session = request.getSession();
session.removeAttribute(user.getUserName());
//清空缓存数据
CommonDataUtil.removeToken(token);
//保存登出信息
SysUserLoginInOut loginInOut = new SysUserLoginInOut();
loginInOut.setCreateTime(new Date());
@@ -411,16 +425,6 @@ public class SysUserService {
return listDto;
}
/**
* 根据用户名查找用户
*
* @param userName
* @return
*/
private SysUser getByUserName(String userName) {
return sysUserRepository.getByUserName(userName);
}
/**
* 校验密码
*
@@ -8,7 +8,7 @@ import java.util.Map;
* 说明: 公共数据类
*/
public class CommonDataUtil {
private static Map<String, Object> token=new HashMap<>();
private static Map<String, Object> token = new HashMap<>();
private CommonDataUtil() {
}
@@ -21,6 +21,10 @@ public class CommonDataUtil {
token.put(key, value);
}
public static void removeToken(String key) {
token.remove(key);
}
public static void clearMap() {
token.clear();
}
@@ -11,7 +11,7 @@ public class ThreadLocalUtil {
}
public static String getToken() {
return (String)token.get();
return token.get();
}
public static void setToken(String tokenVal) {
@@ -1,7 +1,6 @@
package com.xwbing.util.captcha;
import com.xwbing.constant.CommonConstant;
import com.xwbing.util.CommonDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,6 +8,7 @@ import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
@@ -33,10 +33,10 @@ public class CaptchaServlet extends HttpServlet {
CaptchaUtil tool = new CaptchaUtil();
StringBuffer code = new StringBuffer();
BufferedImage image = tool.genRandomCodeImage(code);
// HttpSession session = req.getSession();
// session.removeAttribute(CommonConstant.KEY_CAPTCHA);
// session.setAttribute(CommonConstant.KEY_CAPTCHA, code.toString());
CommonDataUtil.setToken(CommonConstant.KEY_CAPTCHA, code.toString());
HttpSession session = req.getSession();
session.setMaxInactiveInterval(60 * 60);//单位为秒,-1永不过期
session.setAttribute(CommonConstant.KEY_CAPTCHA, code.toString());
// CommonDataUtil.setToken(CommonConstant.KEY_CAPTCHA, code.toString());
// 将内存中的图片通过流形式输出到客户端
OutputStream out = res.getOutputStream();
ImageIO.write(image, "JPEG", out);
@@ -2,7 +2,6 @@ package com.xwbing.controller.sys;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.annotation.LogInfo;
import com.xwbing.constant.CommonConstant;
import com.xwbing.constant.CommonEnum;
import com.xwbing.domain.entity.sys.SysAuthority;
import com.xwbing.domain.entity.sys.SysRole;
@@ -16,6 +15,7 @@ import com.xwbing.service.sys.SysUserService;
import com.xwbing.util.CommonDataUtil;
import com.xwbing.util.JsonResult;
import com.xwbing.util.RestMessage;
import com.xwbing.util.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@@ -152,7 +152,9 @@ public class SysUserControl {
@ApiOperation(value = "获取当前登录用户信息")
@GetMapping("getLoginUserInfo")
public JSONObject getLoginUserInfo() {
SysUser sysUser = sysUserService.getById((String) CommonDataUtil.getToken(CommonConstant.CURRENT_USER_ID));
String token = ThreadLocalUtil.getToken();
String userName = (String) CommonDataUtil.getToken(token);
SysUser sysUser = sysUserService.getByUserName(userName);
if (sysUser == null) {
return JsonResult.toJSONObj("未获取到当前登录用户信息");
}
@@ -2,9 +2,9 @@ package com.xwbing.handler;
import com.alibaba.fastjson.JSON;
import com.xwbing.configuration.DispatcherServletConfig;
import com.xwbing.constant.CommonConstant;
import com.xwbing.util.CommonDataUtil;
import com.xwbing.util.RestMessage;
import com.xwbing.util.ThreadLocalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -45,13 +45,9 @@ public class LoginInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if (!set.contains(servletPath) && !servletPath.contains("login") && !servletPath.contains("test")) {
// HttpSession session = request.getSession();
// if (session.getAttribute(CommonConstant.CURRENT_USER)!=null) {
// return true;
// }else {
// return false;
// }
if (CommonDataUtil.getToken(CommonConstant.CURRENT_USER) != null) {
String token = request.getParameter("token");
if (token != null && CommonDataUtil.getToken(token)!=null) {
ThreadLocalUtil.setToken(token);
return true;
} else {
logger.error("用户未登录");