Skip to main content

Manejo de Expiracion de Sesion con Jakarta EE

Manejo de Expiración de Sesión con Jakarta EE





Video de demostración



En el archivo web.xml 

agregar

    <error-page>

    <exception-type>javax.faces.application.ViewExpiredException</exception-type>

    <location>/expired.xhtml</location>

</error-page>




agregar

  

    <listener>

  <listener-class>

 com.javscaz.fiscalprinter.security.SecuritySessionListener

  </listener-class>

</listener>


Crear la clase SecuritySessionListener.java

Para manejar la creación y destrucción de sesiones

import com.avbravo.jmoordb.configuration.JmoordbContext;

import com.avbravo.jmoordbutils.JsfUtil;

import java.io.Serializable;

import java.util.Date;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

import org.primefaces.PrimeFaces;


/**

 *

 * @author avbravo

 */

public class SecuritySessionListener implements HttpSessionListener, Serializable {


    @Override

    public void sessionCreated(HttpSessionEvent se) {

        System.out.println("Session created : " + se.getSession().getId() + " at " + new Date());


    }


    @Override

    public void sessionDestroyed(HttpSessionEvent se) {

        HttpSession session = se.getSession();

           System.out.println("session destroyed :" + session.getId() + " Logging out user... at  " + new Date());


    }

}
























Crear la pagina expired.xhtml







<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:h="http://java.sun.com/jsf/html"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:b="http://bootsfaces.net/ui"

      xmlns:p="http://primefaces.org/ui"

      xmlns:jmoordbjsf="http://jmoordbjsf.com/taglib">

    <h:head> 

        <title>#{msg['application.title']}</title>

        <meta name="author" content="Aristides Villarreal"></meta>

        <style type="text/css">

            .form-signin {

                margin: 0 auto;

                max-width: 330px;

                padding: 15px;

            }

        </style>

        <f:facet name="first">

            <f:view locale="#{jmoordbLanguajes.locale !=null?jmoordbLanguajes.locale:'es'}"></f:view>

            <f:loadBundle basename="com.properties.messages" var="msg" />

            <f:loadBundle basename="com.jmoordbutils.properties.application" var="app" />


        </f:facet>

        <meta http-equiv="refresh" content="15;url=./login.xhtml"/>


    </h:head>

    <h:body style="padding-top: 60px; background-color: #000000">


        <h:form styleClass="form-signin" prependId="false" id="form">


            <h2 class="form-signin-heading"></h2>


           <h1 style="color:white">Sesion expirada</h1>


            <p style="color:white"> Para proteger su seguridad </p>

            <p>Sera redireccionado a la pagina de autentificación</p>


            <p style="color:white"> Espere unos segundos...</p>

            <a href="./login.xhtml"> o de Clic aqui para ir al Login</a>

        </h:form>


    </h:body>

</html>










Pagina login.xhtml

en el login agregar en el header

<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/#{loginController.expired()}"/>



Quedaria


<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:h="http://java.sun.com/jsf/html"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:b="http://bootsfaces.net/ui"

      xmlns:p="http://primefaces.org/ui"

      xmlns:jmoordbjsf="http://jmoordbjsf.com/taglib">

    <h:head> 

        <title>#{msg['application.title']}</title>

        <meta name="author" content="Aristides Villarreal"></meta>

        <style type="text/css">

            .form-signin {

                margin: 0 auto;

                max-width: 330px;

                padding: 15px;

            }

        </style>

        <f:facet name="first">

            <f:view locale="#{jmoordbLanguajes.locale !=null?jmoordbLanguajes.locale:'es'}"></f:view>

            <f:loadBundle basename="com.properties.messages" var="msg" />

            <f:loadBundle basename="com.jmoordbutils.properties.application" var="app" />


        </f:facet>

<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/#{loginController.expired()}"/>

    </h:head>

    <h:body style="padding-top: 60px; background-color: lightslategray">

        <jmoordbjsf:ajaxstatus text="Verificando credenciales"/>

        

        <b:container fluid="true"> 

            <h:form styleClass="form-signin" prependId="false" id="form">


       

                <p:messages id="growl" closable="true"  />

                <h2 class="form-signin-heading"><p:outputLabel value="#{msg['application.title']}" style="color: white"/></h2>



                <jmoordbjsf:login

                    id="username"

                    fieldusername="#{loginController.username}"

                    labelusername="#{app['login.username']}"

                    fieldpassword="#{loginController.password}"

                    dologin="#{loginController.doLogin()}"

                    showone="false"

                    idone="rol"


                    />



                <p:outputLabel value="#{msg['application.version']}" style="color: white"/>



            </h:form>

        </b:container>

    </h:body>

