Java - filtros j2ee para autenticar

   
Vista:

filtros j2ee para autenticar

Publicado por nerelaya (1 intervención) el 21/03/2009 20:56:31
Hola!

Soy nueva utilizando filtros j2ee. Estoy intentando realizar un filtro j2ee para poder mirar en cada servlet o jsp si se ha creado una sesion con los datos del usuario, pero no encuentro un ejemplo entero para realizar esto y lo creado es el siguiente .java:

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;

public class LoginFilter implements Filter{

public FilterConfig filterConfig;

public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws java.io.IOException, javax.servlet.ServletException, ServletException {
HttpSession session;
if (req instanceof HttpServletRequest){
session = ((HttpServletRequest)req).getSession();
if (session.getAttribute("user") != null){
chain.doFilter(req, res);
}else{
((HttpServletResponse)res).sendRedirect(((HttpServletRequest)req).getContextPath() + "Login");
}
}
}


public void init(final FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

public void destroy() {
this.filterConfig = null;
}

}

y en el web.xml:

<filter>
<filter-name>LoginFilter</filter-name>
<filter-name>LoginFilter</filter-name>
</filter>

<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Pero al arrancar tomcat me pone el siguiente error:

20-mar-2009 5:40:32 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/i386/client:/usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/i386:/usr/lib/jvm/java-6-sun-1.6.0.10/jre/../lib/i386:/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client:/usr/lib/jvm/java-6-openjdk/jre/lib/i386:/usr/lib/jvm/java-6-openjdk/jre/../lib/i386:/usr/lib/xulrunner-addons:/usr/java/packages/lib/i386:/lib:/usr/lib
20-mar-2009 5:40:32 org.apache.coyote.http11.Http11BaseProtocol init
INFO: Inicializando Coyote HTTP/1.1 en puerto http-8080
20-mar-2009 5:40:32 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3702 ms
20-mar-2009 5:40:33 org.apache.catalina.core.StandardService start
INFO: Arrancando servicio Catalina
20-mar-2009 5:40:33 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/5.5.23
20-mar-2009 5:40:33 org.apache.catalina.core.StandardHost start
INFO: Desactivada la validación XML
20-mar-2009 5:40:33 org.apache.catalina.util.ExtensionValidator addFolderList
GRAVE: Failure loading extension /usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/ext/mysql-connector-java-3.1.8-bin.jar
java.io.FileNotFoundException: /usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/ext/mysql-connector-java-3.1.8-bin.jar (Permiso denegado)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at org.apache.catalina.util.ExtensionValidator.addSystemResource(ExtensionValidator.java:209)
at org.apache.catalina.util.ExtensionValidator.addFolderList(ExtensionValidator.java:410)
at org.apache.catalina.util.ExtensionValidator.<clinit>(ExtensionValidator.java:104)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4064)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
20-mar-2009 5:40:34 org.apache.catalina.core.StandardContext filterStart
GRAVE: Excepción arrancando filtro LoginFilter
java.lang.NullPointerException
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:302)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:78)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3635)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4222)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
20-mar-2009 5:40:34 org.apache.catalina.core.StandardContext start
GRAVE: Error filterStart
20-mar-2009 5:40:34 org.apache.catalina.core.StandardContext start
GRAVE: Falló en arranque del Contexto [/proyecto] debido a errores previos
20-mar-2009 5:40:35 org.apache.catalina.core.ApplicationContext log
INFO: org.apache.webapp.balancer.BalancerFilter: init(): ruleChain: [org.apache.webapp.balancer.RuleChain: [org.apache.webapp.balancer.rules.URLStringMatchRule: Target string: News / Redirect URL: http://www.cnn.com], [org.apache.webapp.balancer.rules.RequestParameterRule: Target param name: paramName / Target param value: paramValue / Redirect URL: http://www.yahoo.com], [org.apache.webapp.balancer.rules.AcceptEverythingRule: Redirect URL: http://jakarta.apache.org]]
20-mar-2009 5:40:36 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
20-mar-2009 5:40:36 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
20-mar-2009 5:40:36 org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
20-mar-2009 5:40:36 org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
20-mar-2009 5:40:37 org.apache.coyote.http11.Http11BaseProtocol start
INFO: Arrancando Coyote HTTP/1.1 en puerto http-8080
20-mar-2009 5:40:37 org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
20-mar-2009 5:40:38 org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/226 config=null
20-mar-2009 5:40:38 org.apache.catalina.storeconfig.StoreLoader load
INFO: Find registry server-registry.xml at classpath resource
20-mar-2009 5:40:38 org.apache.catalina.startup.Catalina start
INFO: Server startup in 5536 ms

Y no se como arreglarlo, alguien podria indicarme que es lo que realizo mal o darme alguna url donde haya ejemplos completos de esta clase de filtro.

X otra parte, como se llama desde un servlet o un jsp a estos filtros para poder ver si se ha creado la sesion y como puedo redireccionar a que se logue el usuario y volver a la pagina en la que estaba antes de loguearse.

Muchas gracias, es urgente!!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder