Yes, if you just want to block access to direct pages, this is probably the best way to get around using something like a custom security package. Otherwise, if you just want to make sure the pages are displayed correctly. In fact, you can simply change the display of your faces servlet to .xhtml, which means that your source will not be displayed when people access pages.
<servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.faces</url-pattern> </servlet-mapping>
If you want to make more complex rewrite rules to actually block pages, you might want to consider using a custom rewrite processor and implementing a processor interface.
http://ocpsoft.com/docs/prettyfaces/3.3.0/en-US/html_single/#inbound_rewriting.options
User processors have access to HttpServletRequest and HttpServletResponse and trigger both inbound and outbound rewrites: you can do more complex things using this interface:
public interface Processor { String processInbound(HttpServletRequest request, HttpServletResponse response, RewriteRule rule, String url); String processOutbound(HttpServletRequest request, HttpServletResponse response, RewriteRule rule, String url); }
Otherwise, what you do will work, and until OCPSoft Rewrite https://github.com/ocpsoft/rewrite (who is also behind PrettyFaces) is released, in which If you could do it pretty easy with a simple inbound rewrite rule:
package com.example; public class ExampleConfigurationProvider extends HttpConfigurationProvider { @Override public int priority() { return 10; } @Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .defineRule() .when(Direction.isInbound().and(DispatchType.isRequest()).and(Path.matches(".*\\.xhtml")).andNot(Path.matches(".*javax.faces.resource.*"))) .perform(SendStatus.code(404)); } }
This rewrite rule will block access to incoming HTTP requests in .XHTML files, while allowing forwarding, as well as errors or asynchronous requests. It will also leave the JSF2 Resource API in a functional state, which is not the case if you use the Java EE security constraint, as suggested in another answer.
Hope this helps, Lincoln