java 下载图片403_java – 上传文件返回403错误 – Spring MVC

@H_403_0@

在我的Spring MVC项目中,我试图通过一个简单的表单上传文件.

HTML表格:

我的控制器:

@Controller

public class FileController {

@RequestMapping(value="/upload",method=RequestMethod.POST)

public @ResponseBody String handleFileUpload(

@RequestParam("name") String name,@RequestParam("file") MultipartFile file){

if (!file.isEmpty()) {

try {

//do stuff

} catch (Exception e) {

return "You Failed to upload " + name + " => " + e.getMessage();

}

} else {

return "You Failed to upload " + name + " because the file was empty.";

}

}

}

安全配置:

@Configuration

@EnableWebMvcSecurity

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/upload").permitAll()

.and()

.exceptionHandling().accessDeniedPage("/403")

}

}

但是我收到了403:Forbidden错误,并且每次都被重定向到我的403.html视图

到目前为止,我已经尝试在Spring Security过滤器在一个单独的类中初始化之前指定MultipartFilter,但没有运气

public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

@Override

protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {

insertFilters(servletContext,new MultipartFilter());

}

}

有任何想法吗?

更新:包括我的WebAppInitializer

@Configuration

@Import({ WebSecurityConfig.class })

public class WebAppInitializer implements WebApplicationInitializer {

@Override

public void onStartup(ServletContext servletContext) throws ServletException {

System.out.println(":::Starting My App:::");

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

context.register(WebMVCConfig.class);

context.setServletContext(servletContext);

context.setConfigLocation("com.myApp.configuration");

}

}

我有一个servlet请求属性列表,返回以下403错误:

javax.servlet.forward.request_uri

javax.servlet.forward.context_path

javax.servlet.forward.servlet_path

__spring_security_scpf_applied

org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE

SPRING_SECURITY_403_EXCEPTION

org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER

springMacroRequestContext

themes

thymeleafEvaluationContext

org.springframework.security.web.FilterChainProxy.APPLIED

_csrf

org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.FILTERED

org.springframework.security.web.csrf.CsrfFilter@539743f9.FILTERED

beans

springRequestContext

org.springframework.web.servlet.HandlerMapping.introspectTypeLevelMapping

org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER

org.springframework.web.servlet.DispatcherServlet.CONTEXT

org.springframework.core.convert.ConversionService

execInfo

org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping

org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER

org.springframework.web.servlet.resource.ResourceUrlProvider

org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP

org.springframework.web.servlet.HandlerMapping.bestMatchingPattern

org.springframework.security.web.csrf.CsrfToken

org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER

更新#2:这肯定是CSRF问题;当我在WebSecurityConfig中包含以下内容时,我得到403

.csrf().disable()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值