| | |
| | | package com.whyc.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.UserConstant; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.UserClient; |
| | | import com.whyc.mapper.BaojigroupUsrMapper; |
| | | import com.whyc.mapper.PageParamMapper; |
| | | import com.whyc.mapper.PermitGroupUserMapper; |
| | | import com.whyc.mapper.UserMapper; |
| | | import com.whyc.pojo.db_app_sys.PageParam; |
| | | import com.whyc.pojo.db_user.User; |
| | | import com.whyc.util.CommonUtil; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.MD5Util; |
| | | import com.whyc.util.RSAUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.apache.shiro.authc.UnknownAccountException; |
| | | import org.apache.shiro.authc.UsernamePasswordToken; |
| | | import org.apache.shiro.subject.Subject; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.cache.annotation.Cacheable; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletContext; |
| | | import java.security.InvalidParameterException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class UserService { |
| | | |
| | | @Resource |
| | | private UserMapper userMapper; |
| | | private UserMapper mapper; |
| | | |
| | | @Autowired(required = false) |
| | | private BaojigroupUsrMapper bjgroupUsrMapper; |
| | | |
| | | @Autowired(required = false) |
| | | private PermitGroupUserMapper pergroupUserMapper; |
| | | |
| | | @Resource |
| | | private UserBridgeService userBridgeService; |
| | | |
| | | @Autowired |
| | | private ApplicationContext applicationContext; |
| | | private PageParamMapper pageParamMapper; |
| | | |
| | | |
| | | public Response add(User user) { |
| | | try { |
| | | User userFound = userBridgeService.findPasswordByUserName(user.getName()); |
| | | if (userFound.getId() != 0) { |
| | | return new Response<>().set(1, false, "用户已存在"); |
| | | } |
| | | if (userMapper.addJudge(user.getName(),user.getPhoneNumber())>0){ |
| | | return new Response<>().set(1, false, "用户已存在"); |
| | | } |
| | | //user.setCreateTime(new Date()); |
| | | String[] dataArr = RSAUtil.decryptFrontP(user.getPwd(), RSAUtil.fontSeparator); |
| | | String password = dataArr[0]; |
| | | String passwordMD5 = dataArr[1]; |
| | | if(!CommonUtil.EncryptionMD5(password).equals(passwordMD5)){ |
| | | throw new InvalidParameterException("参数校验失败"); |
| | | } |
| | | user.setPwd(RSAUtil.encrypt(password,RSAUtil.getPublicKey())); |
| | | int flag = userMapper.insert(user); |
| | | if (flag > 0) { |
| | | return new Response<>().set(1, true); |
| | | } else { |
| | | return new Response<>().set(1, false, "添加失败"); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return new Response<>().set(0); |
| | | //查询所有用户信息 |
| | | public Response getAllUser(String uname, int pageCurr, int pageSize) { |
| | | PageHelper.startPage(pageCurr,pageSize); |
| | | List<User> list=mapper.getAllUser(uname); |
| | | PageInfo pageInfo=new PageInfo(list); |
| | | return new Response().setII(1,list!=null,pageInfo,"查询所有用户信息"); |
| | | } |
| | | |
| | | //新添加用户信息 |
| | | @Transactional |
| | | public Response addUser(User uinf) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | //判断是否存在普通用户(uid>100),若无,初始为10001 |
| | | int judgecount=mapper.judgeUname(); |
| | | if(judgecount==0){ |
| | | uinf.setId(10001); |
| | | } |
| | | String pwd= RSAUtil.encrypt("123456", RSAUtil.getPublicKey()); |
| | | uinf.setPwd(pwd); |
| | | wrapper.select("id","name"); |
| | | wrapper.eq("name",uinf.getName()); |
| | | User user=mapper.selectOne(wrapper); |
| | | if(user!=null){ |
| | | return new Response().setII(1,false,user,"用户名已存在"); |
| | | } |
| | | uinf.setCreateTime(new Date()); |
| | | int bl=mapper.insert(uinf); |
| | | return new Response().set(1,bl>0,bl>0?"用户添加成功":"用户添加失败"); |
| | | } |
| | | //修改密码 |
| | | public Response changeSnId(String uname,String oldSnId, String newSnId) { |
| | | if(uname==null||uname.equals("")){ |
| | | return new Response().set(1,false,"找不到用户"); |
| | | } |
| | | //验证老密码是否正确 |
| | | String snIdRsa=mapper.selectSnId(uname); |
| | | //解密 |
| | | String snId=RSAUtil.decrypt(snIdRsa,RSAUtil.getPrivateKey()); |
| | | String[] decOld=RSAUtil.decryptFrontP(oldSnId,RSAUtil.fontSeparator); |
| | | String oldId=decOld[0]; |
| | | String[] newOld=RSAUtil.decryptFrontP(newSnId,RSAUtil.fontSeparator); |
| | | String newId=newOld[0]; |
| | | if(oldId.equals(snId)){ |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.set("pwd",RSAUtil.encrypt(newId,RSAUtil.getPublicKey())); |
| | | wrapper.eq("name",uname); |
| | | int flag=mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"修改成功":"修改失败"); |
| | | }else { |
| | | return new Response().set(1,false,"原密码不正确"); |
| | | } |
| | | } |
| | | |
| | | @CacheEvict(value = "userPage",allEntries = true) |
| | | public Response addByRSA(User user) { |
| | | User userFound = userBridgeService.findPasswordByUserName(user.getName()); |
| | | if (userFound.getId() != 0) { |
| | | return new Response<>().set(1, false, "用户名已存在"); |
| | | } |
| | | User userOfPhoneNumber = userBridgeService.getUserByPhoneNumber(user.getPhoneNumber()); |
| | | if (userOfPhoneNumber.getId()!=0) { |
| | | return new Response<>().set(1, false, "手机号已存在"); |
| | | } |
| | | //String password = URLDecoder.decode(user.getUpassword(), "utf-8"); |
| | | String password = user.getPwd(); |
| | | String[] dataArr = RSAUtil.decryptFront(password, RSAUtil.fontSeparator); |
| | | String pwd = RSAUtil.encrypt(dataArr[0],RSAUtil.getPublicKey()); |
| | | user.setPwd(pwd); |
| | | user.setCreateTime(new Date()); |
| | | int flag = userMapper.insert(user); |
| | | if (flag > 0) { |
| | | return new Response<>().set(1, true); |
| | | //校验密码 |
| | | public Response checkSnId(String uname, String checksnId) { |
| | | //查询当前用户的密码 |
| | | String snIdRsa = mapper.selectSnId(uname); |
| | | String snId = RSAUtil.decrypt(snIdRsa, RSAUtil.getPrivateKey()); |
| | | String[] oldchecksnId = RSAUtil.decryptFrontP(checksnId, RSAUtil.fontSeparator); |
| | | String oldId = oldchecksnId[0]; |
| | | if (oldId.equals(snId)) { |
| | | return new Response().set(1, true, "密码正确"); |
| | | } else { |
| | | return new Response<>().set(1, false, "添加失败"); |
| | | return new Response().set(1, false, "密码不正确"); |
| | | } |
| | | } |
| | | //重置密码 |
| | | public Response resetSnId(int uid ) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.set("pwd",RSAUtil.encrypt("123456",RSAUtil.getPublicKey())); |
| | | wrapper.eq("id",uid); |
| | | int flag=mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"修改成功":"修改失败"); |
| | | |
| | | /*public Response registerUser(User user){ |
| | | try { |
| | | User userFound = userBridgeService.findPasswordByUserName(user.getName()); |
| | | if (userFound.getId() != 0) { |
| | | return new Response<>().set(1, false, "用户名已存在"); |
| | | } |
| | | //删除用户信息 |
| | | @Transactional |
| | | public Response delUser(String uname) { |
| | | //获取用户的uid |
| | | User User=mapper.getUinfByUname(uname); |
| | | if(User==null){ |
| | | return new Response().set(1,false,"用户不存在"); |
| | | }else { |
| | | int uid=User.getId(); |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | wrapper.eq("name",uname); |
| | | int bl=mapper.delete(wrapper); |
| | | if(bl>0){ |
| | | //删除权限组下的用户 |
| | | UpdateWrapper wrapper1=new UpdateWrapper(); |
| | | wrapper1.eq("uid",uid); |
| | | pergroupUserMapper.delete(wrapper1); |
| | | //删除包机组下的用户 |
| | | UpdateWrapper wrapper2=new UpdateWrapper(); |
| | | wrapper2.eq("uid",uid); |
| | | bjgroupUsrMapper.delete(wrapper2); |
| | | } |
| | | User userOfMobilephone = userBridgeService.findUserByMobilephone(user.getPhoneNumber()); |
| | | if (userOfMobilephone.getId()!=0) { |
| | | return new Response<>().set(1, false, "手机号已存在"); |
| | | } |
| | | //String password = URLDecoder.decode(user.getUpassword(), "utf-8"); |
| | | String password = user.getPwd(); |
| | | |
| | | String[] dataArr = RSAUtil.decryptFront(password, RSAUtil.fontSeparator); |
| | | String pwd = RSAUtil.encrypt(dataArr[0],RSAUtil.getPublicKey()); |
| | | user.setPwd(pwd); |
| | | int flag = userMapper.insert(user); |
| | | if (flag > 0) { |
| | | //添加成功,分配到默认初始化权限组 |
| | | return new Response<>().set(1, true,"注册成功"); |
| | | } else { |
| | | return new Response<>().set(1, false, "注册失败"); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return new Response<>().set(0); |
| | | return new Response().setII(1,bl>0,bl,"删除成功"); |
| | | } |
| | | } |
| | | /* |
| | | public Response login(String uname, String usnId, Integer platFrom, HttpServletRequest request) { |
| | | Response response = new Response(); |
| | | String[] dataArr = RSAUtil.decryptFrontP(usnId, RSAUtil.fontSeparator); |
| | | //验签md5 |
| | | if(dataArr[0]==null||dataArr[1]==null){ |
| | | return response.set(1, false, "密码验签失败"); |
| | | } |
| | | if (!dataArr[1].equals(MD5Util.encryptMD5(org.apache.commons.lang3.StringUtils.trim(dataArr[0])))) { |
| | | return response.set(1, false, "密码验签失败"); |
| | | } |
| | | UsernamePasswordToken userToken = new UsernamePasswordToken(uname, dataArr[0]); |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | try { |
| | | subject.login(userToken); |
| | | } catch (Exception e) { |
| | | if(e instanceof UnknownAccountException){ |
| | | return response.set(1,false,"账号不存在"); |
| | | } |
| | | return response.set(1,false,"密码错误"); |
| | | } |
| | | //Session存储当前用户及权限组列表 |
| | | User userDB = (User) subject.getPrincipal(); |
| | | userDB.setPwd(null); |
| | | //登录成功 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | servletContext.setAttribute(uname, request.getSession().getId()); |
| | | request.getSession().setMaxInactiveInterval(60*30); |
| | | request.getSession().setAttribute("user", userDB); |
| | | |
| | | return response.setII(1,true, userDB,"登录成功"); |
| | | }*/ |
| | | |
| | | public User getById(int id) { |
| | | User user = userMapper.selectById(id); |
| | | user.setPwd((String) CommonUtil.objectNull); |
| | | return user; |
| | | public void logout() { |
| | | Subject subject = SecurityUtils.getSubject(); |
| | | subject.logout(); |
| | | } |
| | | //将用户添加至100~10000管理员 |
| | | public Response improveRole(int uid) { |
| | | //判断表是否存在 |
| | | String tableName = mapper.existTable(); |
| | | if(tableName==null){ |
| | | ///创建100~1000的id表,不存在则创建 |
| | | mapper.createNumber(); |
| | | //插入默认数据 |
| | | String sql=" INSERT INTO temp_numbers(unumber) " + |
| | | " VALUES "; |
| | | for(int i=101;i<=10000;i++){ |
| | | sql+="("+i+")"; |
| | | if(i!=10000){ |
| | | sql+=","; |
| | | } |
| | | } |
| | | mapper.setUnumber(sql); |
| | | } |
| | | String minUid=mapper.getIn10000(); |
| | | if(minUid==null){ |
| | | return new Response().set(1,false,"管理员个数超过上限"); |
| | | } |
| | | UpdateWrapper wrapper =new UpdateWrapper(); |
| | | wrapper.set("id",minUid); |
| | | wrapper.eq("id",uid); |
| | | int flag= mapper.update(null,wrapper); |
| | | return new Response().set(1,flag>0,flag>0?"身份变更成功":"身份变更失败"); |
| | | } |
| | | //将管理员变成普通用户 |
| | | public Response dropRole(int uid, HttpServletRequest request) { |
| | | //查询最大的uid |
| | | String maxUid=mapper.getMaxUid(); |
| | | if(maxUid==null){ |
| | | maxUid="10001"; |
| | | } |
| | | //根据用户id查询用户信息 |
| | | QueryWrapper qWrapper=new QueryWrapper(); |
| | | qWrapper.eq("id",uid); |
| | | qWrapper.last("limit 1"); |
| | | User uinf=mapper.selectOne(qWrapper); |
| | | //编辑 |
| | | UpdateWrapper wrapper =new UpdateWrapper(); |
| | | wrapper.set("id",Integer.valueOf(maxUid)+1); |
| | | wrapper.eq("id",uid); |
| | | int flag= mapper.update(null,wrapper); |
| | | //如果是普通用户自己强退,将用户名对应的sessionId变更 |
| | | ServletContext servletContext = request.getServletContext(); |
| | | servletContext.setAttribute(uinf.getName(),"123456"); |
| | | return new Response().set(1,flag>0,flag>0?"身份变更成功":"身份变更失败"); |
| | | } |
| | | |
| | | public List<User> getAll() { |
| | | List<User> users = userMapper.selectList((Wrapper<User>) CommonUtil.objectNull); |
| | | return users; |
| | | public Response getUserNameList() { |
| | | QueryWrapper<User> query = Wrappers.query(); |
| | | query.select("name"); |
| | | List<String> userNameList = mapper.selectList(query).stream().map(User::getName).collect(Collectors.toList()); |
| | | return new Response().set(1,userNameList); |
| | | } |
| | | |
| | | @Cacheable(value = "userPage") |
| | | public Response getPage(int pageNum,int pageSize) { |
| | | PageHelper.startPage(pageNum,pageSize); |
| | | List<User> list = userMapper.selectList((Wrapper<User>) CommonUtil.objectNull); |
| | | for (User user:list) { |
| | | user.setPwd((String) CommonUtil.objectNull); |
| | | //登录检测 |
| | | public Response checkUserWebSocket(HttpSession httpSession){ |
| | | Response model = new Response(); |
| | | try { |
| | | User user = (User) httpSession.getAttribute("user"); |
| | | if(user!=null){ |
| | | String sessionId = (String) httpSession.getServletContext().getAttribute(user.getName()); |
| | | if(httpSession.getId().equals(sessionId)){ |
| | | model.set(1,user,null); |
| | | }else{ |
| | | if(sessionId.equals("123456")){ |
| | | model.set(1,false,"身份权限变更,请重新登录"); |
| | | }else{ |
| | | model.set(1,false,"不同主机登录"); |
| | | } |
| | | //用户在其他主机登录,强迫用户在本机的session失效 |
| | | httpSession.invalidate(); |
| | | } |
| | | } |
| | | else { |
| | | model.set(1,false,"用户信息失效,请重新登录"); |
| | | } |
| | | }catch (Exception e){ |
| | | model.set(1,false,"登录信息失效,重新登录"); |
| | | } |
| | | PageInfo<User> pageInfo = new PageInfo<>(list); |
| | | return new Response().set(1,pageInfo,"查询成功"); |
| | | return model; |
| | | } |
| | | //查询所有用户信息(不分页用于下拉) |
| | | public Response getUinf() { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.select("id","name"); |
| | | wrapper.gt("id",100); |
| | | wrapper.orderByAsc("id"); |
| | | List<User> list=mapper.selectList(wrapper); |
| | | return new Response().setII(1,list!=null,list,"查询所有用户信息(不分页用于下拉)"); |
| | | } |
| | | //根据uId获取设备信息 |
| | | public User getUinfByUId(int uId){ |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("id",uId); |
| | | wrapper.last("limit 1"); |
| | | User uinf=mapper.selectOne(wrapper); |
| | | return uinf; |
| | | } |
| | | //编辑用户信息 |
| | | @Transactional |
| | | public Response updateUinf(User uinf) { |
| | | UpdateWrapper wrapper=new UpdateWrapper(); |
| | | if(uinf.getPhoneNumber()!=null){ |
| | | wrapper.set("phone_number",uinf.getPhoneNumber()); |
| | | } |
| | | if(uinf.getSex()!=null){ |
| | | wrapper.set("sex",uinf.getSex()); |
| | | } |
| | | if(uinf.getEmail()!=null){ |
| | | wrapper.set("email",uinf.getEmail()); |
| | | } |
| | | if(uinf.getRole()!=null){ |
| | | wrapper.set("role",uinf.getRole()); |
| | | } |
| | | wrapper.eq("id",uinf.getId()); |
| | | mapper.update((User) ActionUtil.objeNull,wrapper); |
| | | |
| | | return new Response().set(1,true,"编辑用户信息成功"); |
| | | } |
| | | |
| | | @CacheEvict(value = "userPage",allEntries = true) |
| | | public Response update(User user) { |
| | | User userFound = userBridgeService.findPasswordByUserName(user.getName()); |
| | | User userOfPhoneNumber = userBridgeService.getUserByPhoneNumber(user.getPhoneNumber()); |
| | | if (userFound.getId() != 0 && !userFound.getId().equals(user.getId())) { |
| | | return new Response().set(1,false,"用户名重复"); |
| | | } |
| | | if( userOfPhoneNumber.getId()!=0 && !userFound.getId().equals(user.getId())){ |
| | | return new Response().set(1,false,"手机号重复"); |
| | | } |
| | | user.setPwd(null); |
| | | if(userMapper.updateById(user)>0){ |
| | | return new Response().set(1,true,"修改成功"); |
| | | }else{ |
| | | return new Response().set(1,false,"修改失败"); |
| | | } |
| | | |
| | | //根据uname查询用户 |
| | | public User getUserByUserName(String uname){ |
| | | QueryWrapper queryWrapper = new QueryWrapper(); |
| | | queryWrapper.select("id","name","create_time","phone_number"); |
| | | queryWrapper.eq("name",uname); |
| | | queryWrapper.last("limit 1"); |
| | | User User = mapper.selectOne(queryWrapper); |
| | | return User; |
| | | } |
| | | /**锁定账号*/ |
| | | public void lock(Integer uId) { |
| | | UpdateWrapper<User> wrapper = Wrappers.update(); |
| | | wrapper.set("status", UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getValue()).set("lock_time",new Date()).eq("uId",uId); |
| | | mapper.update((User) ActionUtil.objeNull,wrapper); |
| | | } |
| | | |
| | | |
| | | /**更新登录时间*/ |
| | | public void updateLoginTime(Integer uId) { |
| | | UpdateWrapper<User> wrapper = Wrappers.update(); |
| | | wrapper.set("last_login_time", new Date()).eq("id",uId); |
| | | mapper.update((User) ActionUtil.objeNull,wrapper); |
| | | } |
| | | // 将所有登陆的用户的信息存到application中 |
| | | public void setApplication(User user) { |
| | | ServletContext application = CommonUtil.getApplication(); |
| | | ServletContext application = ActionUtil.getApplication(); |
| | | //查看全局中存储的users的Map的key-value集合 |
| | | Map<String, UserClient> map = (Map) application.getAttribute("users"); |
| | | if (map == CommonUtil.objectNull) { |
| | | if (map == ActionUtil.objeNull) { |
| | | map = new HashMap<String, UserClient>(); |
| | | } else { |
| | | //如果集合中有值,则获取当前用户对应的用户信息,key为用户名username,Value为用户名,存储的时间 |
| | | UserClient client = map.get(user.getName()); |
| | | if (client != CommonUtil.objectNull) { //已存在 |
| | | if (client != ActionUtil.objeNull) { //已存在 |
| | | map.remove(user.getName()); |
| | | } |
| | | } |
| | | Long login_time = new Date().getTime(); |
| | | CommonUtil.getSession().setAttribute("login_time", login_time); |
| | | map.put(user.getName(), new UserClient(CommonUtil.getRequest().getRemoteAddr(),user,login_time)); |
| | | ActionUtil.getSession().setAttribute("login_time", login_time); |
| | | map.put(user.getName(), new UserClient(ActionUtil.getRequest().getRemoteAddr(),user,login_time)); |
| | | application.setAttribute("users", map); |
| | | } |
| | | |
| | | |
| | | public Response updatePasswordByRSA(User user, String newPwd){ |
| | | String[] dataArr = RSAUtil.decryptFrontP(newPwd, RSAUtil.fontSeparator); |
| | | newPwd = dataArr[0]; |
| | | String passwordMD5 = dataArr[1]; |
| | | if(!CommonUtil.EncryptionMD5(newPwd).equals(passwordMD5)){ |
| | | throw new InvalidParameterException("参数校验失败"); |
| | | //验证密码的时效性 |
| | | public int checkPasswordValidity(User uinf) { |
| | | int flag=1; |
| | | //获取系统设置的密码时效(57,15) |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("categoryId",15); |
| | | wrapper.eq("id",57); |
| | | wrapper.last("limit 1"); |
| | | PageParam pageParam=pageParamMapper.selectOne(wrapper); |
| | | if(pageParam!=null){ |
| | | int dayDifference=ActionUtil.daysBetween(uinf.getPasswordUpdateTime(),new Date()); |
| | | if((pageParam.getStatus()>0)&&(dayDifference>pageParam.getStatus())){ |
| | | flag= -1; |
| | | } |
| | | } |
| | | String pwd = RSAUtil.encrypt(newPwd,RSAUtil.getPublicKey()); |
| | | user.setPwd(pwd); |
| | | if (userMapper.updateById(user)>0){ |
| | | return new Response().set(1,true,"新密码修改成功"); |
| | | }else { |
| | | return new Response().set(0,false,"新密码修改失败"); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | public List<User> searchCS_All2() { |
| | | return mapper.searchCS_All2(); |
| | | } |
| | | } |