Corsfilter配置详解

本文介绍了一种通过使用cors-filter解决跨域请求问题的方法。详细配置包括允许的起源、子域、HTTP方法、请求头等参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要解决跨域请求的问题,原理就不多做介绍了,知道怎么用就行了。

官网:http://software.dzhuvinov.com/cors-filter.html

[b]pom.xml[/b]

<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.4</version>
</dependency>



[b]web.xml[/b]

<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.configurationFile</param-name>
<param-value>cors.properties</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


[b]cors.properties[/b]
[quote]
#cors.allowGenericHttpRequests {true|false} defaults to true.
#If true generic HTTP requests will be allowed to pass through the filter, else only valid and accepted CORS requests will be allowed (strict CORS filtering).
cors.allowGenericHttpRequests = true

#cors.allowOrigin {"*"|origin-list} defaults to *.
#Whitespace-separated list of origins that the CORS filter must allow. Requests from origins not included here will be refused with an HTTP 403 "Forbidden" response. If set to * (asterisk) any origin will be allowed.
cors.allowOrigin = http://fanfq.com

#cors.allowSubdomains {true|false} defaults to false.
#If true the CORS filter will allow requests from any origin which is a subdomain origin of the allowed origins. A subdomain is matched by comparing its scheme and suffix (host name / IP address and optional port number).
cors.allowSubdomains = true

#cors.supportedMethods {method-list} defaults to "GET, POST, HEAD, OPTIONS".
#List of the supported HTTP methods. These are advertised through the Access-Control-Allow-Methods header and must also be implemented by the actual CORS web service. Requests for methods not included here will be refused by the CORS filter with an HTTP 405 "Method not allowed" response.
cors.supportedMethods = GET, POST, HEAD, OPTIONS

#cors.supportedHeaders {"*"|header-list} defaults to *.
#The names of the supported author request headers. These are advertised through the Access-Control-Allow-Headers header.
#If the configuration property value is set to * (asterisk) any author request header will be allowed. The CORS Filter implements this by simply echoing the requested value back to the browser.
#What is an author request header? This any custom header set by the browser JavaScript application through the XMLHttpRequest.setRequestHeader() method.
cors.supportedHeaders = *

#cors.exposedHeaders {header-list} defaults to empty list.
#List of the response headers other than simple response headers that the browser should expose to the author of the cross-domain request through the XMLHttpRequest.getResponseHeader() method. The CORS filter supplies this information through the Access-Control-Expose-Headers header.
cors.exposedHeaders = X-Test-1, X-Test-2

#cors.supportsCredentials {true|false} defaults to true.
#Indicates whether user credentials, such as cookies, HTTP authentication or client-side certificates, are supported. The CORS filter uses this value in constructing the Access-Control-Allow-Credentials header.
cors.supportsCredentials = true

#cors.maxAge {int} defaults to -1 (unspecified).
#Indicates how long the results of a preflight request can be cached by the web browser, in seconds. If -1 unspecified. This information is passed to the browser via the Access-Control-Max-Age header.
cors.maxAge = 3600

#cors.tagRequests {true|false} defaults to false (no tagging).
#Enables HTTP servlet request tagging to provide CORS information to downstream handlers (filters and/or servlets).
cors.tagRequests = false
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值