Webflux如何在流内部设置cookie

huangapple go评论93阅读模式
英文:

Webflux how to set cookie from inside stream

问题

我想知道如何在 ServerResponse body 中设置 Cookie。

我找到了一些解决方案,但这些解决方案是在类似这样的 body 函数外部保存 Cookie。

ServerResponse.BodyBuilder response = ServerResponse.ok();
response.cookie(ResponseCookie.from("mycookie", "myVal").build());
return response.body(Mono.just("Welcome"), String.class);

这样可以保存 Cookie,如果我们没有动态 Cookie 值,这个解决方案是可以的。

现在我想要写入来自数据库的 Cookie 值。

像这样:

return response.body(
                Mono.just(101)
                        .flatMap(i ->
                            userRepository
                                    .findById(i)
                                    .map(user -> {
                                        Long time = user.getLastLoginTime();
                                        // 将 time 作为 Cookie 设置,
                                        // 我想要在这里保存 Cookie
                                        return user.getEmail();
                                    })
                        )
                , String.class);

那么如何在 map 函数内部保存 Cookie 呢?由于我正在返回流,WebFlux 将自动订阅它,直到我的流被执行为止。

英文:

I would like to know how could I set cookie from ServerResponse body.

I found few solutions but these are saving cookie from outside body function like this.

ServerResponse.BodyBuilder response = ServerResponse.ok();
response.cookie(ResponseCookie.from("mycookie", "myVal").build());
return response.body(Mono.just("Welcome"), String.class);

This saves cookie and this solution is fine if we do not have dynamic cookie value.
Now I would like to write the cookie value which is coming from db.

like this.

return response.body(
                Mono.just(101)
                        .flatMap(i ->
                            userRepository
                                    .findById(i)
                                    .map(user -> {
                                        Long time = user.getLastLoginTime();
                                        // set time as cookie,
                                        // I would like to save cookie from here
                                        return user.getEmail();
                                    })
                        )
                , String.class);

So how can i save cookie from inside map function and since i am returning stream, webflux will auto subscribe it until then my stream is not executed.

答案1

得分: 4

用以下方式定义您控制器的方法签名,其中包含一个ServerWebExchange参数:

public Mono<String> cookieSetter(ServerWebExchange exchange)

...然后您可以在反应式链的任何位置使用exchange.getResponse().addCookie()。(然而,我建议将其作为专门的副作用,而不是将其捆绑在单个map调用中。)因此,您可以这样做:

return response.body(
        Mono.just(101)
                .flatMap(i ->
                        userRepository
                                .findById(i)
                                .doOnNext(user -> {
                                    exchange.getResponse().addCookie(ResponseCookie.from("time", user.getLastLoginTime())
                                            .httpOnly(true)
                                            .build());
                                })
                                .map(User::getEmail)
                )
        , String.class);
英文:

Define your controller's method signature with a ServerWebExchange parameter like so:

public Mono&lt;String&gt; cookieSetter(ServerWebExchange exchange)

...you can then use exchange.getResponse().addCookie() anywhere in the reactive chain. (I'd do that as a dedicated side-effect however rather than bundling it in a single map call.) So you could do something like:

return response.body(
        Mono.just(101)
                .flatMap(i -&gt;
                        userRepository
                                .findById(i)
                                .doOnNext(user -&gt; {
                                    exchange.getResponse().addCookie(ResponseCookie.from(&quot;time&quot;, user.getLastLoginTime())
                                            .httpOnly(true)
                                            .build());
                                })
                                .map(User::getEmail)
                )
        , String.class);

huangapple
  • 本文由 发表于 2020年6月29日 03:37:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定