我想从angularJS客户端上传文件。我启用了CSRF保护,它工作正常,除非我尝试上传文件,我收到403
错误:
无效的CSRF令牌' null'在请求参数' _csrf'上找到 或标题' X-XSRF-TOKEN'。
但是令牌位于请求标头中并且是正确的!当我禁用CSRF保护时,我可以毫无问题地上传文件。
除此之外,CSRF保护工作正常。
这是我目前的配置:
SecurityConfiguration.java
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private SecurityUserDetailsService securityUserDetailsService;
@Autowired
private AuthFailureHandler authFailureHandler;
@Autowired
private AjaxAuthSuccessHandler ajaxAuthSuccessHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(authFailureHandler)
.and()
.authorizeRequests()
.antMatchers(
"/",
"/index.html",
"/styles/**",
"/bower_components/**",
"/scripts/**"
).permitAll().anyRequest()
.authenticated()
.and().formLogin().loginPage("/login").permitAll()
.and().logout().logoutUrl("/logout").logoutSuccessHandler(ajaxAuthSuccessHandler).permitAll()
.and().httpBasic()
.and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.csrf().csrfTokenRepository(csrfTokenRepository());
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(securityUserDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
}
CsrfHeaderFilter.java
public class CsrfHeaderFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
}
最后我添加了一个指向here的SecurityWebApplicationInitializer。
SecurityWebApplicationInitializer.java
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
insertFilters(servletContext, new MultipartFilter());
}
}
我做错了什么?
更新:我添加了此配置,但我仍然获得了403。
@Configuration
public class MultipartUploadConfig {
@Bean(name = "filterMultipartResolver")
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
}
答案 0 :(得分:1)
如果您可以将CSRF令牌作为url参数发送,则可以快速解决问题。在你的html模板中。
<form .... th:action="@{/upload(${_csrf.parameterName}=${_csrf.token})}">
...
</form>
答案 1 :(得分:0)
如果您使用Ajax,请将token参数放在url中,以表单操作中的操作:
url: "/uploadFile?${_csrf.parameterName}=${_csrf.token}"
它变成这样:
function uploadFile() {
$.ajax({
url: "/uploadFile?${_csrf.parameterName}=${_csrf.token}",
type: "POST",
data: new FormData($("#upload-file-form")[0]),
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
success: function () {
// Handle upload success
$("#upload-file-message").text("File succesfully uploaded");
},
error: function () {
// Handle upload error
$("#upload-file-message").text(
"File not uploaded (perhaps it's too much big)");
}
});
}
答案 2 :(得分:0)
您可以执行以下操作在每次Ajax调用中提供令牌:
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
还有另一篇有关此的StackOverflow帖子,是我在这里找到的,但是距我看到已经有一段时间了。