@ResponseBody @PutMapping @ApiOperation("修改") @LogMethod( type = 1, voParamIndex = 0 ) public HttpResult<Boolean> update(@RequestBody @Validated({Update.class}) V e) { if (this.isPermitted("update")) { this.beforSave(e, false); this.baseService.updateById((BasePO)e); return HttpResult.success(true); } else { throw new NotPremissionException(); } } public boolean isPermitted(String permitName) { NotCheckPermission notCheckPermission = (NotCheckPermission)this.getClass().getAnnotation(NotCheckPermission.class); if (Objects.nonNull(notCheckPermission)) { return true; } else { String path = super.getRequest().getServletPath(); String namespace = path.split("/")[2]; boolean bool = StpUtil.hasPermission(namespace + ":" + permitName); if (!bool) { LogNamespace annotation = (LogNamespace)this.getClass().getAnnotation(LogNamespace.class); List<String> namespaces = Arrays.asList(annotation.permissions()); Iterator var8 = namespaces.iterator(); while(var8.hasNext()) { String ns = (String)var8.next(); if (StpUtil.hasPermission(ns + ":" + permitName)) { return true; } } } return bool; } }
时间: 2024-02-10 09:34:15 浏览: 93
这段代码是一个Controller的update方法,使用了注解来配置接口的行为。具体注解和对应功能如下:
1. @ResponseBody:用于将返回值转换为JSON格式并返回给客户端。
2. @PutMapping:将HTTP请求映射到更新资源的操作。
3. @ApiOperation:Swagger注解,用于描述接口的功能。
4. @LogMethod:自定义注解,用于记录接口的调用日志。type表示日志类型,voParamIndex表示VO对象在方法参数中的索引位置。
接口的主要逻辑如下:
1. 判断当前用户是否有更新权限,如果没有则抛出NotPremissionException异常。
2. 调用beforSave方法,用于在保存前对VO对象进行处理。
3. 调用baseService.updateById方法,更新数据库中对应的记录。
4. 返回HttpResult.success(true),表示更新成功。
isPermitted方法用于判断当前用户是否有指定权限,具体逻辑如下:
1. 判断当前Controller是否有NotCheckPermission注解,如果有则直接返回true。
2. 获取当前请求的路径,并从中提取出命名空间。
3. 判断当前用户是否有命名空间+权限名称的权限,如果有则返回true。
4. 如果没有,则从LogNamespace注解中获取所有权限名称,并循环判断是否有对应权限,如果有则返回true。
5. 如果以上都没有匹配到,则返回false。
相关问题
<h1>上传/下载文件</h1> <form method="post" action="/file/upload" enctype="multipart/form-data"> <input type="file" name="file" id="fileInput"/> <input type="submit" value="上传" /> </form> <hr> <form method="post" action="/file/download" > <input type="text" name="fileName" placeholder="请输入文件名" /> <input type="submit" value="下载" id="downloadBtn" /> </form> public class SFTPUtil { private static final String host = "10.60.0.160"; private static final int port = 22; private static final String username = "root"; private static final String password = "Z20"; private static Session session = null; public static void connect() throws Exception { } public static void upload(MultipartFile file, String path, String fileName) throws Exception { } public static boolean download(HttpServletResponse response, String path, String fileName) throws Exception { } @Controller @RequestMapping("/file") public class FileController { private static final String UPLOAD_PATH = "/data/sftp/adtuser1/upload/winbidding/"; private static final String DOWNLOAD_PATH = "/data/sftp/adtuser1/upload/winbidding/"; @PostMapping("/upload") @ResponseBody public ModelAndView upload(@RequestParam("file") MultipartFile file) { } @PostMapping("/download") public ModelAndView download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws Exception { } 数据库有host port username password UPLOAD_PATH DOWNLOAD_PATH csmc字段,把上边写死的改成数据库查询,前台页面增加csmc字段下拉框,根据csmc字段把查出来的配置数据完成上传下载功能,用的mysql数据库,把代码改好写出来
首先需要在数据库中创建一个表,用于存储SFTP连接的相关信息:
```sql
CREATE TABLE sftp_config (
id INT PRIMARY KEY AUTO_INCREMENT,
csmc VARCHAR(50),
host VARCHAR(50),
port INT,
username VARCHAR(50),
password VARCHAR(50),
upload_path VARCHAR(255),
download_path VARCHAR(255)
);
```
然后在Spring Boot项目中创建一个SFTPUtil类,用于实现SFTP上传和下载功能:
```java
import com.jcraft.jsch.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Properties;
public class SFTPUtil {
private String host;
private int port;
private String username;
private String password;
private String uploadPath;
private String downloadPath;
private Session session;
public SFTPUtil(String host, int port, String username, String password, String uploadPath, String downloadPath) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.uploadPath = uploadPath;
this.downloadPath = downloadPath;
}
public void connect() throws Exception {
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
public void upload(MultipartFile file, String fileName) throws Exception {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd(uploadPath);
InputStream inputStream = file.getInputStream();
channel.put(inputStream, fileName);
inputStream.close();
channel.disconnect();
}
public boolean download(HttpServletResponse response, String fileName) throws Exception {
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
channel.cd(downloadPath);
SftpATTRS attrs = channel.lstat(fileName);
if (attrs == null) {
channel.disconnect();
return false;
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream outputStream = response.getOutputStream();
channel.get(fileName, outputStream);
outputStream.flush();
outputStream.close();
channel.disconnect();
return true;
}
}
```
然后在Spring Boot项目中创建一个FileController类,用于处理文件上传和下载请求:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/file")
public class FileController {
@Autowired
private JdbcTemplate jdbcTemplate;
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file, @RequestParam("csmc") String csmc) {
try {
// 从数据库中获取SFTP连接配置信息
String sql = "SELECT * FROM sftp_config WHERE csmc = ?";
Map<String, Object> map = jdbcTemplate.queryForMap(sql, csmc);
String host = (String) map.get("host");
int port = (int) map.get("port");
String username = (String) map.get("username");
String password = (String) map.get("password");
String uploadPath = (String) map.get("upload_path");
String fileName = file.getOriginalFilename();
// 使用SFTP上传文件
SFTPUtil sftpUtil = new SFTPUtil(host, port, username, password, uploadPath, null);
sftpUtil.connect();
sftpUtil.upload(file, fileName);
return "上传成功";
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}
@PostMapping("/download")
public void download(@RequestParam("fileName") String fileName, @RequestParam("csmc") String csmc, HttpServletResponse response) throws Exception {
// 从数据库中获取SFTP连接配置信息
String sql = "SELECT * FROM sftp_config WHERE csmc = ?";
Map<String, Object> map = jdbcTemplate.queryForMap(sql, csmc);
String host = (String) map.get("host");
int port = (int) map.get("port");
String username = (String) map.get("username");
String password = (String) map.get("password");
String downloadPath = (String) map.get("download_path");
// 使用SFTP下载文件
SFTPUtil sftpUtil = new SFTPUtil(host, port, username, password, null, downloadPath);
sftpUtil.connect();
boolean result = sftpUtil.download(response, fileName);
if (!result) {
response.getWriter().write("文件不存在");
}
}
@GetMapping("/csmcList")
@ResponseBody
public List<String> getCsmcList() {
// 从数据库中获取所有的csmc字段
String sql = "SELECT DISTINCT csmc FROM sftp_config";
List<String> csmcList = jdbcTemplate.queryForList(sql, String.class);
return csmcList;
}
}
```
最后,在HTML页面中加入下面的代码,即可实现文件上传和下载:
```html
<h1>上传/下载文件</h1>
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="file" id="fileInput"/>
<select name="csmc" id="csmcSelect"></select>
<input type="submit" value="上传"/>
</form>
<hr>
<form method="post" action="/file/download">
<input type="text" name="fileName" placeholder="请输入文件名"/>
<select name="csmc" id="csmcSelect"></select>
<input type="submit" value="下载" id="downloadBtn"/>
</form>
<script>
// 获取csmc列表
$.get("/file/csmcList", function (data) {
var csmcSelect = $("#csmcSelect");
for (var i = 0; i < data.length; i++) {
var option = $("<option>").val(data[i]).text(data[i]);
csmcSelect.append(option);
}
});
</script>
```
@Controller @RequestMapping(value = "/JzAtlas") @Api(tags = {"建筑--图册--操作接口"}) public class JzAtlasAction { @Resource private JzAtlasService service; @ApiOperation(value = "查询单个项目", notes = "项目主键ID") //@RequestMapping(value = "/findByKey", method = {RequestMethod.GET}, produces = "application/json; charset=UTF-8") @RequestMapping(value = "/findByKey", method = {RequestMethod.GET}) @ResponseBody public Object findByKey(Integer id) { return service.findById(id); } @ApiOperation(value = "保存单个项目信息", notes = "传递数据") @RequestMapping(value = "/save", method = {RequestMethod.POST}) @ResponseBody public Object save(@RequestBody JzAtlas model) { return service.save(model); } @ApiOperation(value = "更新单个项目信息", notes = "传递数据") @RequestMapping(value = "/update", method = {RequestMethod.POST}) @ResponseBody public Object update(@RequestBody JzAtlas model) { return service.update(model); } @ApiOperation(value = "查询所有", notes = "无查询条件") @RequestMapping(value = "/findAllList", method = {RequestMethod.POST}) @ResponseBody public Object findList() { return service.findList(); } @ApiOperation(value = "根据条件查询", notes = "查询参数") @RequestMapping(value = "/findList", method = {RequestMethod.POST}) @ResponseBody public Object findListByQuery(@RequestBody JzAtlasRequest requestModel) { return service.findListByQuery(requestModel); } @ApiOperation(value = "删除单个项目信息", notes = "项目主键ID") @RequestMapping(value = "/delete", method = {RequestMethod.POST}) @ResponseBody public Object delete(Integer id) { return service.delete(id); } }
这是一个Java类,使用了Spring框架的@Controller注解,表示这是一个控制器类。通过@RequestMapping注解指定了处理请求的URL路径为"/JzAtlas"。@Api注解用于生成API文档,其中的tags属性指定了文档中的标签为"建筑--图册--操作接口"。
该类中包含了多个处理请求的方法,使用了不同的请求方法(GET、POST)和URL路径。每个方法都使用了@ResponseBody注解,表示返回的结果将被转换为JSON格式。其中,@ApiOperation注解用于生成API文档中的方法描述。
- findByKey方法:用于根据项目主键ID查询单个项目信息。
- save方法:用于保存单个项目信息,接收一个JzAtlas对象作为参数。
- update方法:用于更新单个项目信息,接收一个JzAtlas对象作为参数。
- findList方法:用于查询所有项目信息。
- findListByQuery方法:根据条件查询项目信息,接收一个JzAtlasRequest对象作为参数。
- delete方法:删除单个项目信息,接收一个项目主键ID作为参数。
其中,@Resource注解用于注入JzAtlasService对象,表示该类依赖于JzAtlasService服务类。
阅读全文