Autenticação usando Filter.

Java

17/07/2013

Boa noite.

Estou tendo problema para implementar segurança em uma aplicação java web.

Estou usando o Netbeans + JSF 2.0 + Primefaces + Hibernate + MySQL

Segue o meu Filter para vocês poderem me ajudar.

public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {
                
    boolean auth = request.getAttribute("login") != null;
    HttpServletRequest rq = (HttpServletRequest)request;
    HttpServletResponse rp = (HttpServletResponse)response;
    if (!auth && !rq.getRequestURL().toString().contains("login.faces")) {
       rp.sendRedirect("login.faces");
       return;
    } 
	try {
	    chain.doFilter(request, response);
	}
	catch(Throwable t) {
	}
        
    }


Segue o Bean de autenticação.

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
    
    private UsuarioDao usuarioDao = new UsuarioDao();
    private String login;
    private String senha;

    /**
     * Creates a new instance of LoginBean
     */
    public LoginBean() {
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.login != null ? this.login.hashCode() : 0);
        hash = 59 * hash + (this.senha != null ? this.senha.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final LoginBean other = (LoginBean) obj;
        if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
            return false;
        }
        if ((this.senha == null) ? (other.senha != null) : !this.senha.equals(other.senha)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "LoginBean{" + "login=" + login + '}';
    }
    
    
    
    public String doLogin() {
        if (usuarioDao.isValidLoginAndPassword(login, senha)) {
            String usuario = login;
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("login", usuario);
            /*FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("login", "ok");*/
            System.out.println(usuario);
            return "index.faces";
        } else {
            return "falhalogin.faces";
        }
    }
    
}


Segue o faces

<?xml version='1.0' encoding='UTF-8' ?> 
<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
        <link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
        <title>Sistema SindSepe</title>
    </h:head>

    <h:body>

        <div id="top" align="center">
            <ui:insert name="top"> 
                <h:graphicImage value="resources/imagens/logo1.png" />
                <h:graphicImage value="resources/imagens/cab.gif" />
            </ui:insert>
        </div>
        <div id="content" class="right_content" style="height:400px;width:600px" >
            <ui:insert name="content">     
            <h1><h:outputText value="Bem vindo ao Sistema do SINDSEPE-RS" /></h1>
        <h:form>
            <table border="0">
                <thead>
                    <tr>
                        <th>Usuario:</th>
                        <th><h:inputText value="#{loginBean.login}"/></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Senha:</td>
                        <td><h:inputSecret value="#{loginBean.senha}"/></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><h:commandButton action="#{loginBean.doLogin}" value="Login"/></td>
                    </tr>
                </tbody>
            </table>
        </h:form>
            
            </ui:insert>
        </div>
        <div id="bottom">
            <ui:insert name="bottom">
                SindSepe <br/>
                Rua Otavio Rocha, 666 Sala 801 <bv/>
                Centro<br/>
                Porto Alegre <br/>

            </ui:insert>
        </div>

    </h:body>

</html>


Desde já agradeço qualquer ajuda.
Alex Santos

Alex Santos

Curtidas 0
POSTAR