21/06/2007, 07:02
|
| | Fecha de Ingreso: junio-2007
Mensajes: 12
Antigüedad: 17 años, 7 meses Puntos: 0 | |
Re: ayuda: SecurityFilter.doFilter(SecurityFilter.java:120) El filtro es mío, y el código del filtro es el siguiente:
package glucoweb;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import org.apache.struts.* ;
public class LoginFilter implements Filter
{
private ServletContext context = null;
private DataSource dataSource = null;
protected FilterConfig filterConfig;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
/* org.apache.log4j.BasicConfigurator.configure(); */
String userID = null;
HttpSession session = ((HttpServletRequest)request).getSession();
//check if object userbean is present in session
if (session.getAttribute("user") == null)
{
//if not, get user data and load it as a session bean
userID = ((HttpServletRequest)request).getRemoteUser();
User user = new User();
//CONSULTO LA BASE DE DATOS PARA PONER LOS DATOS QUE ME INTERESAN
//EN EL ONJETO USUARIO EN EL SCOPE SESSION
//Ojo!! no podemos utilizar el método getDatasource(HttpServletRequest)
//porque esto no es un Action, hay que conseguir un DataSource de otra manera.
//Globals.DATA_SOURCE_KEY : The context attribute key under which our default configured data
//source (which must implement javax.sql.DataSource) is stored, if one is configured for this module.
//al poner un datasource en struts_config.xml, el atributo keyes...
//key: Once created, the DataSource will be stored under an attribute on the application servlet context.
//This attribute holds the name to be used for the context's attribute.
//The default attribute name is specified by the Globals.DATA_SOURCE_KEY String.
context = filterConfig.getServletContext();
dataSource = (DataSource) context.getAttribute(Globals.DATA_SOURCE_KEY);
try {
user = GlucoWebData.getUser(userID,dataSource);
System.err.println("LoginFilter: " + user.getUserID());
System.err.println("LoginFilter: " + user.getRole());
} catch( Exception e ) {
System.err.println("LoginFilter - Setting target to error");
}
session.setAttribute("user", user);
//si existe el objeto en la sesion pero sus atributos son nulos...
} else if (((User)session.getAttribute("user")).getUserID() == null)
{
//if not, get user data and load it as a session bean
userID = ((HttpServletRequest)request).getRemoteUser();
User user = new User();
//CONSULTO LA BASE DE DATOS PARA PONER LOS DATOS QUE ME INTERESAN
//EN EL ONJETO USUARIO EN EL SCOPE SESSION
//Ojo!! no podemos utilizar el método getDatasource(HttpServletRequest)
//porque esto no es un Action, hay que conseguir un DataSource de otra manera.
//Globals.DATA_SOURCE_KEY : The context attribute key under which our default configured data
//source (which must implement javax.sql.DataSource) is stored, if one is configured for this module.
//al poner un datasource en struts_config.xml, el atributo keyes...
//key: Once created, the DataSource will be stored under an attribute on the application servlet context.
//This attribute holds the name to be used for the context's attribute.
//The default attribute name is specified by the Globals.DATA_SOURCE_KEY String.
context = filterConfig.getServletContext();
dataSource = (DataSource) context.getAttribute(Globals.DATA_SOURCE_KEY);
try {
user = GlucoWebData.getUser(userID,dataSource);
System.err.println("LoginFilter: " + user.getUserID());
System.err.println("LoginFilter: " + user.getRole());
} catch( Exception e ) {
System.err.println("LoginFilter - Setting target to error");
}
session.setAttribute("user", user);
}
// pass the request on
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
}
public void destroy()
{
}
}
muchas gracias! |