Boa tarde colegas,
Estou com um problema, após realizar o login na minha página o filtro criado não é executado, ele executa antes do managenBean. O que estou fazendo de errado?
Segue o código:
Managen Bean
package br.com.rtkomp.managerbean;
import br.com.rtkomp.dao.UsuarioDao;
import br.com.rtkomp.entity.Usuario;
import javax.faces.context.FacesContext;
/**
*
* @author thiagortk
*/
public class LoginFace {
private UsuarioDao ud = new UsuarioDao();
public String user;
public String pass;
boolean logged;
public LoginFace() {
logged = false;
}
public String doLogin(){
try{
Usuario u = ud.validatedUser(user, pass);
logged = (u == null) ? false : true;
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().
put("idLogged", logged);
if(logged == false)
return "invalidLogin";
}
catch(Exception ex){
// TODO implementar tratamento de Erro
}
return "gotoIndex";
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
Classe de Filtro
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.rtkomp.filter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author thiagortk
*/
public class Autenticad implements Filter {
private static final boolean debug = true;
// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;
public Autenticad() {
}
/**
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
try {
HttpServletRequest hsr = (HttpServletRequest) request;
HttpServletResponse hsrp = (HttpServletResponse) response;
String url = hsr.getRequestURL().toString();
Object logged = hsr.getSession().getAttribute("idLogged");
// caso não esteja logado, redireciona para pagina de login
if(logged == null || ((Boolean) logged).booleanValue() == false){
// libera acesso caso esteja entrando na pagina de login
if(!url.contains("login")){
hsrp.sendRedirect("login.jsf");
}
}
chain.doFilter(request, response);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}
/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
/**
* Destroy method for this filter
*/
public void destroy() {
}
/**
* Init method for this filter
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log("Autenticad:Initializing filter");
}
}
}
/**
* Return a String representation of this object.
*/
@Override
public String toString() {
if (filterConfig == null) {
return ("Autenticad()");
}
StringBuffer sb = new StringBuffer("Autenticad(");
sb.append(filterConfig);
sb.append(")");
return (sb.toString());
}
private void sendProcessingError(Throwable t, ServletResponse response) {
String stackTrace = getStackTrace(t);
if (stackTrace != null && !stackTrace.equals("")) {
try {
response.setContentType("text/html");
PrintStream ps = new PrintStream(response.getOutputStream());
PrintWriter pw = new PrintWriter(ps);
pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N
// PENDING! Localize this for next official release
pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
pw.print(stackTrace);
pw.print("</pre></body>\n</html>"); //NOI18N
pw.close();
ps.close();
response.getOutputStream().close();
} catch (Exception ex) {
}
} else {
try {
PrintStream ps = new PrintStream(response.getOutputStream());
t.printStackTrace(ps);
ps.close();
response.getOutputStream().close();
} catch (Exception ex) {
}
}
}
public static String getStackTrace(Throwable t) {
String stackTrace = null;
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
sw.close();
stackTrace = sw.getBuffer().toString();
} catch (Exception ex) {
}
return stackTrace;
}
public void log(String msg) {
filterConfig.getServletContext().log(msg);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<filter-mapping>
<filter-name>Autenticad</filter-name>
<url-pattern>*.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>Autenticad</filter-name>
<url-pattern>*.jsf</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>forward.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<filter>
<filter-name>Autenticad</filter-name>
<filter-class>br.com.rtkomp.filter.Autenticad</filter-class>
</filter>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>blueSky</param-value>
</context-param>
</web-app>
faces.config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<application>
<view-handler>
com.sun.facelets.FaceletViewHandler
</view-handler>
</application>
<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>br.com.rtkomp.managerbean.LoginFace</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/login.jsf</from-view-id>
<navigation-case>
<from-outcome>gotoIndex</from-outcome>
<to-view-id>/index.jsf</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>invalidLogin</from-outcome>
<to-view-id>/login.jsf</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
pagina de login
<?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:rich="http://richfaces.org/rich"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<head>
<link href="./css/cssLayout.css" rel="stylesheet" type="text/css" />
<link href="./css/cssPadrao.css" rel="stylesheet" type="text/css" />
<link href="./css/default.css" rel="stylesheet" type="text/css" />
<title>Login - POS Browser</title>
</head>
<body>
<div class="principal">
<ui:insert name="content">
<h:form id="formLogin">
<rich:panel>
<f:facet name="header">
<h:outputText value="Login - POS Browser" />
</f:facet>
<table align="center" width="100%">
<tbody>
<tr>
<td align="left">
Nome do Usuário:<br/>
<h:inputText id="txtUser" size="20" maxlength="15" value="#{LoginBean.user}" required="true" onkeydown="if(event.keyCode==13) event.keyCode=9;"/>
</td>
<td></td>
<td></td>
<td>
<div align="right">
<h:graphicImage url="./img/users.png"/>
</div>
</td>
</tr>
<tr>
<td align="left">
Senha:<br/>
<h:inputSecret id="txtKey" size="20" maxlength="10" value="#{LoginBean.pass}" required="true"/>
</td>
<td></td>
<td></td>
<td>
<div align="right">
<h:graphicImage url="./img/secretkey.png"/>
</div>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td align="left">
<div class="imagem">
<h:commandLink id="cmdlLogin" action="#{LoginBean.doLogin}" title="Clique aqui para entrar no sistema POS Browser.">
<img src="img/partkey.png" width="64" height="64" alt="partkey" border="0"/>
<br/>Efetuar Login
</h:commandLink>
</div>
</td>
<td></td>
<td></td>
<td align="right">
<div class="imagem">
<h:commandLink id="cmdlSair" action="#" title="Clique aqui para sair do sistema POS Browser.">
<img src="img/power.png" width="64" height="64" alt="partkey" border="0"/>
<br/>Sair do Sistema
</h:commandLink>
</div>
</td>
</tr>
</tbody>
</table>
</rich:panel>
</h:form>
</ui:insert>
</div>
</body>
</html>
Já agradecendo.
Abraços.