Conexión JEE Autenticacion mediante JDBC

Se deben de crean las BD para Usuario y Roles


ahora creamos los dos archivos de configuración

Contenido de glassfish-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
  <security-role-mapping>
        <role-name>ADMINISTRATOR</role-name>
        <group-name>ADMINISTRATOR</group-name>
    </security-role-mapping>
</glassfish-web-app>


Contenido de web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <display-name>Admin Pages</display-name>
        <web-resource-collection>
            <web-resource-name>Admiistrador</web-resource-name>
            <description/>
            <url-pattern>/faces/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>ADMINISTRATOR</role-name>
        </auth-constraint>
        <user-data-constraint>
            <description/>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>JDBCRealm</realm-name>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/loginerror.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description/>
        <role-name>ADMINISTRATOR</role-name>
    </security-role>
</web-app>

Tenemos creado dos paginas una de Login.xhtml y la otra de LoginError.xhtml estas con el fin de re diseccionar a un ambiente mas atractivo para el usuario, al igual que en el loginerror se puede brindar link de re direccionamiento a la aplicación inicial.
login.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
Carlos Ernesto Ruiz Ricaurte
@CarlosCaliche2
Carloscaliche2@gmail.com
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Login Form</title>
    </h:head>
    <h:body>
        <p:panel header="Login From">
            <form method="POST" action="j_security_check">
                Username: <input type="text" name="j_username" />
                Password: <input type="password" name="j_password" />
                <br />
                <input type="submit" value="Login" />
                <input type="reset" value="Reset" />
            </form> 
        </p:panel>
    </h:body>
</html>


loginerror.xhtml


<?xml version="1.0" encoding="UTF-8"?>
<!--
Carlos Ernesto Ruiz Ricaurte
@CarlosCaliche2
Carloscaliche2@gmail.com
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui" 
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Login Error </title>
    </h:head>
    <h:body>
        <p:panel header="Login Error">
            Sorry, you made an Error. <br />Please try again: <a href="#{facesContext.externalContext.requestContextPath}/" >Homepage</a>
        </p:panel>
    </h:body>
</html>


Ahora realizamos la conexión en la consola de Glassfish 4  para ello necesitamos el usuario y contraseña que la encuentras en el servidor de glassfish 

en este caso el servidor se creo sin user ni contraseña.

Ahora creamos la nueva conexión de JDBC dentro de Server-Config



Después se habilita por defecto esta conexión para realizar la autenticación en Default Realm