Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are different ways to do that . Firstly you can use filters to control page access or you can use phase listeners that listens jsf phases .</p> <p>I wanna give you two examples for them ;</p> <pre><code> public class SecurityFilter implements Filter{ FilterConfig fc; public void init(FilterConfig filterConfig)throws ServletException { fc = filterConfig; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{ HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse) response; HttpSession session = req.getSession(true); String pageRequested = req.getRequestURI().toString(); if(session.getAttribute("user") == null &amp;&amp; !pageRequested.contains("login.xhtml")){ resp.sendRedirect("login.xhtml"); }else{ chain.doFilter(request, response); } } public void destroy(){ } } </code></pre> <p>And you should add this filter to web.xml;</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.webapp.SecurityFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>Phase Listener example ;</p> <pre><code>public class SecurityFilter implements PhaseListener { public void beforePhase(PhaseEvent event) { } public void afterPhase(PhaseEvent event) { FacesContext fc = event.getFacesContext(); boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") &gt; -1 ? true : false; if (!loginPage &amp;&amp; !isUserLogged()) { navigate(event,"logout"); } } private boolean isUserLogged() { //looks session for user } private void navigate(PhaseEvent event, String page) { FacesContext fc = event.getFacesContext(); NavigationHandler nh = fc.getApplication().getNavigationHandler(); nh.handleNavigation(fc, null, page); } public PhaseId getPhaseId() { return PhaseId.RESTORE_VIEW; } } </code></pre> <p>So if you want to use listener you should add this to your faces-config.xml ; Note : "logout" is a navigation rule which is defined in faces-config</p> <pre><code>&lt;lifecycle&gt; &lt;phase-listener&gt;com.myapp.SecurityFilter&lt;/phase&gt; &lt;/lifecycle&gt; </code></pre> <p><strong>Edit :</strong> The navigation rule ;</p> <pre><code>&lt;navigation-rule&gt; &lt;from-view-id&gt;/*&lt;/from-view-id&gt; &lt;navigation-case&gt; &lt;from-outcome&gt;logout&lt;/from-outcome&gt; &lt;to-view-id&gt;/login.xhtml&lt;/to-view-id&gt; &lt;redirect/&gt; &lt;/navigation-case&gt; &lt;/navigation-rule&gt; </code></pre> <p>You can put your user to session in login method like that ;</p> <pre><code>FacesContext context = FacesContext.getCurrentInstance(); HttpSession session = (HttpSession)context.getExternalContext().getSession(true); session.setAttribute("user", loggedUser); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload