spring mvc4.2与spring-session整合后的跨域配置
2017-06-16 23:46
260 查看
spring mvc4.2之后增加了CORS跨域配置支持,这功能不错,我非常喜欢,可以通过java的Annotation方式配置,也可以在spring的xml文件中配置。比如这样:
非常方便,也可以在Controller类上加入这个注解
跨域配置后,与spring-session整合会出现跨域请求还没到springmvc的CorsFilter,就被spring-session拦截掉并拿不到cookie,导致拿不到session标识。
这该如何解决呢,经过分析springmvcCorsFilter的源码,找到一个解决方法,就是将CorsFilter放在spring-session之前,使跨域请求先经过CorsFilter,再经过spring-session,就可以解决了。
在web.xml中插入CorsFilter为第一个Filter
这样就解决了先被spring-session拦截跨域请求的问题
<mvc:cors> <mvc:mapping path="/**" allowed-origins="http://localhost:8090" allowed-methods="GET, POST, OPTIONS, PUT, DELETE" allowed-headers="Accept, Origin, X-Requested-With, Content-Type, Last-Modified, Set-Cookie, x-auth-token" exposed-headers="Accept, Origin, X-Requested-With, Content-Type, Last-Modified, Set-Cookie, x-auth-token" allow-credentials="true" max-age="3600" /> </mvc:cors>
非常方便,也可以在Controller类上加入这个注解
@CrossOrigin(origins = "http://www.baidu.com", maxAge = 3600)想要了解更多的可以参考:http://blog.csdn.net/z69183787/article/details/53102112
跨域配置后,与spring-session整合会出现跨域请求还没到springmvc的CorsFilter,就被spring-session拦截掉并拿不到cookie,导致拿不到session标识。
这该如何解决呢,经过分析springmvcCorsFilter的源码,找到一个解决方法,就是将CorsFilter放在spring-session之前,使跨域请求先经过CorsFilter,再经过spring-session,就可以解决了。
在web.xml中插入CorsFilter为第一个Filter
<filter> <filter-name>corsFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>corsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>在spring-config.xml中去掉<mvc:cors/>配置,改为以下配置示例
<bean id="corsFilter" class="org.springframework.web.filter.CorsFilter"> <constructor-arg index="0"> <bean class="org.springframework.web.cors.UrlBasedCorsConfigurationSource"> <property name="corsConfigurations"> <map> <entry key="/**"> <bean class="org.springframework.web.cors.CorsConfiguration"> <property name="allowedOrigins"> <list> <value>http://127.0.0.1:8090</value> <value>http://localhost:8090</value> </list> </property> <property name="allowedMethods" value="*"></property> <property name="allowedHeaders" value="*"></property> <property name="exposedHeaders"> <list> <value>Accept</value> <value>Origin</value> <value>X-Requested-With</value> <value>Content-Type</value> <value>Last-Modified</value> <value>Set-Cookie</value> <value>x-auth-token</value> </list> </property> <property name="allowCredentials" value="true"></property> <property name="maxAge" value="3600"></property> </bean> </entry> </map> </property> </bean> </constructor-arg> </bean>
这样就解决了先被spring-session拦截跨域请求的问题
相关文章推荐
- Spring MVC整理系列(05)————Spring MVC配置解析及整合SpriSpring MVC之@ModelAttribute、@SessionAttributes以及Model的使用介绍
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring、Spring MVC、MyBatis整合文件配置详解
- 【原】保存一下之前spring-session的redis单点登录配置文件【跨域】
- Spring、Spring MVC、MyBatis整合文件配置详解
- Spring3 整合MyBatis3 配置多数据源 动态切换 选择SqlSessionFactory
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring、Spring MVC、MyBatis整合文件配置详解
- Spring、Spring MVC、MyBatis整合文件配置详解
- Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis
- spring mvc 跨域请求处理——spring 4.2 以上
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring、Spring MVC、MyBatis整合文件配置详解
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Spring、Spring MVC、MyBatis整合文件配置详解