【问题标题】:Prevent direct access to xhtml files in jsf 2防止在 jsf 2 中直接访问 xhtml 文件
【发布时间】:2012-10-20 23:12:12
【问题描述】:

我想阻止直接访问我的项目中的 *.xhtml 文件。在页面中,有调用某些 bean 的某些方法的 commandLinks。这些 bean 将视图名称作为字符串返回。

return "campaign.xhtml?faces-redirect=true";

如果用户在浏览器的地址栏写入以下内容,我不希望用户看到 xhtml 文件。

http://localhost:8080/myApp/faces/campaign.xhtml

http://localhost:8080/myApp/faces/campaign.xhtml?faces-redirect=true

因为,在某些 bean 中,我填充了这些 xhtml 视图。但是,如果用户直接访问 xhtml 文件,用户会看到这些视图没有填充信息。

当我在 web.xml 文件中使用时,访问被拒绝。但是,在这种情况下,当 bean 返回值“campaign.xhtml?faces-redirect=true”时,它也不能显示视图。 bean 的访问也被拒绝。

我能做些什么来防止这种情况发生?

谢谢。

法鲁克·库斯坎

【问题讨论】:

    标签: java jsf jsf-2


    【解决方案1】:

    用户在没有填写信息的情况下看到这些视图。

    只需检查preRenderView 事件侦听器是否填写了信息。如果没有,请重定向回来。

    <f:event type="preRenderView" listener="#{bean.init}" />
    

    public void init() throws IOException {
        if (information == null) {
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            externalContext.redirect(externalContext.getRequestContextPath() + "/otherpage.xhtml");
        }
    }
    

    如果您实际上还使用 &lt;f:viewParam&gt; 进行验证,则可以在必要时将其与 FacesContext#isValidationFailed() 结合使用。例如

    <f:viewParam name="id" value="#{bean.information}" required="true" />
    <f:event type="preRenderView" listener="#{bean.init}" />
    

    public void init() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context.isValidationFailed()) {
            ExternalContext externalContext = context.getExternalContext();
            externalContext.redirect(externalContext.getRequestContextPath() + "/otherpage.xhtml");
        }
    }
    

    更新:在 JSF 2.2 中,您可以为此使用 &lt;f:viewAction&gt;

    <f:viewAction listener="#{bean.check}" />
    
    public String check() {
        if (information == null) {
            return "otherpage?faces-redirect=true";
        } else {
            return null;
        }
    }
    

    【讨论】:

      【解决方案2】:

      在您的情况下,您需要将一些模式映射到您的 xhtml 文件,以便通过该模式从 URL 访问它们,同时对 .xhtml 扩展名的访问将受到限制。所以在你的 web.xml 中:

      <servlet>
         <servlet-name>Faces Servlet</servlet-name>
         <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>        
         <load-on-startup>1</load-on-startup>
       </servlet>
      
          <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.someExtension</url-pattern>
          </servlet-mapping>
      
        <security-constraint>  
          <display-name>Restrict access to 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>
      

      这是你的 bean 应该返回的:

      return "campaign.someExtension?faces-redirect=true";
      

      通过这种方式,您将能够通过 commandLinks 将用户重定向到您希望的页面,但当用户键入时

      http://localhost:8080/myApp/faces/campaign.xhtml
      

      http://localhost:8080/myApp/faces/campaign.xhtml?faces-redirect=true
      

      对 URL 的访问将被拒绝。

      【讨论】:

      • 我强烈认为这不是问题所在。否则 BalusC 会将其标记为重复,而不会给出他所做的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2016-10-09
      • 2019-09-20
      • 2012-05-01
      相关资源
      最近更新 更多