Senhores após implementar a classe de FiltroAuditoria especialmente na parte de logar do usuário, começou a aprensentar alguns problemas.
package br.com.alura.gerenciador.web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import br.com.alura.gerenciador.Usuario;
@WebFilter(urlPatterns = "/*")
public class FiltroAuditoria implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession();
Usuario usuarioLogado = (Usuario) session
.getAttribute("usuario_logado");
String usuario = "<deslogado>";
if (usuarioLogado != null) {
usuario = usuarioLogado.getEmail();
}
System.out.println("Usuario " + usuario + " acessando a URI "
+ req.getRequestURI());
chain.doFilter(request, response);
}
@SuppressWarnings("unused")
private Cookie getUsuario(HttpServletRequest req) {
Cookie cookie = new Cookies(req.getCookies()).getUsuarioLogado();
return cookie;
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
Quando logo com o usuario:
Usuario acessando a URI /gerenciador/busca Usuario acessando a URI /gerenciador/index.html Usuario acessando a URI /gerenciador/login abr 03, 2017 1:25:27 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.web.BuscaEmpresa] in context with path [/gerenciador] threw exception java.lang.ClassCastException: br.com.alura.gerenciador.dao.UsuarioDAO cannot be cast to br.com.alura.gerenciador.Usuario at br.com.alura.gerenciador.web.FiltroAuditoria.doFilter(FiltroAuditoria.java:34) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)
abr 03, 2017 1:25:37 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.web.BuscaEmpresa] in context with path [/gerenciador] threw exception java.lang.ClassCastException: br.com.alura.gerenciador.dao.UsuarioDAO cannot be cast to br.com.alura.gerenciador.Usuario at br.com.alura.gerenciador.web.FiltroAuditoria.doFilter(FiltroAuditoria.java:34) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
Pelo que entendi não pode fazer este cast:
Usuario usuarioLogado = (Usuario) session.getAttribute("usuario_logado");