0

我正在使用 Spring boot MultipartFile 来允许用户上传他们的文件,并且我想将上传的文件保存在项目目录或我的磁盘“C:\upload”(或它在本地工作的任何地方)中。当我提交任何文件时,我会收到 403 错误禁止。(spring-boot-starter-security 用于嵌入式登录)。

这是在浏览器中打印的错误的屏幕截图:403 Error
在控制台中我收到以下错误:

org.thymeleaf.exceptions.TemplateInputException:解析模板时出错[错误],模板可能不存在或可能无法被任何已配置的模板解析器访问

这是我的项目结构:项目结构
这是我的上传控制器:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class UploadController {

//Save the uploaded file to this folder
private static String UPLOADED_FOLDER = "./src/main/resources/uploaded";

@GetMapping("/test")
public String index() {
return "upload";
}

@PostMapping("/upload") 
public String singleFileUpload(@RequestParam("file") MultipartFile file,
                           RedirectAttributes redirectAttributes) {

if (file.isEmpty()) {
    redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
    return "redirect:uploadStatus";
}

try {

    // Get the file and save it somewhere
    byte[] bytes = file.getBytes();
    Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
    Files.write(path, bytes);

    redirectAttributes.addFlashAttribute("message",
            "You successfully uploaded '" + file.getOriginalFilename() + "'");

} catch (IOException e) {
    e.printStackTrace();
}

return "redirect:/uploadStatus";
}

@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}

}

这是upload.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>

<h1>You can upload your files here</h1>

<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>

</body>
</html>

上传状态.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>

<h1>Upload Status</h1>

<div th:if="${message}">
<h2 th:text="${message}"/>
</div>

</body>
</html>

我使用 thymeleaf 作为模板引擎:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

我仍然是初学者,并且对 spring boot 有基本的经验,我一直在寻找解决方案,所以请任何帮助/建议来解决问题,我们将不胜感激。

提前致谢!

4

2 回答 2

0

这是由于 CSRF(跨站点请求伪造)保护。在表单上添加带有 CSRF 令牌的隐藏字段。

<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
于 2020-12-22T21:54:01.620 回答
0

问题是因为s​​pring boot security(CSRF保护),要解决这个问题,有两种可能:

1-将此行添加到上传表单,工作正常:

<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>

2- 也可以更改安全自动配置并进行我自己的配置以禁用 CSRF。我使用了@Configuration 类,如下所示:

@Configuration
@EnableWebSecurity
public class configuration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.csrf().disable();
}
}

有关 Spring Boot 安全性的详细说明, 请参阅配置 Spring Boot 安全性!
非常感谢@Gustlik

于 2020-12-22T22:34:29.067 回答