How to detect whether user have reached a page by JSF navigation rule redirect or by typing the URL?

只愿长相守 提交于 2019-12-21 17:24:39

问题


Is there any way in JSF which can be used to find out whether an user reached a page by the Navigation rule of the JSF or user has typed the URL directly to reach that page?

Actully I want to prevent user to go to a page directly by typing the URL for that directly in the Browser, rather I want to restrict the user to use the application navigation bar to go to an page.


回答1:


There is a way: check the presence of the referer header (yes, with the misspelling).

if (externalContext.getRequestHeader("referer") == null) {
    // User has most likely accessed the page by directly typing the URL.
}

or

<h:panelGroup rendered="#{empty header['referer']}">
    <!-- User has most likely accessed the page by directly typing the URL. -->
</h:panelGroup>

This will however fail for users who have neatly used the link in your webpage, but are using some overzealous proxy/firewall/security software which hides the referer header altogether.

You might want to consider to make the page never directly available to users by placing it in /WEB-INF folder and using it as a conditional include which is executed by a POST request (if necessary with ajax). E.g.

<h:form>
    <h:commandLink action="#{bean.showPage}" />
</h:form>

<h:panelGroup rendered="#{bean.showPage}">
    <ui:include src="/WEB-INF/includes/foo.xhtml" />
</h:panelGroup>


来源:https://stackoverflow.com/questions/10994666/how-to-detect-whether-user-have-reached-a-page-by-jsf-navigation-rule-redirect-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!