过滤器或拦截器跨域CORS处理
程序员文章站
2022-07-15 16:02:00
...
在Java程序中加入拦截器,或者过滤器实现request的包装,设置header , httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET,PUT, OPTIONS, DELETE");
关键是这个OPTIONS之后就没有跨域的问题了
方式一:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/" />
<bean class="com.houbank.incoming.web.interceptor.LoginInterceptor" />
</mvc:interceptor>
<bean class="com.houbank.incoming.web.interceptor.CORSInterceptor"/>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
CORSInterceptor
方式二:
CrossDomainFilter
<filter>
<filter-name>cors</filter-name>
<filter-class>com.common.CrossDomainFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>