imbolc / axum-client-ip

A client IP address extractor for Axum
MIT License
41 stars 13 forks source link

Axum Middleware Support ? #23

Closed TxMat closed 8 months ago

TxMat commented 10 months ago

I need to auth clients based on their IPs on every request so i just thought about using axum middleware but it seems that the client IP is inaccessible from this scope. Am I doing it wrong ? Is it already supported ?

Thanks !

imbolc commented 10 months ago

You can just use extractors in your middleware

banool commented 8 months ago

For anyone wondering what this means, you can just do this:

pub async fn auth<B>(
    State(state): State<AuthMiddlewareState>,
    client_ip: InsecureClientIp,
    config: ConfigExt,
    req: Request<B>,
    next: Next<B>,
) -> Result<Response, MyError> {

This is with Axum 0.6. If you get an error with this, make sure you're using the version of this library that corresponds with the version of Axum you're using. For example, 0.4.2 of axum-client-ip for axum version 0.6. Also make sure you've registered your app like this:

app.into_make_service_with_connect_info::<SocketAddr>()
TxMat commented 8 months ago

Oops, forgot to close the issue !

Thanks to both of you.