【问题标题】:HTTP 403 error while upload - Invalid CSRF Token 'null'上传时出现 HTTP 403 错误 - 无效的 CSRF 令牌“空”
【发布时间】:2015-07-06 22:13:45
【问题描述】:

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

上传表单.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>

我的控制器方法是

@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 状态 403 - 在请求参数“_csrf”或标头“X-CSRF-TOKEN”上发现无效的 CSRF 令牌“null”

我也使用过弹簧安全性。但我总是给出同样的错误。我尝试了很多但无法解决它。请您帮忙解决这个问题。

【问题讨论】:

标签: jsp spring-mvc spring-security apache-commons-fileupload


【解决方案1】:

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

根据spring.io

什么时候应该使用 CSRF 保护?我们的建议是使用 CSRF 保护可以由浏览器处理的任何请求 普通用户。如果您只创建一个由 非浏览器客户端,您可能希望禁用 CSRF 保护。

所以要禁用它:

@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 令牌:

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

【讨论】:

  • 感谢f的回复,不过我是通过xml自己配置的。
  • 在 spring security 4 上,添加到表单操作 URL 有效,但添加为隐藏字段无效。
  • 这有点晚了,但如果其他人遇到与 digz 相同的问题,请确保您的主上下文 xml 文件中有您的 filterMultipartResolver bean,而不是 servlet xml。
  • 如果不是 jsp 并从 ionic 等移动混合框架发送请求,我们如何生成 csrf 令牌
猜你喜欢
  • 2017-07-15
  • 2015-11-05
  • 2018-05-12
  • 2017-07-29
  • 2017-04-12
  • 1970-01-01
  • 2017-11-02
  • 2015-09-03
  • 2013-04-25
相关资源
最近更新 更多