</html>












Template.xhtml

En la plantilla para que se habilite en todas las páginas agregamos


<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/#{loginController.expired()}"/>


Quedaria

<?xml version='1.0' encoding='UTF-8' ?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

      xmlns:h="http://java.sun.com/jsf/html"

      xmlns:f="http://java.sun.com/jsf/core"

      xmlns:b="http://bootsfaces.net/ui"

      xmlns:ui="http://java.sun.com/jsf/facelets"


      xmlns:jmoordbjsf="http://jmoordbjsf.com/taglib"

      xmlns:p="http://primefaces.org/ui">

    <h:head>

        <f:facet name="first">

            <f:view locale="#{jmoordbLanguajes.locale !=null?jmoordbLanguajes.locale:'es'}"></f:view>

            <f:loadBundle basename="com.properties.messages" var="msg" />

            <f:loadBundle basename="com.jmoordbutils.properties.application" var="app" />


        </f:facet>

        <title>#{msg['application.shorttitle']}</title>

        <meta name="author" content="Aristides Villarreal" />

        <meta name="description" content="BootsFaces, a powerful JSF framework that takes the best from Bootstrap and jQuery UI to let develop well-designed state-of-the-art next-gen Front-end Enterprise Applications fast and easy supporting HTML5." />

        <meta name="KEYWORDS" content="jsf, bootstrap, framework, java, enterprise, server, faces, jquery, usability, next-gen, web, html5, easy, modern, well-designed, website, state-of-the-art" />

        <meta name="robots" content="index, follow" />


<meta http-equiv="refresh" content="#{facesContext.externalContext.sessionMaxInactiveInterval};url=#{request.contextPath}/#{loginController.expired()}"/>

        <style type="text/css">


            .ui-growl {


                right: 10%;

                top: 20%;

            }

        </style>

        <jmoordbjsf:metatemplate/>

        <jmoordbjsf:csstemplate/>

        <style>.tooltip-inner {

                max-width: 350px;

                /* If max-width does not work, try using width instead */

                width: 350px; 

            }</style>


     


        <script name="jquery/jquery.js" library="primefaces" target="head" ></script>

    </h:head>

    <h:body style="padding-top: 85px;padding-bottom: 85px;" >


        <jmoordbjsf:jstemplate/>

       


        <ui:insert name="nbtop">


            <ui:include src="/layout/top.xhtml"/>

        </ui:insert>



        <b:container fluid="true">

            <b:row>

                <b:column medium-screen="2" hidden="tiny-screen,small-screen" style-class="site-navigation">

                    <ui:include src="left.xhtml" />

                </b:column>

                <b:column medium-screen="9">

                    <ui:insert name="content"/>

                </b:column>

                <b:column medium-screen="1"  hidden="tiny-screen,small-screen">

                    <div style="position:fixed;top:118px" class="internal-navigation">

                        <ui:insert name="content-navigator"/>

                    </div>

                </b:column>

            </b:row>

        </b:container>


        <ui:insert name="nbtop">

            <ui:include src="/layout/footer.xhtml"/>

        </ui:insert>

        


    </h:body>

</html>






En el LoginController.java


Crear el método expired() que devuelve el path a la pagina expired.xhtml


     // <editor-fold defaultstate="collapsed" desc="String expired()">

     

     public String expired(){


        return "faces/expired.xhtml?faces-redirect=true";

     }// </editor-fold>




Comments

Popular posts from this blog

Primefaces v13

 La versión nueva de primefaces v13.0 elimina el componente <p:repeat>  Consulte los cambios en  https://github.com/primefaces/primefaces/releases/tag/13.0.0 por lo tanto si necesita una solución similar considere utilizar  <p:carousel>  o  <ui:repeat> En la versión 13 genera una excepción

Jmoordb-Core @UpdateMany y @DeleteMany

 Jmoordb-Core @UpdateMany y @DeleteMany Estoy trabajando en una versión nueva de jmoordb-core en la cual se estará implementando nuevas funcionalidades como lo son @DeleteMany y    @UpdateMany con la finalidad de hacer operaciones sobre múltiples documentos de la colección.