gpt4 book ai didi

java - Spring Boot 不允许对静态资源进行 POST 请求

转载 作者:行者123 更新时间:2023-12-01 19:52:43 28 4
gpt4 key购买 nike

我有一个使用 Angular 2 前端的 Spring Boot Web 应用程序。当我构建应用程序时,Angular 2 前端全部位于类路径上的 /static/ 目录中,以便 Spring Boot 将其全部作为静态资源提供。

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

这会导致405 - 方法不允许

有什么方法可以通过 Spring Boot 允许对静态资源进行 POST 请求吗?

我什至尝试添加一个 Controller 来接受 POST 请求,并将请求简单地转发到我的应用程序的根目录到正确的 View ,index.html。

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

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

并添加了以下 Controller :

@Controller
public class IndexController {

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

}

但是没有效果。 Spring Boot 仍然在内部解决了我对静态服务资源的请求,该资源不允许 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

最佳答案

我最终添加了 spring-boot-starter-thymeleaf 作为依赖项,然后将 index.html/static/ 移出并放入 /templates/。我从 application.properties 中删除了 spring.mvc.view.prefix 和 spring.mvc.view.suffix 属性,因为 spring boot thymeleaf 会自动查找 .html 模板位于 /templates/ 中。这样做之后,我的 index.html 不再由 spring 作为静态资源,并且我的 IndexController (与原始问题中所示的相同)正确处理我的POST 请求并呈现“index” View ,该 View 正确显示我的 index.html

关于java - Spring Boot 不允许对静态资源进行 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42957619/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com