Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue while redirecting from a filter
    primarykey
    data
    text
    <p>I have an issue with a filter in an Spring 3 MVC web application. My enviroment is configured as followed: SpringSource Tool Suite, JDK 7, Tomcat 7, Maven 3.0.2, Hibernate 3 and MySQL 5 database.</p> <p>What I want:</p> <ul> <li>Implement a filter in order that it handles all the incoming HTTP requests (to do some URL blocking) and then forward the request to the Spring handler mapper.</li> </ul> <p>What I get:</p> <ul> <li>When I start the server (Tomcat) everything works fine and the main page (index.jsp) is shown.</li> <li>When I debug, I can see that the filter is intercepting the HTTP request as expected and forwarding to the next filter in the chain. However, when I try accessing an URL from a link in the main page and this link is mapped to a method using the DefaultAnnotationHandlerMapping (e.g annotated with @RequestMethod()), it intercepts the URL but the forward doesn't work as expected (seems to not being intercepted by the DefaultAnnotationHandlerMapping) and it shows an ERROR 404 (resource not available) in the browser.</li> </ul> <p>To provide you some information, these are my files:</p> <p><strong>web.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;display-name&gt;cheapig&lt;/display-name&gt; &lt;!--Definição do Contexto Global do Container do Spring com recursos (beans) que são compartilhados com TODOS os Servlets e Filtros --&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Cria o container do Spring compartilhado com todos os Servlets e Filtros --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- Filtro para controlar acesso --&gt; &lt;filter&gt; &lt;filter-name&gt;cheapigFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;cheapigFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;!-- Definição do Servlet que processa todos os requests da aplicação. Como se está utilizando o Framework Spring, o servlet é o DispatcherServlet. --&gt; &lt;servlet&gt; &lt;servlet-name&gt;cheapig&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/cheapig/servlet-context.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;!-- Mapeamento dos Servlets e URLs --&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;cheapig&lt;/servlet-name&gt; &lt;url-pattern&gt;*.html&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p><strong>root-context.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"&gt; &lt;!-- Root Context: defines shared resources visible to all other web components --&gt; &lt;!-- Configurações de Internacionalização --&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; &lt;property name="basename" value="classpath:messages" /&gt; &lt;property name="defaultEncoding" value="latin1"/&gt; &lt;/bean&gt; &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt; &lt;property name="paramName" value="lang" /&gt; &lt;/bean&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt; &lt;property name="defaultLocale" value="pt"/&gt; &lt;/bean&gt; &lt;bean id="cheapigFilter" class="br.com.cheapig.seguranca.SegurancaFilter" /&gt; &lt;bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt; &lt;property name="interceptors"&gt; &lt;ref bean="localeChangeInterceptor" /&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p><strong>servlet-context.xml:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt; &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;annotation-driven /&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- Imports user-defined @Controller beans that process client requests --&gt; &lt;beans:import resource="controllers.xml" /&gt; &lt;beans:import resource="hibernateMySQL5.xml"/&gt; &lt;beans:import resource="integracaoTiles2.xml"/&gt; &lt;beans:import resource="servicoEmail.xml"/&gt; &lt;context:component-scan base-package="br.com.cheapig" /&gt; &lt;/beans:beans&gt; </code></pre> <p><strong>SegurancaFilter.java:</strong></p> <pre><code>package br.com.cheapig.seguranca; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SegurancaFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest) { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String reqURI = req.getRequestURI(); System.out.println("Requested URI: " + reqURI); /*if(reqURI.equals("/cheapig/") || reqURI.contains("/cheapig/resources/")){ chain.doFilter(req, res); }else{ req.getRequestDispatcher("acesso/login").forward(req, res); }*/ chain.doFilter(request, response); } } @Override public void destroy() { // TODO Auto-generated method stub } } </code></pre> <p>What am I missing? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload