Display user info on postback from protected pages in JSF 2.0

做~自己de王妃 提交于 2019-12-11 07:39:46

问题


I have a JSF app with a div on the left that displays the user name of logged in User on the main page.

I store the logged in users info in a sessionScoped managed bean.

I have couple of protected pages on the main page for which I use container managed JDBC realm.

If the user directly clicks on the links without logging in , container managed security kicks in and asks the user to Log in , which is fine.

Now if the user hits the back button , the browser displays the main page . Now how do I display the users name here as my managed bean has not been triggered.

Also the browser seems to cache the page and does request the page from the server. Can anyone shed some light please.

Thanks, Goutham


回答1:


Disable browser caching of dynamic JSF pages. Create a filter which is mapped on *.xhtml and does the following job in doFilter() method:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest) request;
    HttpServletResponse httpRes = (HttpServletResponse) response;

    if (!httpReq.getRequestURI().startsWith(httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
        httpRes.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        httpRes.setDateHeader("Expires", 0); // Proxies.
    }

    chain.doFilter(request, response);
}

This way the browser is forced to send a full HTTP request instead of showing the page from the browser cache.

For displaying the user login name of container managed security I would by the way just use #{request.remoteUser} instead.

<h:outputText value="Welcome, #{request.remoteUser}!" rendered="#{not empty request.remoteUser}" />
<h:link value="Login" outcome="login" rendered="#{empty request.remoteUser}" />


来源:https://stackoverflow.com/questions/7163051/display-user-info-on-postback-from-protected-pages-in-jsf-2-0

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