从过滤器重定向时发出问题

时间:2012-04-03 12:26:12

标签: java-ee spring-mvc tomcat7 servlet-filters

我在Spring 3 MVC Web应用程序中遇到过滤器问题。我的环境配置如下:SpringSource Tool Suite,JDK 7,Tomcat 7,Maven 3.0.2,Hibernate 3和MySQL 5数据库。

我想要的是什么:

  • 实现一个过滤器,以便它处理所有传入的HTTP请求(进行一些URL阻止),然后将请求转发给Spring处理程序映射器。

我得到了什么:

  • 当我启动服务器(Tomcat)时,一切正常,并显示主页面(index.jsp)。
  • 当我调试时,我可以看到过滤器正在按预期拦截HTTP请求并转发到链中的下一个过滤器。但是,当我尝试从主页面中的链接访问URL并且此链接使用DefaultAnnotationHandlerMapping(例如使用@RequestMethod()注释)映射到方法时,它会拦截URL但转发不能按预期工作(似乎没有被DefaultAnnotationHandlerMapping拦截,并且它在浏览器中显示了ERROR 404(资源不可用)。

为了向您提供一些信息,这些是我的文件:

的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">

<display-name>cheapig</display-name>

<!--Definição do Contexto Global do Container do Spring com recursos (beans) que são compartilhados com
TODOS os Servlets e Filtros -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Cria o container do Spring compartilhado com todos os Servlets e Filtros -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Filtro para controlar acesso -->
<filter>
    <filter-name>cheapigFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

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

</filter-mapping>

<!-- Definição do Servlet que processa todos os requests da aplicação. Como se está utilizando
o Framework Spring, o servlet é o DispatcherServlet. -->
<servlet>
    <servlet-name>cheapig</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/cheapig/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Mapeamento dos Servlets e URLs -->     
<servlet-mapping>
    <servlet-name>cheapig</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>            


  </web-app>

根context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- Configurações de Internacionalização -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="latin1"/>              
</bean>

<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />      
</bean>

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="defaultLocale" value="pt"/>

</bean>

<bean id="cheapigFilter" class="br.com.cheapig.seguranca.SegurancaFilter" />

<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
   <property name="interceptors">
       <ref bean="localeChangeInterceptor" />
   </property>     
</bean>
</beans>

servlet的context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<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">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
<beans:import resource="hibernateMySQL5.xml"/>  
<beans:import resource="integracaoTiles2.xml"/>
<beans:import resource="servicoEmail.xml"/>

<context:component-scan base-package="br.com.cheapig" />
</beans:beans>

SegurancaFilter.java:

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

}

} 

我错过了什么? 提前谢谢!

0 个答案:

没有答案