gpt4 book ai didi

jsp - 上传时出现 HTTP 403 错误 - 无效的 CSRF token 'null'

转载 作者:行者123 更新时间:2023-12-02 02:39:58 32 4
gpt4 key购买 nike

此文件包含上传文件的表单

uploadForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<sec:csrfMetaTags/>
<title>File Upload</title>
</head>
<body>
<jsp:include page="/resources/layout/header.jsp"/> <!-- Header -->
<div class="container">

<form action="uploadfile" method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="file"><br />
Name: <input type="text" name="name"><br /> <br />
<input type="submit" value="Upload"> Press here to upload the file!
</form>
</div> <!-- Container -->

<jsp:include page="/resources/layout/footer.jsp"/> <!-- Footer -->
</body>
</html>

我的 Controller 方法是

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("name") String name,@RequestParam("file") MultipartFile file) {

if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();

// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
if (!dir.exists())
dir.mkdirs();

// Create the file on server
File serverFile = new File(dir.getAbsolutePath()
+ File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();

logger.info("Server File Location="
+ serverFile.getAbsolutePath());

return "You successfully uploaded file=" + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name
+ " because the file was empty.";
}
}

我在上传时遇到以下错误:

HTTP Status 403 - Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'

我也使用过Spring Security。但我总是给出同样的错误。我尝试了很多但无法解决。您能帮忙解决这个问题吗?

最佳答案

您的 Spring 应用程序中的 CSRF(跨站请求伪造)保护似乎已启用。实际上它是默认启用的。

根据spring.io :

When should you use CSRF protection? Our recommendation is to use CSRF protection for any request that could be processed by a browser by normal users. If you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection.

因此要禁用它:

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

如果您想保持 CSRF 保护启用,那么您必须在表单中包含 csrftoken。你可以这样做:

<form .... >
....other fields here....
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>

您甚至可以在表单的操作中包含 CSRF token :

<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data">

关于jsp - 上传时出现 HTTP 403 错误 - 无效的 CSRF token 'null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913023/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com