【发布时间】:2016-09-07 15:54:46
【问题描述】:
我尝试通过 Spring Form 在我的 Spring MVC 4 项目中上传文件,但是当我用我的日志提交表单时说:Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@527de1e2 和 Invalid CSRF token found。
我从Spring Security Reference 找到了解决方案 ,但在 Spring 安全性之前放置 Multipart Filter 后,我的模型属性返回 NULL。
这是我的代码:
JSP 片段:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<body>
<div>
<form:form action="save?${_csrf.parameterName}=${_csrf.token}"
method="post" modelAttribute="book" enctype="multipart/form-data">
<table>
<form:input type="hidden" path="id" />
<tr>
<td>ISBN:</td>
<td><form:input path="isbn" autofocus="autofocus"/></td>
</tr>
<tr>
<td>Title:</td>
<td><form:input path="title" /></td>
</tr>
<tr>
<td>Author:</td>
<td><form:input path="author" /></td>
</tr>
<tr>
<td>Publisher:</td>
<td><form:input path="publisher" /></td>
</tr>
<tr>
<td>Call Number:</td>
<td><form:input path="callNumber" /></td>
</tr>
<tr>
<td>Pages:</td>
<td><form:input path="pages" /></td>
</tr>
<tr>
<td>Pages:</td>
<td><form:input path="imageFile" type="file" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save">
</td>
</tr>
</table>
</form:form>
</div>
型号
@Entity
@Table(name="books")
public class Book implements Serializable {
private static final long serialVersionUID = 4235334951865878125L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotNull
@NotEmpty
@Size(min = 1, max = 25)
@Column(name = "isbn")
private String isbn;
@NotNull
@NotEmpty
@Size(min = 1, max = 50)
@Column(name = "title")
private String title;
@Size(max = 50)
@Column(name = "author")
private String author;
@Size(max = 50)
@Column(name = "publisher")
private String publisher;
@Size(max = 25)
@Column(name = "call_number")
private String callNumber;
@Column(name = "pages")
private int pages;
@Column(name = "image_file")
private byte[] imageFile;
//Setter & Getter
}
控制器片段:
@RequestMapping(value = "/save", method = RequestMethod.POST)
@PreAuthorize("hasAnyAuthority('BOOK_ADD', 'BOOK_EDIT')")
public String saveBook(@ModelAttribute @Valid Book book, BindingResult result) {
bookValidator.validate(book, result);
if (result.hasErrors()) {
return "book/form";
}
if (bookService.getBook(book.getId()) == null) {
bookService.save(book);
} else {
bookService.update(book);
}
return "redirect:/book";
}
Servlet 配置:
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
return resolver;
}
Spring 安全初始化器:
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
insertFilters(servletContext, new MultipartFilter());
}
SQL 表:
CREATE TABLE `books` (
`id` INT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
`isbn` VARCHAR(25) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`author` VARCHAR(50) NULL DEFAULT NULL,
`publisher` VARCHAR(50) NULL DEFAULT NULL,
`call_number` VARCHAR(25) NULL DEFAULT NULL,
`pages` INT(5) NULL DEFAULT NULL,
`image_file` MEDIUMBLOB NULL,
PRIMARY KEY (`id`)
)
和我正在使用的库片段:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency.
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
【问题讨论】:
标签: java spring hibernate spring-mvc spring-security