Users table;
create table usuario(
usuario_nome varchar(15) not null,
usuario_senha varchar(15) not null,
primary key (usuario_nome)
);
table permission;
create table permissao(
usuario_nome varchar(15) not null,
usuario_permissao varchar(15) not null,
primary key (usuario_nome, usuario_permissao),
foreign key (usuario_nome) references usuario(usuario_nome)
);
Inserts;
insert into usuario values('matheus','maithe');
insert into usuario values('maithe','matheus');
insert into permissao values('matheus','administrador');
insert into permissao values('maithe','usuario');
bean
@ManagedBean
public class UsuarioBean {
private String usuario_nome;
private String usuario_senha;
public String login() {
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
try {
request.login(this.usuario_nome, this.usuario_senha);
return "dados?.redrect-true";
} catch (ServletException ex) {
return null;
}
}
/**
* @return the usuario_nome
*/
public String getUsuario_nome() {
return usuario_nome;
}
/**
* @param usuario_nome the usuario_nome to set
*/
public void setUsuario_nome(String usuario_nome) {
this.usuario_nome = usuario_nome;
}
/**
* @return the usuario_senha
*/
public String getUsuario_senha() {
return usuario_senha;
}
/**
* @param usuario_senha the usuario_senha to set
*/
public void setUsuario_senha(String usuario_senha) {
this.usuario_senha = usuario_senha;
}
}
web.xml
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/TelaDeLogin.xhtml</form-login-page>
<form-error-page>/TelaDeLogin.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>usuario</role-name>
</security-role>
<security-role>
<role-name>administrador</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>Tela Usuario</web-resource-name>
<url-pattern>/telausuario_1.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>usuario</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Administrador</web-resource-name>
<url-pattern>/dados.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>administrador</role-name>
</auth-constraint>
</security-constraint>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ProjetoGrandeRecife" >
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/dp"
connectionName="root" connectionPassword="root"
userTable="usuario" userNameCol="usuario_nome" userCredCol="usuario_senha"
userRoleTable="permissao" roleNameCol="usuario_permissao"/>
</Context>
login screen
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Teste</title>
<style type="text/css">
body {background-color: #eeeeee; font-size: 12px}
</style>
</h:head>
<h:body>
<div align="center">
<p:layout style="min-width:1020px;max-width:1070px;min-height:640px">
<p:layoutUnit position="north" size="100">
<h:graphicImage library="imagens" name="logo.png" width="1060" height="85"/>
</p:layoutUnit>
<p:layoutUnit position="center" size="800" rendered="true">
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<div id="msg1" style="background-color: #eeeeee; font-size: 30px">
GRANDE RECIFE CONSORCIO DE TRANSPORTE
</div><br/>
<div id="msg2">
Identifique-se por favor para utilizar
o sistema.
</div><br/>
<h:panelGrid columns="2">
<h:outputLabel value="Nome:" for="nome" style="background-color: navajowhite"/>
<h:inputText id="nome" style="background-color: #dddddd" label="Nome" value="#{usuarioBean.usuario_nome}" required="true" />
<h:outputLabel value="Senha:" for="senha" style="background-color: navajowhite"/>
<h:inputSecret id="senha" style="background-color: #dddddd" label="Senha" value="#{usuarioBean.usuario_senha}" required="true" />
</h:panelGrid><br/>
<h:commandButton action="#{usuarioBean.login()}" value="Entrar" style="position: relative;left: 78px;"/><br/>
</p:layoutUnit>
</p:layout>
</div>
</h:body>
</html>
Result
Message from lcosta, Thursday, 13:34 28-Jul-2016 13: 39: 09.356 INFO [http-apr-8080-exec-32] org.apache.catalina.util.LifecycleBase.start The start () method was called on component [StandardEngine [Catalina] .StandardHost [localhost] .StandardContext [/ RecifeGreat]]] after start () had already been called. The second call will be ignored.