package br.com.alura.gerenciador.servlet;
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.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AutorizacaoFilter extends HttpFilter implements Filter {
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
System.out.println("Autorização filter");
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String paramAcao = request.getParameter("acao");
HttpSession sessao = request.getSession();
boolean usuarioNaoEstaLogado =sessao.getAttribute("usuarioLogado") == null;
boolean ehUmaAcaoProtegida = !(paramAcao.equals("Login")||paramAcao.equals("loginForm"));
if(ehUmaAcaoProtegida && usuarioNaoEstaLogado) {
response.sendRedirect("entrada?acao=loginForm");
return;
}
chain.doFilter(request, response);
}
}
package br.com.alura.gerenciador.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
public class MonitoramentoFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
long antes=System.currentTimeMillis();
System.out.println("Monitoramento Filter");
String acao =request.getParameter("acao");
System.out.println("Entrando no Chain");
chain.doFilter(request, response);
System.out.println("Saindo do chain");
long depois=System.currentTimeMillis();
System.out.println("Tempo de execução da acao : "+acao+ "->'" + (depois-antes));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>gerenciador</display-name>
<welcome-file-list>
<welcome-file>bem-vindo.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>OiMundoServlet</servlet-name>
<servlet-class>br.com.alura.gerenciador.servlet.OiMundoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OiMundoServlet</servlet-name>
<url-pattern>/ola</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MF</filter-name>
<filter-class>br.com.alura.gerenciador.servlet.MonitoramentoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MF</filter-name>
<url-pattern>/entrada</url-pattern>
</filter-mapping>
<filter>
<filter-name>AF</filter-name>
<filter-class>br.com.alura.gerenciador.servlet.AutorizacaoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AF</filter-name>
<url-pattern>/entrada</url-pattern>
</filter-mapping>
<filter>
<filter-name>CF</filter-name>
<filter-class>br.com.alura.gerenciador.servlet.ControladorFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CF</filter-name>
<url-pattern>/entrada</url-pattern>
</filter-mapping>
</web-app>
Meus filtros ficaram iguais ao que foi proposto em aula , entretanto acrescentei "entrando no chain" e "Saindo do chain" pois não ficou muito claro o fluxo que é feito quando se saí "abruptamente" com o return em :
if(ehUmaAcaoProtegida && usuarioNaoEstaLogado) {
response.sendRedirect("entrada?acao=loginForm");
return;
}