diff --git a/sql/mysql.sql b/sql/mysql.sql index e654e984..ba78cd2a 100644 --- a/sql/mysql.sql +++ b/sql/mysql.sql @@ -219,5 +219,8 @@ create table user update_time varchar(50) not null ); +create unique index user_username_uindex + on user (username); + insert into user (username, password, roleId, create_time, update_time) values ('admin', '21232f297a57a5a743894a0e4a801fc3', '0', '2021-04-13 14:14:57', '2021-04-13 14:14:57'); diff --git a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java index 66dedf23..72cefe1c 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java @@ -188,8 +188,8 @@ public class MediaConfig{ mediaServerItem.setRecordAssistPort(recordAssistPort); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - mediaServerItem.setCreateTime(format.format(new Date(System.currentTimeMillis()))); - mediaServerItem.setUpdateTime(format.format(new Date(System.currentTimeMillis()))); + mediaServerItem.setCreateTime(format.format(System.currentTimeMillis())); + mediaServerItem.setUpdateTime(format.format(System.currentTimeMillis())); return mediaServerItem; } diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/dto/LoginUser.java b/src/main/java/com/genersoft/iot/vmp/conf/security/dto/LoginUser.java index 4bb70178..93680fde 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/security/dto/LoginUser.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/security/dto/LoginUser.java @@ -92,4 +92,10 @@ public class LoginUser implements UserDetails, CredentialsContainer { public int getId() { return user.getId(); } + + public int getRoleId() { + return user.getRoleId(); + } + + } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/event/online/OnlineEventListener.java b/src/main/java/com/genersoft/iot/vmp/gb28181/event/online/OnlineEventListener.java index acdf2722..270fb360 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/event/online/OnlineEventListener.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/event/online/OnlineEventListener.java @@ -52,7 +52,7 @@ public class OnlineEventListener implements ApplicationListener { case VideoManagerConstants.EVENT_ONLINE_REGISTER: // 超时时间 redis.set(key, event.getDevice().getDeviceId(), sipConfig.getKeepaliveTimeOut()); - device.setRegisterTime(format.format(new Date(System.currentTimeMillis()))); + device.setRegisterTime(format.format(System.currentTimeMillis())); break; // 设备主动发送心跳触发的在线事件 case VideoManagerConstants.EVENT_ONLINE_KEEPLIVE: @@ -63,7 +63,7 @@ public class OnlineEventListener implements ApplicationListener { } else { redis.expire(key, sipConfig.getKeepaliveTimeOut()); } - device.setKeepaliveTime(format.format(new Date(System.currentTimeMillis()))); + device.setKeepaliveTime(format.format(System.currentTimeMillis())); break; // 设备主动发送消息触发的在线事件 case VideoManagerConstants.EVENT_ONLINE_MESSAGE: diff --git a/src/main/java/com/genersoft/iot/vmp/service/IUserService.java b/src/main/java/com/genersoft/iot/vmp/service/IUserService.java index 112af354..c170c021 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IUserService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IUserService.java @@ -12,11 +12,11 @@ public interface IUserService { User getUserByUsername(String username); - void addUser(User user); + int addUser(User user); - void deleteUser(int id); + int deleteUser(int id); List getAllUsers(); - void updateUsers(User user); + int updateUsers(User user); } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index d65b461f..62340734 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -276,7 +276,7 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR resetOnlineServerItem(serverItemFromConfig); setZLMConfig(serverItemFromConfig); }else { - String now = this.format.format(new Date(System.currentTimeMillis())); + String now = this.format.format(System.currentTimeMillis()); if (serverItem == null){ // 一个新的zlm接入wvp serverItem = new MediaServerItem(zlmServerConfig, sipConfig.getSipIp()); diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/UserServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/UserServiceImpl.java index e10fc795..32b6ad68 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/UserServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/UserServiceImpl.java @@ -13,8 +13,7 @@ public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; - - + @Override public User getUser(String username, String password) { return userMapper.select(username, password); @@ -33,12 +32,14 @@ public class UserServiceImpl implements IUserService { } @Override - public void addUser(User user) { - userMapper.add(user); + public int addUser(User user) { + User userByUsername = userMapper.getUserByUsername(user.getUsername()); + if (userByUsername != null) return 0; + return userMapper.add(user); } @Override - public void deleteUser(int id) { - userMapper.delete(id); + public int deleteUser(int id) { + return userMapper.delete(id); } @Override @@ -47,8 +48,8 @@ public class UserServiceImpl implements IUserService { } @Override - public void updateUsers(User user) { - userMapper.update(user); + public int updateUsers(User user) { + return userMapper.update(user); } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java index 82d9bdb5..89c1d6c1 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java @@ -109,7 +109,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { */ @Override public synchronized boolean updateDevice(Device device) { - String now = this.format.format(new Date(System.currentTimeMillis())); + String now = this.format.format(System.currentTimeMillis()); device.setUpdateTime(now); Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId()); if (deviceByDeviceId == null) { @@ -126,7 +126,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { String channelId = channel.getChannelId(); channel.setDeviceId(deviceId); channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); - String now = this.format.format(new Date(System.currentTimeMillis())); + String now = this.format.format(System.currentTimeMillis()); channel.setUpdateTime(now); DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId); if (deviceChannel == null) { @@ -463,7 +463,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { boolean result = false; streamProxyItem.setStreamType("proxy"); streamProxyItem.setStatus(true); - String now = this.format.format(new Date(System.currentTimeMillis())); + String now = this.format.format(System.currentTimeMillis()); streamProxyItem.setCreateTime(now); try { if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) { @@ -609,7 +609,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { @Override public void updateMediaServer(MediaServerItem mediaServerItem) { - String now = this.format.format(new Date(System.currentTimeMillis())); + String now = this.format.format(System.currentTimeMillis()); mediaServerItem.setUpdateTime(now); if (mediaServerMapper.queryOne(mediaServerItem.getId()) != null) { mediaServerMapper.update(mediaServerItem); diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java index b552e1e8..de40c2c7 100644 --- a/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java +++ b/src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java @@ -3,16 +3,24 @@ package com.genersoft.iot.vmp.vmanager.user; import com.genersoft.iot.vmp.conf.security.SecurityUtils; import com.genersoft.iot.vmp.conf.security.dto.LoginUser; import com.genersoft.iot.vmp.service.IUserService; +import com.genersoft.iot.vmp.storager.dao.dto.User; +import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.*; import javax.security.sasl.AuthenticationException; +import javax.xml.crypto.Data; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; @Api(tags = "用户管理") @CrossOrigin @@ -26,13 +34,15 @@ public class UserController { @Autowired private IUserService userService; + private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + @ApiOperation("登录") @ApiImplicitParams({ - @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", value = "密码(32位md5加密)", dataTypeClass = String.class), + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class), + @ApiImplicitParam(name = "password", required = true, value = "密码(32位md5加密)", dataTypeClass = String.class), }) @GetMapping("/login") - public String login(String username, String password){ + public String login(@RequestParam String username, @RequestParam String password){ LoginUser user; try { user = SecurityUtils.login(username, password, authenticationManager); @@ -49,17 +59,17 @@ public class UserController { @ApiOperation("修改密码") @ApiImplicitParams({ - @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class), - @ApiImplicitParam(name = "oldpassword", value = "旧密码(已md5加密的密码)", dataTypeClass = String.class), - @ApiImplicitParam(name = "password", value = "新密码(未md5加密的密码)", dataTypeClass = String.class), + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class), + @ApiImplicitParam(name = "oldpassword", required = true, value = "旧密码(已md5加密的密码)", dataTypeClass = String.class), + @ApiImplicitParam(name = "password", required = true, value = "新密码(未md5加密的密码)", dataTypeClass = String.class), }) @PostMapping("/changePassword") - public String changePassword(String oldpassword, String password){ + public String changePassword(@RequestParam String oldPassword, @RequestParam String password){ // 获取当前登录用户id String username = SecurityUtils.getUserInfo().getUsername(); LoginUser user = null; try { - user = SecurityUtils.login(username, oldpassword, authenticationManager); + user = SecurityUtils.login(username, oldPassword, authenticationManager); if (user != null) { int userId = SecurityUtils.getUserId(); boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes())); @@ -72,4 +82,67 @@ public class UserController { } return "fail"; } + + + @ApiOperation("添加用户") + @ApiImplicitParams({ + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class), + @ApiImplicitParam(name = "password", required = true, value = "密码(未md5加密的密码)", dataTypeClass = String.class), + @ApiImplicitParam(name = "roleId", required = true, value = "角色ID", dataTypeClass = String.class), + }) + @PostMapping("/add") + public ResponseEntity> add(@RequestParam String username, + @RequestParam String password, + @RequestParam int roleId){ + // 获取当前登录用户id + int currenRoleId = SecurityUtils.getUserInfo().getRoleId(); + if (currenRoleId != 0) { + // 只用角色id为0才可以删除和添加用户 + return new ResponseEntity<>(null, HttpStatus.FORBIDDEN); + } + User user = new User(); + user.setUsername(username); + user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes())); + user.setRoleId(roleId); + user.setCreateTime(format.format(System.currentTimeMillis())); + user.setUpdateTime(format.format(System.currentTimeMillis())); + int addResult = userService.addUser(user); + WVPResult result = new WVPResult<>(); + result.setCode(addResult > 0 ? 0 : -1); + result.setMsg(addResult > 0 ? "success" : "fail"); + result.setData(addResult); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @ApiOperation("删除用户") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", required = true, value = "用户Id", dataTypeClass = Integer.class), + }) + @DeleteMapping("/delete") + public ResponseEntity> delete(@RequestParam Integer id){ + // 获取当前登录用户id + int currenRoleId = SecurityUtils.getUserInfo().getRoleId(); + if (currenRoleId != 0) { + // 只用角色id为0才可以删除和添加用户 + return new ResponseEntity<>(null, HttpStatus.FORBIDDEN); + } + int deleteResult = userService.deleteUser(id); + WVPResult result = new WVPResult<>(); + result.setCode(deleteResult>0? 0 : -1); + result.setMsg(deleteResult>0? "success" : "fail"); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @ApiOperation("查询用户") + @ApiImplicitParams({}) + @GetMapping("/all") + public ResponseEntity>> all(){ + // 获取当前登录用户id + List allUsers = userService.getAllUsers(); + WVPResult> result = new WVPResult<>(); + result.setCode(0); + result.setMsg("success"); + result.setData(allUsers); + return new ResponseEntity<>(result, HttpStatus.OK); + } } diff --git a/src/main/resources/wvp.sqlite b/src/main/resources/wvp.sqlite index 4dd9860c..a76d7ee8 100644 Binary files a/src/main/resources/wvp.sqlite and b/src/main/resources/wvp.sqlite differ