Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止直接访问JSF2中的xhtml文件_Java_Jsf_Jsf 2 - Fatal编程技术网

Java 防止直接访问JSF2中的xhtml文件

Java 防止直接访问JSF2中的xhtml文件,java,jsf,jsf-2,Java,Jsf,Jsf 2,我想阻止直接访问项目中的*.xhtml文件。在页面中,有commandLinks调用某些bean的某些方法。这些bean以字符串形式返回视图的名称 return "campaign.xhtml?faces-redirect=true"; 如果用户向浏览器的地址栏写入以下内容,我不希望用户看到xhtml文件 http://localhost:8080/myApp/faces/campaign.xhtml http://localhost:8080/myApp/faces/campaign.xh

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

return "campaign.xhtml?faces-redirect=true";
如果用户向浏览器的地址栏写入以下内容,我不希望用户看到xhtml文件

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

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

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

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

谢谢

法鲁克·库坎

用户看到这些视图时没有填写信息

如果信息已填充或未填充,只需签入
preRenderView
事件侦听器即可。如果没有,请重定向回


如有必要,您可以将其与if结合使用,如果您实际上也在使用带有验证的
。例如



更新:在JSF2.2中,您可以使用
进行更新

<f:viewAction listener="#{bean.check}" />
用户看到这些视图时没有填写信息

如果信息已填充或未填充,只需签入
preRenderView
事件侦听器即可。如果没有,请重定向回


如有必要,您可以将其与if结合使用,如果您实际上也在使用带有验证的
。例如



更新:在JSF2.2中,您可以使用
进行更新

<f:viewAction listener="#{bean.check}" />

在您的情况下,您需要将一些模式映射到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>
通过这种方式,您将能够通过commandLinks将用户重定向到您想要的页面,但是当用户键入


对URL的访问将被拒绝

在您的情况下,您需要将一些模式映射到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>
通过这种方式,您将能够通过commandLinks将用户重定向到您想要的页面,但是当用户键入


对URL的访问将被拒绝

我强烈认为这不是问题所在。否则巴卢斯克会把它标为复制品,而不会给出他所做的答案。我强烈认为这不是问题所在。否则BalusC会将其标记为副本,而不会给出他所做的回答。
<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>
return "campaign.someExtension?faces-redirect=true";
http://localhost:8080/myApp/faces/campaign.xhtml
http://localhost:8080/myApp/faces/campaign.xhtml?faces-redirect=true