Spring 启动 2.0.0.M4 OAuth2 令牌端点抛出 org.springframework.web.HttpRequestMethodNotSupportedException

Spring Boot 2.0.0.M4 OAuth2 token endpoint throws org.springframework.web.HttpRequestMethodNotSupportedException

我正在尝试从 Spring 引导 1.5.7 迁移到 2.0.0.M4

现在我无法在 Spring Boot 2.0.0.M4

上正确地重新配置我的 OAuth2 + JWT 配置

在启动期间,我注意到日志中 /oauth/token 端点 (throws org.springframework.web.HttpRequestMethodNotSupportedException) 的以下错误:

2017-10-04 09:52:46.841  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/authorize],methods=[POST],params=[user_oauth_approval]}" onto public org.springframework.web.servlet.View org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.approveOrDeny(java.util.Map<java.lang.String, java.lang.String>,java.util.Map<java.lang.String, ?>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)
2017-10-04 09:52:46.842  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/authorize]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)
2017-10-04 09:52:46.843  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/token],methods=[GET]}" onto public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.getAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException
2017-10-04 09:52:46.844  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/token],methods=[POST]}" onto public org.springframework.http.ResponseEntity<org.springframework.security.oauth2.common.OAuth2AccessToken> org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(java.security.Principal,java.util.Map<java.lang.String, java.lang.String>) throws org.springframework.web.HttpRequestMethodNotSupportedException
2017-10-04 09:52:46.845  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/check_token]}" onto public java.util.Map<java.lang.String, ?> org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint.checkToken(java.lang.String)
2017-10-04 09:52:46.845  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/confirm_access]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.WhitelabelApprovalEndpoint.getAccessConfirmation(java.util.Map<java.lang.String, java.lang.Object>,javax.servlet.http.HttpServletRequest) throws java.lang.Exception
2017-10-04 09:52:46.846  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/error]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.WhitelabelErrorEndpoint.handleError(javax.servlet.http.HttpServletRequest)
2017-10-04 09:52:46.848  INFO 11780 --- [           main] .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/oauth/token_key],methods=[GET]}" onto public java.util.Map<java.lang.String, java.lang.String> org.springframework.security.oauth2.provider.endpoint.TokenKeyEndpoint.getKey(java.security.Principal)

为了重现这个问题,我创建了 GitHub 项目 - https://github.com/Artgit/spring-boot-2.0.0.M4-oauth2-token-issue/tree/master/api

我为了产生这个问题请尝试执行com.decisionwanted.domain.api.decision.DecisionControllerIT.testCreateDecision()测试

对于代码中的混乱和 pom.xml,我深表歉意。为了测试目的,我从多模块 Maven 应用程序编译了这个测试项目。

请帮我修复我的配置,以便能够使用 Spring 启动 2.0.0.M4

已更新

在调试时,我发现 application.properties 中的 server.contextPath: /api 没有考虑在内。现在我需要使用 /oauth/token(而不是 /api/oauth/token,就像我在 Spring Boot 1.5.7 中使用的那样)以获得令牌。这可能是什么原因以及如何解决?

我找到了问题的原因 - 根据以下文档 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/ 和 Spring Boot 2* 应使用以下 属性 来定义上下文路径申请:

server.servlet.context-path= /api

请注意 Spring Boot 1.5.* 属性

server.contextPath: /api

没有更多考虑