Fórum Salvar usuario logado na sessao spring security #458330
12/10/2013
0
tenho uma classe pessoa onde tenho meus atributos e tenho uma outra chamada pontoVenda onde herdo alguns atributos da pessoa como ex ( nome,email ,senha) estou usando spring para autenticar e ja estou conseguindo logar no spring com email ,senha . mas como eu faço para pegar o usuário logado e salvar na sessão. para quando eu logar aparecer só os pontos de venda cadastrado de acordo com cada usuário logado.
@Entity
@Table(name="pessoa")
@Inheritance(strategy = InheritanceType.JOINED)
public class Pessoa implements Serializable{
@Id
@GeneratedValue
@Column(name="id_pessoa")
private int idPessoa;
private String nome;
private String email;
private String cidade;
private String bairro;
private String telefone;
private String celular;
private String senha;
private String cep;
private String estado;
@Column(name="data_cadastro")
@Temporal(javax.persistence.TemporalType.DATE)
private Date dataCadastro;
@Column(name = "status", columnDefinition = "BOOLEAN")
private boolean status;
private String tipo;
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(int idPessoa) {
this.idPessoa = idPessoa;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
@Entity
@Table(name="pontovenda")
@PrimaryKeyJoinColumn(name = "id_pontovenda")
public class PontoVenda extends PessoaJuridica implements Serializable {
@OneToMany(mappedBy="pontovenda")
private List<Venda> venda;
public List<Venda> getVenda() {
return venda;
}
public void setVenda(List<Venda> venda) {
this.venda = venda;
}
}
@ManagedBean(name = "pontovendaMB")
@SessionScoped
public class PontoVendaController implements Serializable{
private PontoVenda pontoVenda;
private DataModel listaPontoVendas;
public DataModel getListaPontoVendas() {
List<PontoVenda> lista = new PontoVendaDaoImp().listaPontoVenda();
listaPontoVendas = new ListDataModel(lista);
return listaPontoVendas;
}
public void ativo(ActionEvent event){
if(pontoVenda.isStatus() == false)
{
pontoVenda.setStatus(true);
}
else{
pontoVenda.setStatus(false);
}
}
public String prepararAdicionarPontoVenda() {
pontoVenda = new PontoVenda();
return "listaPontoVenda.jsf";
}
public void prepararAlterarPontoVenda() {
pontoVenda = (PontoVenda) (listaPontoVendas.getRowData());
}
public void prepararExcluirPontoVenda() {
pontoVenda = (PontoVenda) (listaPontoVendas.getRowData());
}
public String excluirPontoVenda() {
PontoVendaDao dao = new PontoVendaDaoImp();
//this.pontoVenda.setStatus(false);
dao.inativa(pontoVenda);
return "listaPontoVenda.jsf";
}
public String adicionarPontoVenda() throws IOException {
PontoVendaDao dao = new PontoVendaDaoImp();
int cod = pontoVenda.getIdPessoa();
if (cod == 0) {
pontoVenda.setDataCadastro(new Date());
pontoVenda.setStatus(true);
dao.salvar(this.pontoVenda);
} else {
dao.alterar(this.pontoVenda);
}
return "default.jsf";
}
public PontoVenda getPontoVenda() {
return pontoVenda;
}
public void setPontoVenda(PontoVenda pontoVenda) {
this.pontoVenda = pontoVenda;
}
}
@Override
public void salvar(PontoVenda pontovenda) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
session.save(pontovenda);
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
session.close();
}
}
@Override
public PontoVenda getPontoVenda(int idPessoa) {
Session session = HibernateUtil.getSessionFactory().openSession();
return (PontoVenda) session.load(PontoVenda.class, idPessoa);
}
@Override
public List<PontoVenda> listaPontoVenda() {
List<PontoVenda> lista = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
lista = session.createQuery("from PontoVenda where status = true").list();
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
session.close();
}
return lista;
}
@Override
public void alterar(PontoVenda pontoVenda) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
session.update(pontoVenda);
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
session.close();
}
}
@Override
public void inativa(PontoVenda pontoVenda) {
Session session = HibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = session.beginTransaction();
session.update(pontoVenda);
tx.commit();
} catch (HibernateException ex) {
ex.printStackTrace();
} finally {
session.close();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Bem vindo ao Gerenciador de Livros!</title>
<link type="text/css" rel="stylesheet" href="dot-luv/skin.css"/>
</h:head>
<h:body style="background-image:url('http://pwes.rocklin.k12.ca.us/j0439526.jpg');background-position: 50% 0%;background-repeat: no-repeat">
<p:dialog header="Login" visible="true" closable="false" draggable="false" resizable="false" width="420">
<center>
<h:outputText value="Usuário ou senha incorretos!" rendered="#{param.erro}" style="color: darkred"/>
</center>
<form action="j_spring_security_check" method="post">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="j_username" value="Username: *" />
<h:inputText id="j_username" required="true"/>
<h:outputLabel for="j_password" value="Password: * " />
<h:inputSecret id="j_password" required="true"/>
<h:commandButton value="Login"/>
</h:panelGrid>
</form>
</p:dialog>
</h:body>
</html>
Thiago Castello
Curtir tópico
+ 0
Responder
Posts
30/10/2015
Alvaro Silva
Cara, estou com esse mesmo problema, vc já teve uma solução?
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)