Spring Boot不允许POST请求静态资源

时间:2017-03-22 16:33:35

标签: java spring spring-mvc spring-boot http-post

我有一个使用Angular 2前端的spring boot Web应用程序。当我构建我的应用程序时,我的Angular 2前端都位于我的类路径的/static/目录中,因此spring boot将它作为静态资源提供。

我的应用程序使用SAML 2.0对IdP进行身份验证,当身份验证后IdP返回我的应用程序时,它会发送POST请求以发布SAML响应。

这导致 405 - 方法不允许

有没有办法允许POST请求通过spring boot启动静态资源?

我甚至尝试添加一个接受POST请求的控制器,只需将请求转发到我的应用程序的根目录到index.html。

我在application.properties中添加了以下属性:

spring.mvc.view.prefix=/static/
spring.mvc.view.suffix=.html

并添加了以下控制器:

@Controller
public class IndexController {

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "index";
    }

}

但无济于事。 Spring启动仍在内部解析我对静态服务资源的请求,该资源不允许POST请求。以下是来自spring的相关调试消息:

2017-03-22 12:20:56.261 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:865 - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/notification-service/]
2017-03-22 12:20:56.261 DEBUG [http-nio-8080-exec-6] o.s.w.s.m.m.a.RequestMappingHandlerMapping:310 - Looking up handler method for path /
2017-03-22 12:20:56.262 DEBUG [http-nio-8080-exec-6] o.s.w.s.m.m.a.RequestMappingHandlerMapping:317 - Returning handler method [public java.lang.String com.papajohns.corporate.notification.controller.IndexController.index()]
2017-03-22 12:20:56.262 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'indexController'
2017-03-22 12:20:56.262 DEBUG [http-nio-8080-exec-6] o.s.o.j.s.OpenEntityManagerInViewInterceptor:85 - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2017-03-22 12:20:56.263 DEBUG [http-nio-8080-exec-6] o.s.web.cors.DefaultCorsProcessor:71 - Skip CORS processing: response already contains "Access-Control-Allow-Origin" header
2017-03-22 12:20:56.268 DEBUG [http-nio-8080-exec-6] o.s.w.s.v.ContentNegotiatingViewResolver:263 - Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2017-03-22 12:20:56.268 DEBUG [http-nio-8080-exec-6] o.s.w.s.view.BeanNameViewResolver:74 - No matching bean found for view name 'index'
2017-03-22 12:20:56.269 DEBUG [http-nio-8080-exec-6] o.s.w.s.view.BeanNameViewResolver:74 - No matching bean found for view name 'index.html'
2017-03-22 12:20:56.269 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:1670 - Invoking afterPropertiesSet() on bean with name 'index'
2017-03-22 12:20:56.269 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-03-22 12:20:56.270 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-03-22 12:20:56.274 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:1670 - Invoking afterPropertiesSet() on bean with name 'index.html'
2017-03-22 12:20:56.274 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-03-22 12:20:56.274 DEBUG [http-nio-8080-exec-6] o.s.b.f.s.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2017-03-22 12:20:56.275 DEBUG [http-nio-8080-exec-6] o.s.w.s.v.ContentNegotiatingViewResolver:338 - Returning [org.springframework.web.servlet.view.InternalResourceView: name 'index'; URL [/static/index.html]] based on requested media type 'text/html'
2017-03-22 12:20:56.276 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:1265 - Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'index'; URL [/static/index.html]] in DispatcherServlet with name 'dispatcherServlet'
2017-03-22 12:20:56.278 DEBUG [http-nio-8080-exec-6] o.s.w.s.view.InternalResourceView:166 - Forwarding to resource [/static/index.html] in InternalResourceView 'index'
2017-03-22 12:20:56.279 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:865 - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/notification-service/static/index.html]
2017-03-22 12:20:56.280 DEBUG [http-nio-8080-exec-6] o.s.w.s.m.m.a.RequestMappingHandlerMapping:310 - Looking up handler method for path /static/index.html
2017-03-22 12:20:56.282 DEBUG [http-nio-8080-exec-6] o.s.w.s.m.m.a.RequestMappingHandlerMapping:320 - Did not find handler method for [/static/index.html]
2017-03-22 12:20:56.282 DEBUG [http-nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping:190 - Matching patterns for request [/static/index.html] are [/**]
2017-03-22 12:20:56.282 DEBUG [http-nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping:219 - URI Template variables for request [/static/index.html] are {}
2017-03-22 12:20:56.282 DEBUG [http-nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping:140 - Mapping [/static/index.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@77392aa5]]] and 1 interceptor
2017-03-22 12:20:56.283 DEBUG [http-nio-8080-exec-6] o.s.web.cors.DefaultCorsProcessor:71 - Skip CORS processing: response already contains "Access-Control-Allow-Origin" header
2017-03-22 12:20:56.292 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:1044 - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-03-22 12:20:56.292 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:1000 - Successfully completed request
2017-03-22 12:20:56.294 DEBUG [http-nio-8080-exec-6] o.s.o.j.s.OpenEntityManagerInViewInterceptor:110 - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2017-03-22 12:20:56.295 DEBUG [http-nio-8080-exec-6] o.s.o.jpa.EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2017-03-22 12:20:56.295 DEBUG [http-nio-8080-exec-6] o.s.web.servlet.DispatcherServlet:1000 - Successfully completed request
2017-03-22 12:20:56.295 DEBUG [http-nio-8080-exec-6] o.s.b.w.f.OrderedRequestContextFilter:104 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@27aca4f4
2017-03-22 12:20:56.295 ERROR [http-nio-8080-exec-6] o.s.boot.web.support.ErrorPageFilter:206 - Cannot forward to error page for request [/] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

1 个答案:

答案 0 :(得分:0)

我最终添加了spring-boot-starter-thymeleaf作为依赖项,然后将index.html移出/static/并进入/templates/。我从application.properties中删除了spring.mvc.view.prefixspring.mvc.view.suffix属性,因为spring boot thymeleaf将自动在.html中查找/templates/个模板。执行此操作后,我的index.html在春天之前不再作为静态资源提供,我的IndexController(原始问题中显示的相同)正确处理我的POST请求并呈现&#34 ;索引"视图,正确显示我的index.html