[英]How to disable all URIs which are not mapped in pretty-config.xml
我希望每當用戶在地址欄中輸入uri時,我的pretty-config.xml文件中沒有映射到404錯誤。 我的漂亮配置看起來像:
<?xml version="1.0" encoding="UTF-8"?>
<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces>
<url-mapping id="landing">
<pattern value="/" />
<view-id value="/faces/index.xhtml" />
</url-mapping>
<url-mapping id="login">
<pattern value="/login" />
<view-id value="/faces/login.xhtml" />
</url-mapping>
</pretty-config>
例如,當用戶鍵入myapp.com/faces/login.xhtml時,應該返回404錯誤。 怎么做?
我建議使用Rewrite( https://www.ocpsoft.org/rewrite )。 它已經包含在您的PrettyFaces項目中:
package com.example;
@RewriteConfiguration
public class ExampleConfigurationProvider extends HttpConfigurationProvider
{
@Override
public int priority()
{
return 10000000; // Very large priority # should occur last.
}
@Override
public Configuration getConfiguration(final ServletContext context)
{
return ConfigurationBuilder.begin()
.addRule()
.when(
// filter inbound requests only
Direction.isInbound()
// match all paths
.and(Path.matches("/{p}"))
// only catch requests if they were not already internally forwarded by another rule
.and(Not.any(DispatchType.isForward()))
)
// Show the 404 page.
.perform(Forward.to("/404"))
// Allow "p" to match any URL path
.where("p").matches(".*");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.