如何避免用户访问JSF中的.xhtml页面?

发布于 2024-11-02 01:53:16 字数 134 浏览 1 评论 0原文

我是 JSF 新手,首先编写简单的 jsf Web 应用程序。

带有 .jsf 的 URL 映射到 WebContent 中的 .xhtml 文件,但为什么我可以在 Web 浏览器中使用所有 jsf 标签打开 .xhtml。如何保护这个?

I am new to JSF and writing first simply jsf web app.

URL with .jsf are mapping to .xhtml files in WebContent but why I can open .xhtml in web browser with all jsf tags. How to protect this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小兔几 2024-11-09 01:53:16

您可以向 web.xml 添加安全约束,阻止对 *.xhtml 的所有请求。

<security-constraint>
    <display-name>Restrict raw XHTML Documents</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>

You could add a security constraint to your web.xml blocking all requests to *.xhtml.

<security-constraint>
    <display-name>Restrict raw XHTML Documents</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>
南渊 2024-11-09 01:53:16

除了定义 来阻止直接访问 .xhtml 文件(正如 Stacker 在此问题上正确回答的那样)之外,您还可以更改 FacesServlet*.jsf 映射到 *.xhtml

<servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

在 JSF 1.x 中,这通常会导致无限循环,但在 JSF 2.x 中不再如此。因此,您可以将所有页面调用/链接为 .xhtml ,而无需摆弄不同的扩展名。唯一的缺点是,如果不调用 FacesServlet,您将无法显示“纯”XHTML 文件,但无论如何,这样的页面应该命名为 .html :)

Apart from defining a <security-constraint> to block direct access to .xhtml files as correctly answered by Stacker on this question, you could also just change the <url-pattern> of the FacesServlet mapping from *.jsf to *.xhtml.

<servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

In JSF 1.x this used to end up in an infinite loop, but in JSF 2.x not anymore. So you could just call/link all pages as .xhtml without fiddling with different extensions. The only disadvantage is that you won't be able to display a "plain" XHTML file without invoking the FacesServlet, but such a page should be named .html anyway :)

反差帅 2024-11-09 01:53:16

在 GAE 上,您需要两件事:

  1. 如上所述编辑 web.xml
  2. 添加 appengine-web.xml
<static-files>
    <exclude path="/**.xhtml" />
</static-files>`

On GAE you need two things:

  1. edit web.xml as described above
  2. add in appengine-web.xml
<static-files>
    <exclude path="/**.xhtml" />
</static-files>`
倒数 2024-11-09 01:53:16

您可以使用 servlet 过滤器

@WebFilter(filterName = "XhtmlFilter", urlPatterns = { "*.xhtml" })
public class XhtmlFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        ((HttpServletResponse) response).sendError(404);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}

You can use a servlet filter

@WebFilter(filterName = "XhtmlFilter", urlPatterns = { "*.xhtml" })
public class XhtmlFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        ((HttpServletResponse) response).sendError(404);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
泛泛之交 2024-11-09 01:53:16

据我的经验,mk761203的答案在为谷歌应用程序引擎和服务器面孔设置项目时绝对有帮助。如果不排除这些文件,GAE 会自动将扩展名为 .xhtml 的文件解释为静态文件,这些文件由 google 服务器场的专用服务器提供服务。请在此处阅读更多信息:https://developers.google.com/appengine/docs /java/config/appconfig#Static_Files_and_Resource_Files

as far as i experienced it, the answer of mk761203 is definitely helpful when setting up a project for google app engine and server faces. without the exclusion of this files, the GAE automatically interpets the files with the .xhtml extension as static files which get served by dedicated servers from googles server farm. read more here: https://developers.google.com/appengine/docs/java/config/appconfig#Static_Files_and_Resource_Files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文