最近碰到前端和後端兩個項目沒法關聯的問題,接觸到了一個新的知識點,跨域,一直覺得是前端瀏覽器那邊的攔截,後來發現想的有點簡單,特在此整理一下前端
首先在pom文件裏面須要導入兩個jar包java
<!-- java Web 應用中支持跨域請求 --> <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>cors-filter</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>java-property-utils</artifactId> <version>1.10</version> </dependency>
而後再web.xml進行攔截配置web
<!-- Java Web應用中支持跨域請求 --> <filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, POST, HEAD, PUT, DELETE</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified, access-token </param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
而後就解決了跨域這個問題,心滿意足,但是後來看了別人的博客,發現仍是太年輕後端
在此,引用一下博客網址,有興趣能夠去看看http://blog.csdn.net/lmy86263/article/details/51724221跨域