我最近在我的spring mvc應用程序中實現了一個文件上傳功能,但不幸的是它不能正常工作,因爲它經常被Spring Security阻止。如果我在安全配置中禁用CSRF,它會起作用,所以它導致我相信在那裏有錯誤。上傳文件被Spring Security拒絕
Spring配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/test/**").permitAll()
.antMatchers("/admin/**","/user/secure").hasRole("ADMIN")
.antMatchers("/**").permitAll()
.anyRequest().anonymous()
.and()
.exceptionHandling().accessDeniedPage("/denied")
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/error-login")
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.and()
.rememberMe()
.userDetailsService(userAccessDetails)
.tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(16000)
;
}
文件上傳表單:
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
<h4>Single File</h4>
<form method="POST" th:action="@{/test/uploadFile}" 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>
</body>
</html>
任何援助,將不勝感激你們。
注:我不得不重新創建了一個問題,原來我錯
爲什麼要投票?這個問題是合法的,並且符合所有必要的標準。 – Aeseir 2014-11-10 00:15:54