Servlet filter with JSF

后端 未结 1 493
北恋
北恋 2021-01-22 20:30

I tried to configure Servlet filter with JSF. I get lot of problems here I am using PrimeFaces also.

Here is my web.xml



        
相关标签:
1条回答
  • 2021-01-22 20:52

    With CDI I use this. Seems to work fine. Redirects on ajax requests too.

    All pages are in /secure/, except login.xhtml which are in the root.

    <filter>         
        <filter-name>LoginFilter</filter-name>         
        <filter-class>...LoginFilter</filter-class>     
    </filter>      
    <filter-mapping>         
        <filter-name>LoginFilter</filter-name>         
        <url-pattern>/secure/*</url-pattern>     
    </filter-mapping>  
    

    Filter:

    @Inject
    private LoginBean loginBean;
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
        // Set response headers to no-cache
        HttpServletResponse res = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        res.setDateHeader("Expires", 0); // Proxies.
    
        // Check if user logged in, if not redirect to login.xhtml
        if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) {
            boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
    
            if (!isAjax) {
                res.sendRedirect(req.getContextPath() + "/login.xhtml"); 
            } else {
                // Redirecting an ajax request has to be done in the following way:
                // http://javaevangelist.blogspot.dk/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html
                String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml");
                StringBuilder sb = new StringBuilder();
                sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>");
                res.setCharacterEncoding("UTF-8");
                res.setContentType("text/xml");
                PrintWriter pw = response.getWriter();
                pw.println(sb.toString());
                pw.flush();
            }
        } else {
            // Let chain of filters continue;
            chain.doFilter(request, response);
        }
    }
    

    login.xhtml:

    <h:body onload="PF('dlg').show()">
        <p:growl id="growl" life="5000" autoUpdate="true" showDetail="true" escape="false"/>
    
        <h:form>    
            <p:dialog id="dialog" header="Login" footer="..." width="400" widgetVar="dlg" closable="false" showEffect="clip" draggable="false" resizable="false" style="box-shadow: 7px 10px 5px #303030;"> 
    
                <p:panelGrid columns="2">
                    <p:outputLabel value="Username"/>            
                    <p:inputText value="#{loginBean.username}" id="username"/>            
                    <p:outputLabel value="Password"/>            
                    <p:password value="#{loginBean.password}" id="password"/>            
                </p:panelGrid>
                <p:commandButton  id="button" value="Login" action="#{loginBean.doLogin}" style="float:right"/> 
    .... close tags
    

    LoginBean is a simple SessionScoped CDI bean.

    0 讨论(0)
提交回复
热议问题