HTTP Error 403 on Boot - Invalid CSRF Token "null"

this file contains a file upload form

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>

and my controller method

@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.";
        }
    }

I have the following error while loading:

HTTP Status 403 - An invalid CSRF token "null" was found in the request parameter "_csrf" or the header "X-CSRF-TOKEN"

I also used spring protection. But I always give the same error. I tried a lot, but could not solve it. Could you help solve this problem.

+4
source share
1 answer

, CSRF (Cross Site Request Forgery) Spring . .

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">
+4

Source: https://habr.com/ru/post/1585049/


All Articles