Authentication success handler

We set up a custom AuthenticationSuccessHandler (the source code for this class is shown next) in our Spring Security configuration class. On successful authentication, it will generate the JWT and also set a HTTP response header:

  • Header name: Authorization
  • Header value: Bearer JWT

Let's take a look at the following code:

public class JWTAuthSuccessHandler implements ServerAuthenticationSuccessHandler{    @Override    public Mono<Void> onAuthenticationSuccess(WebFilterExchange                 webFilterExchange, Authentication authentication) {        ServerWebExchange exchange = webFilterExchange.getExchange();        exchange.getResponse()            .getHeaders()            .add(HttpHeaders.AUTHORIZATION,                     getHttpAuthHeaderValue(authentication)); return webFilterExchange.getChain().filter(exchange); ...

Get Hands-On Spring Security 5 for Reactive Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.