# IOT **Repository Path**: a_bad_fox/IOT ## Basic Information - **Project Name**: IOT - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 2 - **Created**: 2020-08-02 - **Last Updated**: 2021-01-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 开发注意事项 ## 开发规范 1. 系统为前后端分离,全部对外开放的网址都是以`/api/v3`开头,权限系统对非`/api/v3`开头的链接全部放行 示例`Controller` ```java @RestController @Api(tags = "用户管理") @LogTag("用户管理") @RequestMapping(value = Constants.BASE_API_PATH + "/user") public class UserController { } ``` 2. 系统对错误异常进行统一处理,可以不需要采用过多的异常处理代码 3. 每次返回数据必须是指定格式 返回代码如下 ```java @Autowired TokenUtils tokenUtils; @GetMapping("/profile") @PreAuthorize("isAuthenticated()") @ApiOperation("获取当前用户的全部个人信息") @LogOperation(operationName = "获取当前用户的全部个人信息", operationType = OperationType.SELECT) public ResponseEntity getMyProfile(HttpServletRequest request, @ApiIgnore @CurrentUser User user){ UserDetail userDetail = userService.getMyUserDetail(user); ResultMap resultMap = new ResultMap(tokenUtils).successAndRefreshToken(request).payload(userDetail); return ResponseEntity.ok(resultMap); } ``` 4. 后端文档接口地址为`http://host:{port}/doc.html` ## 权限相关 权限采用注解的方式校验 只有超级管理员可以进行用户管理和产品管理,剩余可以控制的权限如下 * 设备管理:设备列表:添加设备 * 设备管理:设备列表:删除设备 * 设备管理:设备列表:编辑设备信息 * 设备管理:设备列表:编辑设备影子 * 设备管理:设备列表:添加子设备 * 设备管理:设备列表:解除子设备 * 设备管理:设备列表:批量启用 * 设备管理:设备列表:批量禁用 * 设备管理:设备地图:查询及展示 * 设备管理:分组设置:添加项目 * 设备管理:分组设置:编辑项目 * 设备管理:分组设置:添加下级分组 * 设备管理:分组设置:删除 * 设备管理:分组设置:移动 * 设备管理:分组设置:查询 * 报警监控:报警通知:删除报警 * 报警监控:报警通知:忽略报警 * 报警监控:报警规则:新增报警规则 * 报警监控:报警规则:编辑规则 * 报警监控:报警规则:删除设备报警规则 * 报警监控:报警联系人:新增报警联系人 * 报警监控:编辑报警联系人:删除报警联系人 * 数据服务:服务端订阅:创建订阅 * 数据服务:服务端订阅:查看订阅 * 数据服务:服务端订阅:编辑订阅 * 数据服务:服务端订阅:删除订阅 * 数据服务:服务端订阅:创建消费组 * 数据服务:服务端订阅:删除消费组 * 数据服务:服务端订阅:添加客户端 * 数据服务:服务端订阅:编辑客户端 * 数据服务:服务端订阅:删除客户端 * 数据服务:历史采集:查询历史采集数据 * 数据服务:历史采集:下载数据 * 数据服务:设备上下线:查询上下线数据 * 数据服务:设备上下线:下载数据 ### 权限校验 权限采用注解方式进行校验 示例如下 ```java @PutMapping("/{id}") @PreAuthorize("hasPermission('','超级管理员')") // 权限校验 public ResponseEntity updateUser(@PathVariable("id") Long id, @Valid @RequestBody UpdateUserDto updateUserDto, @ApiIgnore BindingResult bindingResult, HttpServletRequest request){ } ``` 超级管理员权限 `@PreAuthorize("hasPermission('','超级管理员')")` 其余权限`@PreAuthorize("hasPermission('','数据服务:历史采集:查询历史采集数据')")` ## 如何获取当前用户 当前用户可以直接通过参数注入,需要再Controller添加指定注解`@CurrentUser`即可 示例 ```java @GetMapping("/profile") public ResponseEntity getMyProfile(HttpServletRequest request, @ApiIgnore @CurrentUser User user // 系统自动注入 ){ UserDetail userDetail = userService.getMyUserDetail(user); ResultMap resultMap = new ResultMap(tokenUtils).successAndRefreshToken(request).payload(userDetail); return ResponseEntity.ok(resultMap); } ``` ## 日志系统 日志相关有两个注解`@LogTag`和`@LogOperation` ```java @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface LogOperation { /** * 操作名 */ String operationName() default "unknown"; /** * 是否忽略结果 */ boolean ignoreOutput() default true; /** * 敏感参数 * 日志会记录操作参数。 如果有敏感参数(例如密码等),请忽略该参数 */ String[] sensitiveParams() default {}; /** * 操作类型 */ OperationType operationType(); } ``` 在每一个`Controller`类中标识`@LogTag`,在每一个`Mapping`方法中标注`@LogOperation`,系统会自动记录日志。 示例 ```java @RestController @Api(tags = "角色管理") @RequestMapping(Constants.BASE_API_PATH ) @LogTag("角色管理") public class RoleController { @GetMapping("/all_role") @PreAuthorize("hasPermission('','超级管理员')") @ApiOperation(value = "查询全部角色", response =ResponseEntity.class) @LogOperation(operationName = "查询全部角色", operationType = OperationType.SELECT) public ResponseEntity getAllRole(HttpServletRequest request){ } @PostMapping("/role") @PreAuthorize("hasPermission('','超级管理员')") @ApiOperation(value = "新建角色", response = ResponseEntity.class) @LogOperation(operationName = "新建角色", operationType = OperationType.INSERT) public ResponseEntity addRole(@CurrentUser @ApiIgnore User user, @RequestBody @Valid CreateRoleDto createRoleDto, @ApiIgnore BindingResult bindingResult, HttpServletRequest request){ } @DeleteMapping("/role/{id}") @PreAuthorize("hasPermission('','超级管理员')") @ApiOperation(value = "删除角色", response = ResponseEntity.class) @LogOperation(operationName = "删除角色", operationType = OperationType.DELETE) public ResponseEntity deleteRole(@CurrentUser @ApiIgnore User user, @PathVariable Long id, HttpServletRequest request){ } @PutMapping("/role/{id}") @PreAuthorize("hasPermission('','超级管理员')") @ApiOperation(value = "更新角色", response = ResponseEntity.class) @LogOperation(operationName = "更新角色", operationType = OperationType.UPDATE) public ResponseEntity deleteRole(@CurrentUser @ApiIgnore User user, @Valid @RequestBody UpdateRoleDto updateRoleDto, @ApiIgnore BindingResult bindingResult, @PathVariable Long id, HttpServletRequest request){ } } ``` ## 如何进行接口测试 1. 进入API文档 ![image-20200802145522116](https://ning-wang.oss-cn-beijing.aliyuncs.com/blog-imags/image-20200802145522116.png) 2. 生成用户`Token` 输入用户名,返回指定Token。 系统管理员用户名为`admin` ![image-20200802145620618](https://ning-wang.oss-cn-beijing.aliyuncs.com/blog-imags/image-20200802145620618.png) 3. 复制获取到的token,并指定Token测试 ![image-20200802145801205](https://ning-wang.oss-cn-beijing.aliyuncs.com/blog-imags/image-20200802145801205.png) ![image-20200802145824060](https://ning-wang.oss-cn-beijing.aliyuncs.com/blog-imags/image-20200802145824060.png)