如何禁用所有未在 pretty-config.xml 中映射的 URI

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 应用程序时,应用程序应该 return 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(".*");
    }
}