Corsfilter配置详解 博客分类: javaee Corsfilter
程序员文章站
2024-02-16 20:52:40
...
主要解决跨域请求的问题,原理就不多做介绍了,知道怎么用就行了。
官网:http://software.dzhuvinov.com/cors-filter.html
pom.xml
web.xml
cors.properties
#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
官网:http://software.dzhuvinov.com/cors-filter.html
pom.xml
<dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>cors-filter</artifactId> <version>2.4</version> </dependency>
web.xml
<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>
cors.properties
引用
#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