Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF - implementing filter for restricted pages
    primarykey
    data
    text
    <p>I'm following answer by @BalusC to <a href="https://stackoverflow.com/questions/4105263/jsf-2-0-how-to-get-the-url-that-is-entered-in-the-browsers-address-bar">JSF 2.0: How to get the URL that is entered in the browser&#39;s address bar</a> to restrict pages from users who are not logged in. </p> <p>Filter:</p> <pre><code>public class RestrictPageFilter implements Filter{ FilterConfig fc; @Override public void init(FilterConfig filterConfig) throws ServletException { fc=filterConfig; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpreq = (HttpServletRequest) request; HttpServletResponse httpres = (HttpServletResponse) response; if (httpreq.getUserPrincipal() == null) { httpreq.getSession().setAttribute("from", httpreq.getRequestURI()); httpres.sendRedirect("/pages/login.xhtml"); } else { chain.doFilter(request, response); } } @Override public void destroy() { // TODO Auto-generated method stub } } </code></pre> <p>web.xml:</p> <pre><code>&lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Admin pages&lt;/web-resource-name&gt; &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt; &lt;url-pattern&gt;/restricted/*&lt;/url-pattern&gt; &lt;http-method&gt;GET&lt;/http-method&gt; &lt;http-method&gt;POST&lt;/http-method&gt; &lt;/web-resource-collection&gt; &lt;auth-constraint&gt; &lt;role-name&gt;ADMIN&lt;/role-name&gt; &lt;/auth-constraint&gt; &lt;/security-constraint&gt; &lt;security-constraint&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;User pages&lt;/web-resource-name&gt; &lt;url-pattern&gt;/restricted/*&lt;/url-pattern&gt; &lt;http-method&gt;GET&lt;/http-method&gt; &lt;http-method&gt;POST&lt;/http-method&gt; &lt;/web-resource-collection&gt; &lt;auth-constraint&gt; &lt;role-name&gt;ADMIN&lt;/role-name&gt; &lt;role-name&gt;USER&lt;/role-name&gt; &lt;/auth-constraint&gt; &lt;/security-constraint&gt; &lt;!--login-config&gt; &lt;auth-method&gt;FORM&lt;/auth-method&gt; &lt;realm-name&gt;jdbc-realm&lt;/realm-name&gt; &lt;form-login-config&gt; &lt;form-login-page&gt;/pages/login.xhtml&lt;/form-login-page&gt; &lt;form-error-page&gt;/pages/error.xhtml&lt;/form-error-page&gt; &lt;/form-login-config&gt; &lt;/login-config--&gt; &lt;filter&gt; &lt;filter-name&gt;RestrictPageFilter&lt;/filter-name&gt; &lt;filter-class&gt;gov.denis.chanceryweb5.filter.RestrictPageFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;RestrictPageFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/restricted/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>glassfish-web.xml</p> <pre><code>&lt;glassfish-web-app&gt; &lt;security-role-mapping&gt; &lt;role-name&gt;ADMIN&lt;/role-name&gt; &lt;group-name&gt;ADMIN&lt;/group-name&gt; &lt;/security-role-mapping&gt; &lt;security-role-mapping&gt; &lt;role-name&gt;USER&lt;/role-name&gt; &lt;group-name&gt;USER&lt;/group-name&gt; &lt;/security-role-mapping&gt; </code></pre> <p></p> <p>realm in glassfish gui console: <img src="https://i.stack.imgur.com/zA8dc.jpg" alt="enter image description here"></p> <p>When accessing my web app, in browser i see this for some reason? why?</p> <p><img src="https://i.stack.imgur.com/WwivJ.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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