在我的项目中,我想限制JSF Web应用程序中的直接URL访问。尽管我在Web上找到了它,但它建议在web.xml中配置安全性约束。
<security-constraint>
<display-name>Restrict raw XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>/manage/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint />
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
因此,我可以将直接URL访问限制为/manage/*.jsp
。但是我有很多文件夹要限制,例如/view/*.jsp
,/others/*.jsp
等。并且我想在发生错误时显示错误页面。
您现在可能已经想出了解决方案,但想到了要回答。
达到此限制的方法是将所有url模式作为web-resource-collection的一部分。而且,您可以定义另一个没有auth-constraint的安全性约束,以允许直接访问,如下所示。
<security-constraint>
<display-name>Restrict raw XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>/manage/*</url-pattern>
<url-pattern>/view/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint />
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>Allow login pages</display-name>
<web-resource-collection>
<web-resource-name>Seam</web-resource-name>
<url-pattern>*.seam</url-pattern>
...<!-- You can define all the url-patterns you want to allow direct access to -->
</web-resource-collection>
</security-constraint>
您正在使用空的auth-constraint,这意味着无论身份验证如何,它都会阻止对列出的所有URL模式的直接访问。您将收到403错误,并且可以使用错误页面标签为其定义页面
<error-page>
<error-code>403</error-code>
<location>/path/to/error.jsp</location>
</error-page>
否则,您可以使用错误页面标记来定义错误jsps。
<error-page>
<exception-type>anyexception class name</exception-type>
<location>/path/to/error.jsp</location>
</error-page>
一种方法是将jsp文件移动到web-inf目录中,这将阻止直接url访问