-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return 406 status code if welcome page is not accepted
Add `WelcomePageNotAcceptableHandlerMapping` which will return an HTTP 406 status if a suitable welcome page is found but cannot be accepted for the request. An additional mapper is used so that we don't need to change the order of the `WelcomePageHandlerMapping`. It's possible that users may have additional root handler mappings registered to run after the `WelcomePageHandlerMapping` and we still need to respect those. Fixes gh-35559
- Loading branch information
1 parent
e1663e2
commit d7ff379
Showing
8 changed files
with
347 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...nfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WelcomePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.web.servlet; | ||
|
||
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.io.Resource; | ||
|
||
/** | ||
* Details for a welcome page resolved from a resource or a template. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
final class WelcomePage { | ||
|
||
/** | ||
* Value used for an unresolved welcome page. | ||
*/ | ||
static final WelcomePage UNRESOLVED = new WelcomePage(null, false); | ||
|
||
private final String viewName; | ||
|
||
private final boolean templated; | ||
|
||
private WelcomePage(String viewName, boolean templated) { | ||
this.viewName = viewName; | ||
this.templated = templated; | ||
} | ||
|
||
/** | ||
* Return the view name of the welcome page. | ||
* @return the view name | ||
*/ | ||
String getViewName() { | ||
return this.viewName; | ||
} | ||
|
||
/** | ||
* Return if the welcome page is from a template. | ||
* @return if the welcome page is templated | ||
*/ | ||
boolean isTemplated() { | ||
return this.templated; | ||
} | ||
|
||
/** | ||
* Resolve the {@link WelcomePage} to use. | ||
* @param templateAvailabilityProviders the template availability providers | ||
* @param applicationContext the application context | ||
* @param indexHtmlResource the index HTML resource to use or {@code null} | ||
* @param staticPathPattern the static path pattern being used | ||
* @return a resolved {@link WelcomePage} instance or {@link #UNRESOLVED} | ||
*/ | ||
static WelcomePage resolve(TemplateAvailabilityProviders templateAvailabilityProviders, | ||
ApplicationContext applicationContext, Resource indexHtmlResource, String staticPathPattern) { | ||
if (indexHtmlResource != null && "/**".equals(staticPathPattern)) { | ||
return new WelcomePage("forward:index.html", false); | ||
} | ||
if (templateAvailabilityProviders.getProvider("index", applicationContext) != null) { | ||
return new WelcomePage("index", true); | ||
} | ||
return UNRESOLVED; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...pringframework/boot/autoconfigure/web/servlet/WelcomePageNotAcceptableHandlerMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.web.servlet; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.boot.autoconfigure.template.TemplateAvailabilityProviders; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; | ||
import org.springframework.web.servlet.mvc.Controller; | ||
|
||
/** | ||
* An {@link AbstractUrlHandlerMapping} for an application's welcome page that was | ||
* ultimately not accepted. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
class WelcomePageNotAcceptableHandlerMapping extends AbstractUrlHandlerMapping { | ||
|
||
WelcomePageNotAcceptableHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, | ||
ApplicationContext applicationContext, Resource indexHtmlResource, String staticPathPattern) { | ||
setOrder(LOWEST_PRECEDENCE - 10); // Before ResourceHandlerRegistry | ||
WelcomePage welcomePage = WelcomePage.resolve(templateAvailabilityProviders, applicationContext, | ||
indexHtmlResource, staticPathPattern); | ||
if (welcomePage != WelcomePage.UNRESOLVED) { | ||
setRootHandler((Controller) this::handleRequest); | ||
} | ||
} | ||
|
||
private ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) { | ||
response.setStatus(HttpStatus.NOT_ACCEPTABLE.value()); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected Object getHandlerInternal(HttpServletRequest request) throws Exception { | ||
return super.getHandlerInternal(request); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.