Как отключить все URI, которые не отображаются в pretty-config.xml - PullRequest
0 голосов
/ 22 мая 2018

Я хочу, чтобы каждый раз, когда пользователь вводил в адресной строке uri, который не отображается в моем файле pretty-config.xml, чтобы получить ошибку 404.Мой pretty-config выглядит так:

<?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.Как это сделать?

1 Ответ

0 голосов
/ 23 мая 2018

Я бы рекомендовал использовать для этого 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(".*");
    }
}
...