java - 在Spring MVC中设置拦截器

标签 java spring interceptor

我正在尝试处理 SOP,并且需要设置一个过滤器和拦截器,根据 http://patrickgrimard.com/2013/12/12/cross-origin-resource-sharing-cors-requests-with-spring-mvc/ 。 我在 web.xml 中设置了过滤器:

<filter>
    <filter-name>simpleCORSFilter</filter-name>
    <filter-class>base.SimpleCORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>simpleCORSFilter</filter-name>
    <servlet-name>rest</servlet-name>
</filter-mapping>

SimpleCORSFilter 在哪里

@Component
public class SimpleCORSFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest req,
        HttpServletResponse res, FilterChain filterChain)
        throws ServletException, IOException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods",
            "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");  
    filterChain.doFilter(req, res);
}

它有效,因为我的测试 POST 请求被捕获并被操纵。现在,我尝试配置以下拦截器:

@Component
public class SimpleCORSInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    response.addHeader("Access-Control-Allow-Origin", "*");
    return true;
}
}

我在rest-servlet.xml中添加了以下配置(该配置有效,因为正确调用了REST服务):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.lh.clte.web" />
<mvc:annotation-driven />

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/*" />
        <bean class="base.SimpleCORSInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>

但是,重写的方法没有被调用。我错过了什么?

最佳答案

在本文中,过滤器用于使用OPTION 方法的请求,而拦截器则用于“真实”调用。在您的示例中,所有请求都将由过滤器和拦截器处理。所以你不需要拦截器。

此外,只有当有 Controller 方法处理请求时才会调用拦截器。当您调用未映射的 URL 时,不会调用拦截器。

顺便说一句:不需要@Component

关于java - 在Spring MVC中设置拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25503061/

相关文章:

java - 使用 Java 从共享点检索数据并对其进行处理的最佳方法是什么?

java - 如何修复 com.google.gson.stream.MalformedJsonException

java - 在java中从斜率求截距

java - 为什么拦截器会破坏Struts 2中的通配符?

struts2 - token 拦截器和 token session 拦截器之间的区别?

Android 上的 Java 并发

java - Tomcat 无法从过多的流量中恢复

java - 使用 Spring 和 AspectJ 拦截私有(private)方法

java - Spring Data MongoDB 聚合 $out 管道

java - 需要通过 Apache camel-exec 执行带参数的 gzip 命令