JSF和PrettyFaces - 如何限制直接的xhtml请求

Ale*_*bon 7 jsf restriction prettyfaces

我是JSF和PrettyFaces的新手.所以到现在为止我发现我可以配置PrettyFaces将请求"转发"到正确的.xhtml文件.问题是,我(或用户,如果他知道我的文件夹结构)也可以请求该文件.这是我的样本:

文件:webbapp/mypage.xhtml

我在pretty-config.xml中添加了以下行:

<url-mapping id="myPageId">
    <pattern value="/prettyurltomypage" />
    <view-id value="/mypage.xhtml" /> 
</url-mapping>
Run Code Online (Sandbox Code Playgroud)

PrettyFaces过滤器配置为拦截"/ ".Faces Front Controller配置为处理所有" .xhtml"请求.当我要求......

http://localhost:8080/myapp/prettyurltomypage
Run Code Online (Sandbox Code Playgroud)

......一切都很好.我的问题是,我也可以要求......

http://localhost:8080/myapp/mypage.xhtml
Run Code Online (Sandbox Code Playgroud)

我如何限制.xhtml请求?我的目标是让jsf/server提供默认的404页面.

我的解决方案(到目前为止)是在pretty-config.xml中定义重写规则:

<rewrite match="/mypage.xhtml" substitute="/prettyurltomypage" redirect="301" />
Run Code Online (Sandbox Code Playgroud)

还有其他(更智能)的方式吗?

BCs*_*gor 6

可以通过将XHTML文件标记为部署描述符中的Web资源来完成.
为此,您可以在web.xml中添加以下内容:

<security-constraint>
    <display-name>Restrict direct access to XHTML files</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML files</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint/>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)

如果您想阅读更多有关安全性约束的内容,请参阅Javalobby 的简短文章.