File was renamed from src/main/java/com/yckj/service/UserService.java |
| | |
| | | package com.yckj.service; |
| | | package com.whyc.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.sun.deploy.util.ArrayUtil; |
| | | import com.yckj.dto.UserLoginInfo; |
| | | import com.yckj.mapper.UserMapper; |
| | | import com.yckj.pojo.User; |
| | | import com.whyc.dto.UserLoginInfo; |
| | | import com.whyc.mapper.UserMapper; |
| | | import com.whyc.pojo.User; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.assertj.core.util.Arrays; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletRequest; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | |
| | | //servletContext中存储用户信息 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | List<UserLoginInfo> users = (List<UserLoginInfo>) servletContext.getAttribute("users"); |
| | | if(users==null){ |
| | | Map<String,UserLoginInfo> userMap = (Map<String, UserLoginInfo>) servletContext.getAttribute("users"); |
| | | if(userMap==null){ |
| | | log.info("servletContext首次存值"); |
| | | users = new LinkedList<>(); |
| | | users.add(new UserLoginInfo(username,timestamp)); |
| | | servletContext.setAttribute("users",users); |
| | | userMap = new HashMap<>(); |
| | | userMap.put(username,new UserLoginInfo(username,timestamp)); |
| | | servletContext.setAttribute("users",userMap); |
| | | }else{ |
| | | log.info("servletContext已经有值"); |
| | | users.add(new UserLoginInfo(username,timestamp)); |
| | | servletContext.setAttribute("users",users); |
| | | userMap.put(username,new UserLoginInfo(username,timestamp)); |
| | | servletContext.setAttribute("users",userMap); |
| | | } |
| | | } |
| | | return flag; |
| | |
| | | User user = (User) session.getAttribute("user"); |
| | | Long timestamp = (Long) session.getAttribute("timestamp"); |
| | | |
| | | List<UserLoginInfo> users = (List<UserLoginInfo>) servletContext.getAttribute("users"); |
| | | if(user==null){ |
| | | return false; |
| | | } |
| | | Map<String,UserLoginInfo> userMap = (Map<String, UserLoginInfo>) servletContext.getAttribute("users"); |
| | | |
| | | for (UserLoginInfo temp:users){ |
| | | if(temp.getUsername().equals(user.getUsername()) && temp.getTimestamp()==timestamp){ |
| | | UserLoginInfo userLoginInfo = userMap.get(user.getUsername()); |
| | | if(userLoginInfo.getUsername().equals(user.getUsername()) && userLoginInfo.getTimestamp().compareTo(timestamp)==0){ |
| | | //说明没有从其他地方登录 |
| | | }else{ |
| | | //说明从其他地方登录了,当前用户的session清除 |
| | | flag = true; |
| | | session.invalidate(); |
| | | } |
| | | } |
| | | return flag; |
| | | } |