Skip to main content

CorsConfig

Struct CorsConfig 

Source
pub struct CorsConfig {
    pub allowed_origins: Vec<HeaderValue>,
    pub allowed_methods: Vec<Method>,
    pub allowed_headers: Vec<HeaderName>,
    pub allow_credentials: bool,
    pub max_age: u64,
}
Expand description

CORS 配置结构体

用于配置跨域资源共享的各项参数。

Fields§

§allowed_origins: Vec<HeaderValue>

允许的源列表

§allowed_methods: Vec<Method>

允许的 HTTP 方法列表

§allowed_headers: Vec<HeaderName>

允许的请求头列表

§allow_credentials: bool

是否允许携带凭证

§max_age: u64

预检请求缓存时间(秒)

Implementations§

Source§

impl CorsConfig

Source

pub fn new() -> Self

创建默认的 CORS 配置

默认配置:

  • 允许所有源
  • 允许所有方法
  • 允许所有请求头
  • 不允许凭证
  • 预检缓存时间为 600 秒
Source

pub fn allow_origin(self, origin: impl Into<String>) -> Self

添加允许的源

§参数
  • origin - 允许的源地址,如 “https://example.com”
Source

pub fn allow_origins<I, S>(self, origins: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置允许的源列表

§参数
  • origins - 允许的源地址迭代器
Source

pub fn allow_method(self, method: impl Into<String>) -> Self

添加允许的 HTTP 方法

§参数
  • method - 允许的 HTTP 方法,如 “GET”、“POST”
Source

pub fn allow_methods<I, S>(self, methods: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置允许的 HTTP 方法列表

§参数
  • methods - 允许的 HTTP 方法迭代器
Source

pub fn allow_header(self, header_name: impl Into<String>) -> Self

添加允许的请求头

§参数
  • header_name - 允许的请求头名称,如 “Content-Type”
Source

pub fn allow_headers<I, S>(self, headers: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

设置允许的请求头列表

§参数
  • headers - 允许的请求头名称迭代器
Source

pub fn allow_credentials(self, allow: bool) -> Self

设置是否允许携带凭证

§参数
  • allow - 是否允许凭证
§注意

当启用凭证模式时,不能使用通配符源。

Source

pub fn max_age(self, seconds: u64) -> Self

设置预检请求缓存时间

§参数
  • seconds - 缓存时间(秒)
Source

pub fn into_layer(self) -> CorsLayer

将配置转换为 CorsLayer

§返回

返回配置好的 CorsLayer 实例。

Trait Implementations§

Source§

impl Default for CorsConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more